叮当猫咪
我将使用@ JToggelButton,如这里所示,或委托给包含的按钮,如@duffymo建议的那样。如果您确实需要定制OnOffSwitchEvent,则在EventListenerList其中概述了标准接线,每个接线盒中都包含一个实例JComponent。附录:这是一个委派给ButtonGroup包含两个按钮的示例。标签上装饰有符号,但是任何实现Icon都更加灵活。import java.awt.Color;import java.awt.EventQueue;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ButtonGroup;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JToggleButton;/** @see https://stackoverflow.com/questions/6035834 */public class ButtonGroupTest extends JComponent { private static final String ON = "On"; private static final String OFF = "Off"; private final JToggleButton bOn = new JToggleButton(ON); private final JToggleButton bOff = new JToggleButton(OFF); private final JLabel label = new JLabel(" \u2301 "); private final ButtonHandler handler = new ButtonHandler(); public ButtonGroupTest() { this.setLayout(new FlowLayout()); label.setOpaque(true); label.setBackground(Color.red); label.setFont(label.getFont().deriveFont(36f)); ButtonGroup bg = new ButtonGroup(); this.add(bOn); bg.add(bOn); bOn.setSelected(true); bOn.addActionListener(handler); this.add(label); this.add(bOff); bg.add(bOff); bOff.addActionListener(handler); } public void addActionListener(ActionListener listener) { bOn.addActionListener(listener); bOff.addActionListener(listener); } private class ButtonHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (ON.equals(cmd)) { label.setBackground(Color.red); } else { label.setBackground(Color.black); } } } private void display() { JFrame f = new JFrame("ButtonGroupTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ButtonGroupTest().display(); } }); }}