[英]How to change JButton Color while pressed
I'm trying to make a Login / Register system.我正在尝试制作登录/注册系统。 I'm trying to make it so while a button is Pressed it has a different colour instead of it being the default aqua colour.我正在尝试使它在按下按钮时具有不同的颜色,而不是默认的浅绿色。
Here is one of my JButtons这是我的一个 JButton
login = new JButton("Login");
login.setFont(f2);
Dimension size2 = login.getPreferredSize();
login.setBounds(105, 85, size2.width, size2.height);
login.addActionListener(new LoginSystem());
login.setBackground(Color.WHITE);
login.setForeground(Color.BLACK);
Any way for me to make it so it look different while pressed?我有什么办法让它在按下时看起来不一样?
You have to add a MouseAdapter you button's MouseListener.您必须为按钮的 MouseListener 添加一个 MouseAdapter。 For example:例如:
login.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// do stuff here, change button background for example ...
login.setBackground(Color.BLACK);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.