【发布时间】:2021-11-06 01:29:54
【问题描述】:
有人可以帮忙吗?这是一个您登录并弹出另一个框架的系统。在那个框架里你可以放两个词,框架会告诉你作品是否相同。但是由于某种原因该按钮不起作用。我已经这样做了 4 天,我真的需要一些帮助。
代码
public class Userinter implements ActionListener {
//first frame
private static JLabel userLabel;
private static JTextField userText;
private static JLabel passwordLabel;
private static JPasswordField passwordText;
private static JButton button;
private static JLabel success;
//second frame
private static JLabel label1;
private static JLabel label2;
private static JTextField WordNum1;
private static JTextField WordNum2;
private static JButton button2;
private static JLabel success2;
public static void main(String[] args) {
//frame
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
//username
userLabel = new JLabel("Username");
userLabel.setBounds(80,80,80,25);
panel.add(userLabel);
userText = new JTextField(20);
userText.setBounds(180,80,165,25);
panel.add(userText);
//password
passwordLabel = new JLabel("password");
passwordLabel.setBounds(80,150,80,25);
panel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(180,150,165,25);
panel.add(passwordText);
//button
button = new JButton("Login");
button.setBounds(80,190,80,25);
button.addActionListener((ActionListener) new Userinter());
panel.add(button);
success = new JLabel("set");
success.setBounds(80,250,300,25);
panel.add(success);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String user = userText.getText();
String password = passwordText.getText();
if (user.equals("Gal") && password.equals("Skr")){
// second frame
JPanel Panel2 = new JPanel();
JFrame frame2 = new JFrame();
frame2.setSize(400,300);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
frame2.add(Panel2);
Panel2.setLayout(null);
label1 = new JLabel("Set word/number here:");
label1.setBounds(10,20,200,25);
Panel2.add(label1);
WordNum1 = new JTextField();
WordNum1.setBounds(100,50,165,25);
Panel2.add(WordNum1);
label2 = new JLabel("Set word/number here:");
label2.setBounds(10,90,200,25);
Panel2.add(label2);
WordNum2 = new JTextField();
WordNum2.setBounds(100,130,165,25);
Panel2.add(WordNum2);
JButton Button2 = new JButton("Check");
Button2.setBounds(100,200,80,25);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e1) {
String word = WordNum1.getText();
String word2 = WordNum2.getText();
if(word.equals(word2)){
success2.setText("yes");
}
}
});
Panel2.add(Button2);
success = new JLabel("set");
success.setBounds(80,250,300,25);
Panel2.add(success);
}else
success.setText("incorrect username or/and password.");
}
}
【问题讨论】:
-
panel.setLayout(null);是我要停下来的地方——这是个坏主意 -
但按钮不起作用 - 我们不知道您在说什么按钮。无论如何,您的代码结构很差。从阅读 How to Use Buttons 上的 Swing 教程开始。
ButtonDemo可以通过单击“示例索引”链接找到,这是开始一些基本知识的好地方。它将向您展示:1)为您的代码提供更好的结构。 2)如何创建一个带有按钮的框架。然后你只需修改 ActionListener 代码来做你想做的事。 -
另外,您的弹出窗口应该是 JDialog,而不是 JFrame。变量名称不应以大写字符开头。