编程语言
首页 > 编程语言> > Swing程序设计实践与练习3

Swing程序设计实践与练习3

作者:互联网

public class asd extends JFrame {

    public asd() {
        // TODO Auto-generated constructor stub
        Container c = getContentPane();
        setLayout(new GridLayout(3, 1, 5, 5));
        final JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
        jp1.setLayout(new GridLayout(1, 2, 5, 5));
        jp2.setLayout(new GridLayout(1, 2, 5, 5));
        jp3.setLayout(new GridLayout(1, 2, 5, 5));
        
        
        jp1.add(new JLabel("用户名"));
        final JTextField jt = new JTextField();
        jp1.add(jt);
        
        
        jp2.add(new JLabel("密码"));
        final JPasswordField jp = new JPasswordField();
        jp2.add(jp);

        JButton jb1 = new JButton("登录");
        JButton jb2 = new JButton("重置");
        jp3.add(jb1);
        jp3.add(jb2);
        
        jb1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                // TODO Auto-generated method stub
                if (jt.getText().trim().equals("mr")&&jp.getText().trim().equals("mrsoft")) {
                    JOptionPane.showMessageDialog(null, "登陆成功");
                }
                else{
                    JOptionPane.showMessageDialog(null, "登陆失败,用户名或密码错误");
                }
            }
        });
        
        jb2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                jt.setText("");
                jp.setText("");
            }
        });
        
        c.add(jp1);
        c.add(jp2);
        c.add(jp3);
        setSize(200, 150);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new asd();
    }

}

标签:练习,JPanel,Swing,add,new,jp1,程序设计,jp3,jp2
来源: https://www.cnblogs.com/dulute/p/10588358.html