【发布时间】:2019-11-30 21:57:35
【问题描述】:
我目前还在学习这个叫做摇摆的咒语,所以我写了这段代码
lblWarning = new JLabel("<html>Incorrect Username or Password<br/> please try again!</html>");
lblWarning.setBounds(10,121,220,48);
lblWarning.setForeground(new Color(150, 0, 0));
lblWarning.setBackground(new Color(255, 255, 255));
lblWarning.setFont(new Font("Tahoma", Font.BOLD, 12));
JButton btnNewButton_1 = new JButton("Confirm");
btnNewButton_1.setFont(new Font("Microsoft JhengHei", Font.BOLD, 14));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(txtUsername.getText()!="user" && txtPassword.getText()!="pass") {
contentPane.add(lblWarning);
}else {
}
}
});
btnNewButton_1.setBounds(10, 180, 94, 23);
contentPane.add(btnNewButton_1);
txtusername 和 txtpassword 是文本字段。但问题是 contentPane (jpanel) 在条件为真时没有添加标签“lblWarning”,但在 actionListener 之外时工作并显示正常,有什么问题?
【问题讨论】:
-
按钮 btnNewButton_1 未添加到任何其他组件。你要点击它吗?
-
如果你的意思是这个 contentPane.add(btnNewButton_1);然后它就在那里,我只是忘了在这里粘贴它
-
不要使用 setBounds()。 Swing 旨在与布局管理器一起使用。在提出问题时发布正确的minimal reproducible example,以便我们更好地了解您的代码上下文。