import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Gui { public static void main(String[] args) { JFrame jf = new JFrame("用户登录"); jf.setSize(400, 300); jf.setDefaultCloseOperation(3); jf.setLocationRelativeTo(null); // jf.setAlwaysOnTop(true); jf.setVisible(true); JButton jButton = new JButton("登录"); jButton.setBounds(50,200,280,20); jf.setLayout(null); jf.add(jButton); JLabel jLabel = new JLabel("用户名"); jLabel.setBounds(50,50,50,20); jf.add(jLabel); JLabel jLabel2 = new JLabel("密码"); jLabel2.setBounds(50,100,50,20); jf.add(jLabel2); JTextField jTextField1 = new JTextField(); jTextField1.setBounds(150,50,180,20); jf.add(jTextField1); JPasswordField jTextField2 = new JPasswordField(); jTextField2.setBounds(150,100,180,20); jf.add(jTextField2); jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(jTextField1.getText().equals("root") && jTextField2.getText().equals("123456")){ JOptionPane.showMessageDialog(null,"登录成功"); }else{ JOptionPane.showMessageDialog(null,"登录失败"); } } }); } }
当输入错误的用户名或密码时发出消息提示登录失败
当输入正确的用户名和密码时发出消息提示登录成功