我是Java GUI开发的新手,我被以下代码卡住了
我愿意接受建议。实际上,我正在尝试创建一个简单的登录,如果密码与数字3124匹配,则会显示OK,否则会显示错误消息。请帮帮我。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class testing {
private JFrame frame;
private JTextField username;
private JTextField password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testing window = new testing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public testing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton_1 = new JButton("cancel");
btnNewButton_1.setBounds(266, 181, 109, 56);
frame.getContentPane().add(btnNewButton_1);
username = new JTextField();
username.setBounds(227, 11, 128, 39);
frame.getContentPane().add(username);
username.setColumns(10);
password = new JTextField();
password.setBounds(227, 76, 128, 39);
frame.getContentPane().add(password);
final int num;
num=Integer.parseInt(password);
password.setColumns(10);
JButton btnNewButton = new JButton("login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(num==3124)
{JOptionPane.showMessageDialog(null, "correct");}
else
{JOptionPane.showMessageDialog(null, "wrong");}
}
});
btnNewButton.setBounds(62, 181, 123, 56);
frame.getContentPane().add(btnNewButton);
}
}
甚至在用户有机会在文本框中输入任何内容之前,您就已经在检查密码了。您需要获取并检查事件侦听器代码中num
的值,即actionPerformed
中的值。此外,不要将密码转换为int
(有些人可能会输入一些非数字字符串(。
下面的代码效果更好。
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
public class Testing {
private JFrame frame;
private JTextField username;
private JTextField password;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Testing window = new Testing();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Testing() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton_1 = new JButton("cancel");
btnNewButton_1.setBounds(266, 181, 109, 56);
frame.getContentPane().add(btnNewButton_1);
username = new JTextField();
username.setBounds(227, 11, 128, 39);
frame.getContentPane().add(username);
username.setColumns(10);
password = new JTextField();
password.setBounds(227, 76, 128, 39);
frame.getContentPane().add(password);
password.setColumns(10);
JButton btnNewButton = new JButton("login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String num;
num = (password.getText());
if (num.equalsIgnoreCase("3124")) {
JOptionPane.showMessageDialog(null, "correct");
} else {
JOptionPane.showMessageDialog(null, "wrong");
}
}
});
btnNewButton.setBounds(62, 181, 123, 56);
frame.getContentPane().add(btnNewButton);
}
}