我想取消选择我在JFrame中的程序中的单选按钮,它有一些方法可以通过一个名为Clean的按钮来取消选择所有单选按钮吗?此按钮将清除辐射按钮和具有的文本。
行动的代码将使所有人认为干净:
JButton btnClean = new JButton("Clean");
btnClean.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnClean.setBounds(208, 184, 126, 28);
contentPane.add(btnClean);
答案 0 :(得分:5)
通常,JRadioButtons在ButtonGroup对象中组合在一起,这是一个逻辑对象,一次只能选择一个JRadioButton。你想要做的是在这个ButtonGroup对象上调用clearSelection()
。
另外,您希望在GUI中使用null
布局。虽然null布局和setBounds()
似乎是Swing新手,比如创建复杂GUI的最简单和最好的方法,但是你创建的Swing GUI越多,你在使用它们时会遇到更严重的困难。 。当GUI调整大小时,他们不会调整组件的大小,他们是增强或维护的皇室女巫,当他们放置在滚动窗格中时,他们完全失败,当他们在所有平台或屏幕分辨率不同时看起来很糟糕原来的。
例如
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class BunchOButtons extends JPanel {
private static final String[] RADIO_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private ButtonGroup weekDayBtnGroup = new ButtonGroup();
public BunchOButtons() {
// JRadioButtons love being held in JPanels that use GridLayout
JPanel radioPanel = new JPanel(new GridLayout(1, 0));
radioPanel.setBorder(BorderFactory.createTitledBorder("Week Days"));
for (String radioText : RADIO_TEXTS) {
JRadioButton radioButton = new JRadioButton(radioText); // create radiobtn
radioButton.setActionCommand(radioText); // set its action command text
weekDayBtnGroup.add(radioButton); // add to ButtonGroup
radioPanel.add(radioButton); // add to a GridLayout-using JPanel
}
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton(new AbstractAction("Show Selection") {
@Override
public void actionPerformed(ActionEvent evt) {
ButtonModel model = weekDayBtnGroup.getSelection();
if (model != null) {
// if something is selected
System.out.println(model.getActionCommand());
} else {
System.out.println("No weekday selected");
}
}
}));
buttonPanel.add(new JButton(new AbstractAction("Clear Selection") {
@Override
public void actionPerformed(ActionEvent e) {
weekDayBtnGroup.clearSelection();
}
}));
setLayout(new BorderLayout());
add(radioPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
BunchOButtons mainPanel = new BunchOButtons();
JFrame frame = new JFrame("BunchOButtons");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
答案 1 :(得分:1)
由于您的JFrame
可能包含任意嵌套在框架布局中的许多JRadioButton
个组件,您可能需要使用Container.getComponents
递减组件层次结构,并检查每个组件是否为instanceof { {1}},在这种情况下你应该强制转换JRadioButton
或.setSelected(false)
,在这种情况下你应该继续递归。要获取Container
的顶级容器,请使用JFrame
。
用一个简单的例子来说明这种方法(包含OP请求的JFrame.getContentPane
方法的实现):
Clean
当然,正如其他人所建议的那样,您很可能想要将单选按钮的选择状态管理分配给public class FrameWithRadios extends JFrame {
public FrameWithRadios() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panelCenter = new JPanel();
panelCenter.setLayout(new GridLayout(1, 2));
JRadioButton rb11 = new JRadioButton("Option 1.1");
JRadioButton rb12 = new JRadioButton("Option 1.2");
JRadioButton rb13 = new JRadioButton("Option 1.3");
JPanel panelLeft = new JPanel();
panelLeft.setLayout(new GridLayout(3, 1));
panelLeft.add(rb11);
panelLeft.add(rb12);
panelLeft.add(rb13);
panelCenter.add(panelLeft);
JRadioButton rb21 = new JRadioButton("Option 2.1");
JRadioButton rb22 = new JRadioButton("Option 2.2");
JRadioButton rb23 = new JRadioButton("Option 2.3");
JPanel panelRight = new JPanel();
panelRight.setLayout(new GridLayout(3, 1));
panelRight.add(rb21);
panelRight.add(rb22);
panelRight.add(rb23);
panelCenter.add(panelRight);
JButton bClean = new JButton("Clean");
bClean.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clean(getContentPane());
}
});
JPanel panelBottom = new JPanel();
panelBottom.add(bClean);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(panelCenter, BorderLayout.CENTER);
cp.add(panelBottom, BorderLayout.SOUTH);
}
protected void clean(Component comp) {
if (comp instanceof JRadioButton) {
((JRadioButton) comp).setSelected(false);
}
else if (comp instanceof Container) {
for (Component child: ((Container) comp).getComponents()) {
clean(child);
};
}
}
public static void main(String[] args) {
FrameWithRadios frame = new FrameWithRadios();
frame.setSize(320, 200);
frame.setVisible(true);
}
}
并使用ButtonGroup
取消选择所有内容的方法(以上内容不适用于ButtonGroup.clearSelection
中的单选按钮。)