如何在属于按钮组的第二次单击时取消切换 jtogglebutton
How to untoggle jtogglebuttons on second click that belongs to ButtonGroups
如何在第二次单击时取消切换属于 ButtonGroups 的 jtogglebuttons?我应该为此使用什么侦听器?谢谢!
JtoggleButton toggleButton;
toggleButton.addActionListener?
toggleButton.addChangeListener?
public class CustomButtonGroup extends ButtonGroup {
@Override
public void setSelected(ButtonModel model, boolean selected) {
if (selected) {
super.setSelected(model, selected);
} else {
clearSelection();
}
}
}
How to untoggle jtogglebuttons that belongs to ButtonGroups on second click? What listener should I use for this?
None,ButtonGroup
会自动处理,这就是重点。 ButtonGroup
一次只允许在组中选择一个按钮。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public JavaApplication243() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
ButtonGroup bg = new ButtonGroup();
JToggleButton btn1 = new JToggleButton("One");
JToggleButton btn2 = new JToggleButton("Two");
bg.add(btn1);
bg.add(btn2);
add(btn1);
add(btn2);
}
}
}
如何在第二次单击时取消切换属于 ButtonGroups 的 jtogglebuttons?我应该为此使用什么侦听器?谢谢!
JtoggleButton toggleButton;
toggleButton.addActionListener? toggleButton.addChangeListener?
public class CustomButtonGroup extends ButtonGroup {
@Override
public void setSelected(ButtonModel model, boolean selected) {
if (selected) {
super.setSelected(model, selected);
} else {
clearSelection();
}
}
}
How to untoggle jtogglebuttons that belongs to ButtonGroups on second click? What listener should I use for this?
None,ButtonGroup
会自动处理,这就是重点。 ButtonGroup
一次只允许在组中选择一个按钮。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public JavaApplication243() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
ButtonGroup bg = new ButtonGroup();
JToggleButton btn1 = new JToggleButton("One");
JToggleButton btn2 = new JToggleButton("Two");
bg.add(btn1);
bg.add(btn2);
add(btn1);
add(btn2);
}
}
}