请教:判断JToggleButton的select状态设置不同背景色问题
想实现一个效果:JToggleButton,当没按下去时显示绿色,按下去时显示黄色。
但不知为什么按下去后显示不出黄色来,而是默认的灰色。
请教各位高手,这是什么原因?以及要达到这个目的应该如何做!
非常感谢各位高手,有提示也欢迎,谢谢!
代码如下:
- Java code
package test24Buttons;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JToggleButton;public class Hour { JPanel timePanel = new JPanel(); JBusyTimeButton btn1 = new JBusyTimeButton("1"); JBusyTimeButton btn2 = new JBusyTimeButton("2"); JBusyTimeButton btn3 = new JBusyTimeButton("3"); JBusyTimeButton btn4 = new JBusyTimeButton("4"); JBusyTimeButton btn5 = new JBusyTimeButton("5"); JBusyTimeButton btn6 = new JBusyTimeButton("6"); public Hour() { timePanel.add(btn1); timePanel.add(btn2); timePanel.add(btn3); timePanel.add(btn4); timePanel.add(btn5); timePanel.add(btn6); } public JPanel getBusyTimePanel() { return timePanel; } public class JBusyTimeButton extends JToggleButton { private static final long serialVersionUID = 1L; public JBusyTimeButton(String text) { super(text); setBackground(Color.green); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!((JToggleButton)e.getSource()).getModel().isSelected()) { ((JToggleButton)e.getSource()).setBackground(Color.green); } else { ((JToggleButton)e.getSource()).setBackground(Color.yellow); } } }); } } public static void main(String[] args) { JFrame frame = new JFrame(); Hour bt = new Hour(); frame.add(bt.getBusyTimePanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }}[解决办法]
我改了一下第一次按下是灰色的,第二次点击是黄色背景,我也不是很清楚为什么出现这个效果
发现如果设置前景色的话一切会是按照逻辑走
- Java code
import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class Hour { JPanel timePanel = new JPanel(); JBusyTimeButton btn1 = new JBusyTimeButton("1"); JBusyTimeButton btn2 = new JBusyTimeButton("2"); JBusyTimeButton btn3 = new JBusyTimeButton("3"); JBusyTimeButton btn4 = new JBusyTimeButton("4"); JBusyTimeButton btn5 = new JBusyTimeButton("5"); JBusyTimeButton btn6 = new JBusyTimeButton("6"); public Hour() { timePanel.add(btn1); timePanel.add(btn2); timePanel.add(btn3); timePanel.add(btn4); timePanel.add(btn5); timePanel.add(btn6); } public JPanel getBusyTimePanel() { return timePanel; } public class JBusyTimeButton extends JToggleButton { private static final long serialVersionUID = 1L; public JBusyTimeButton(String text) { super(text); setBackground(Color.green); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// if (!((JToggleButton)e.getSource()).isSelected()) {// ((JToggleButton)e.getSource()).setBackground(Color.green);// } else {// ((JToggleButton)e.getSource()).setBackground(Color.YELLOW);// } JToggleButton j=(JToggleButton)e.getSource(); ButtonModel bm=j.getModel(); if(bm.isSelected()) { j.setBackground(Color.YELLOW); System.out.println("奇数次按下,更改颜色"); } } }); } } public static void main(String[] args) { JFrame frame = new JFrame(); Hour bt = new Hour(); frame.add(bt.getBusyTimePanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }}
[解决办法]
我做实验的时候也是猜到了是执行了setBackgroud的方法,但是因为多线程的机制没有及时重绘按钮
不过也没整清楚怎么改,另一方面,我用前景色设置逻辑上都是很正常的。
我觉得能满足需求的情况下,用按钮的Icon来解决是不错的选择,给你贴出来代码,代码里面获取image的方法不推荐,虽然可以跑
- Java code
public JBusyTimeButton(String text) { super(text); setBackground(Color.green); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JToggleButton j=(JToggleButton)e.getSource(); ButtonModel bm=j.getModel(); if(bm.isSelected()) {// j.setBackground(Color.YELLOW); //这里找一张跟按钮恰好大小的图片就可以了 j.setIcon(new ImageIcon("resources/images/thu2.jpeg")); System.out.println("奇数次按下,更改颜色"); } else{ j.setIcon(null); } } });
[解决办法]
- Java code
public class JBusyTimeButton extends JToggleButton { private static final long serialVersionUID = 3243445422431L; public JBusyTimeButton(String text) { super(text); setBackground(Color.GREEN); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JToggleButton toggle = (JToggleButton)e.getSource(); if(toggle.isSelected()) { toggle.setBackground(Color.YELLOW); }else { toggle.setBackground(Color.GREEN); } } }); } }
[解决办法]
很,代码证明你这个根本不会变成黄色背景色一次……
楼主还是用Icon的方法解决吧,可以参照我写的那个例子
[解决办法]
public class JBusyTimeButton extends JToggleButton {
private static final long serialVersionUID = 3243445422431L;
public JBusyTimeButton(String text) {
super(text);
setBackground(Color.GREEN);
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JToggleButton toggle = (JToggleButton)e.getSource();
if(toggle.isSelected()) {
toggle.setBackground(Color.YELLOW);
}else {
toggle.setBackground(Color.GREEN);
}
}
});
}
}
[解决办法]
[解决办法]
楼上的方法可以啊!牛人,学习