我目前仍在学习名为swing的咒语,所以我编写了此代码lblWarning = new JLabel("Incorrect Username or Password
please try again!");
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是文本字段btw。但是问题是当条件为真时,contentPane(jpanel)不会添加标签“ lblWarning”,但是可以正常工作,并且在actionListener外部时可以正常显示,这是什么问题?