[英]GUI… Why isnt my button responding to the first if and always heading to the next option
输入密码后,我希望窗口消失并弹出一个新窗口。
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
if(passwordField.equals("test"))
{
frame.setVisible(false);
}
else if(!passwordField.equals("test"))
{
JOptionPane.showMessageDialog(null,"Access Denied!!");
}
}
});
btnEnter.setBounds(149, 184, 117, 29);
frame.getContentPane().add(btnEnter);
我假设passwordField是一个JTextField,如果是这样,则需要从中获取文本,只是我认为的.getText()
并将其存储在字符串中。 然后测试字符串。 目前,您正在测试JTextField是否等于字符串。
创建2个JFrame,并为每个JFrame进行引用:
JFrame oldFrame = new JFrame();
// ...
JFrame newFrame = new JFrame();
// ...
// ...
if(passwordField.equals("test"))
{
oldFrame.setVisible(false);
newFrame.setVisible(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.