日期:2014-05-20  浏览次数:20749 次

请教:判断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);
    }
}