Java中的重置单选按钮
我有三个单选按钮在一个按钮组。我想在Java中的重置单选按钮,java,swing,actionlistener,jradiobutton,Java,Swing,Actionlistener,Jradiobutton,我有三个单选按钮在一个按钮组。我想在JButton上单击,将单选按钮重置为未填充。我尝试了你提出的逻辑建议,键入.set,所有的布尔值都没有达到我想要的效果。如果您有任何建议,我们将不胜感激。谢谢 简单地使用 例如 import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraint
JButton
上单击,将单选按钮重置为未填充。我尝试了你提出的逻辑建议,键入.set
,所有的布尔值都没有达到我想要的效果。如果您有任何建议,我们将不胜感激。谢谢 简单地使用
例如
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 {
private JRadioButton[] buttons;
private ButtonGroup buttonGroup;
public TestPane() {
buttons = new JRadioButton[] {
new JRadioButton("Nuclear"),
new JRadioButton("Gas"),
new JRadioButton("Stream"),
new JRadioButton("Peddle"),
new JRadioButton("Electric")
};
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
buttonGroup = new ButtonGroup();
for (JRadioButton btn : buttons) {
add(btn, gbc);
buttonGroup.add(btn);
}
JButton clear = new JButton("Clear");
add(clear, gbc);
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonGroup.clearSelection();
}
});
}
}
}
简单使用
例如
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 {
private JRadioButton[] buttons;
private ButtonGroup buttonGroup;
public TestPane() {
buttons = new JRadioButton[] {
new JRadioButton("Nuclear"),
new JRadioButton("Gas"),
new JRadioButton("Stream"),
new JRadioButton("Peddle"),
new JRadioButton("Electric")
};
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
buttonGroup = new ButtonGroup();
for (JRadioButton btn : buttons) {
add(btn, gbc);
buttonGroup.add(btn);
}
JButton clear = new JButton("Clear");
add(clear, gbc);
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonGroup.clearSelection();
}
});
}
}
}
请张贴您迄今为止尝试过的内容,我尝试过(错误);setContentAreaFilled(假);按钮。重新验证();button.firePropertyChange(null、true、false);setIconPainted(假);还有一些我想不起来了。请把你到目前为止尝试过的东西贴出来,我试过了(错);setContentAreaFilled(假);按钮。重新验证();button.firePropertyChange(null、true、false);setIconPainted(假);还有一些我想不起来了。哦,天哪,你太棒了。我完全忘了检查按钮组本身能做什么。谢谢你:天哪,你太棒了。我完全忘了检查按钮组本身能做什么。谢谢:D