我想知道我是否可以创建一个可以关闭的JLabel。 这是我的代码:
<div id="outer">
<h2>blah</h2>
<div id="inner"><!--content--></div>
</div>
该程序基本上是一个登录/注册的东西。我制作了两个分别存储用户名和密码的数组。在用户注册后,我制作了一个Jlabel来表示感谢您注册,但我希望能够被用户关闭。 现在我需要将消息的可见性设置为false,但还有另一种方法吗?
答案 0 :(得分:0)
是的,还有另一种方式。您可以使用已经格式化为看起来像标签的JButton,而不是使用标签。除此之外,当您点击它时,文本消失。添加动作侦听器以在单击按钮时删除文本。请尝试下面的代码。新的和更改的代码标有相应的注释。
package banklogin;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Color;
public class BankLogin {
String usernameuser;
String passworduser;
ArrayList<String> Usernames = new ArrayList<String>();
ArrayList<String> Passwords = new ArrayList<String>();
private JFrame frame;
private JTextField Username;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BankLogin window = new BankLogin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public BankLogin() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 508, 381);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton Messages = new JButton("New label"); // new
Messages.setBorderPainted(false); // new
Messages.setBackground(new Color(240,240,240)); // new
Messages.setFont(new Font("Tahoma", Font.PLAIN, 18)); // changed
Messages.setBounds(50, 175, 383, 50); // changed
frame.getContentPane().add(Messages);
Messages.setVisible(false);
Username = new JTextField();
Username.setBounds(231, 66, 131, 30);
frame.getContentPane().add(Username);
Username.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(231, 132, 131, 30);
frame.getContentPane().add(textField_1);
JLabel lblUsername = new JLabel("Username");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblUsername.setBounds(109, 66, 112, 36);
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblPassword.setBounds(109, 125, 112, 36);
frame.getContentPane().add(lblPassword);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
Messages.addActionListener(new ActionListener() { // new
public void actionPerformed(ActionEvent e) { // new
Messages.setText(""); // new
} // new
}); // new
btnLogin.setBounds(109, 240, 89, 23);
frame.getContentPane().add(btnLogin);
JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if((lblUsername.getText()).length() != 0 && (lblPassword.getText()).length() != 0){
Usernames.add(lblUsername.getText());
Passwords.add(lblPassword.getText());
Messages.setVisible(true);
Messages.setText("Thank you for creating an account.");
// Messages.setVisible(false);
}
else{
Messages.setVisible(true);
Messages.setText("Error");
}
}
});
btnRegister.setBounds(273, 240, 89, 23);
frame.getContentPane().add(btnRegister);
}
}