java 选中复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2228880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selected Checkbox
提问by greenLizard
I am new to swing, I would like to ask how can I make this code , so when the submit button is pressed to get the selected checkboxes and display their names.
我是swing的新手,我想问一下如何制作此代码,以便在按下提交按钮时获取所选复选框并显示其名称。
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class ACheckBox {
public static void main(String args[]) {
String title = (args.length == 0 ? "CheckBox Sample" : args[0]);
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Pizza Toppings");
panel.setBorder(border);
JCheckBox check = new JCheckBox("Anchovies");
panel.add(check);
check = new JCheckBox("Garlic");
panel.add(check);
check = new JCheckBox("Onions");
panel.add(check);
check = new JCheckBox("Pepperoni");
panel.add(check);
check = new JCheckBox("Spinach");
panel.add(check);
JButton button = new JButton("Submit");
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
回答by Adamski
You need to define a bespoke ActionListenerimplementation whose job is to analyse each JCheckBox's selected state when an action is fired; i.e. when the "Submit" button is clicked.
你需要定义一个定制的ActionListener实现,它的工作是JCheckBox在一个动作被触发时分析每个的选择状态;即当“提交”按钮被点击时。
// ActionListener implementation that maintains a reference to each JCheckBox.
// We will register this listener with the Submit button.
public class MyActionListener implements ActionListener {
private final List<JCheckBox> checkBoxes = new LinkedList<JCheckBox>();
/**
* Adds the specified JCheckBox to the list of JCheckBoxes.
*/
public void addCheckBox(JCheckBox checkBox) {
this.checkBoxes.add(checkBox);
}
/**
* Called when the Submit button is pressed.
*/
public void actionPerformed(ActionEvent evt) {
StringBuilder sb = new StringBuilder();
sb.append("Selected Check Boxes: ");
// Iterate over each JCheckBox and build message ready for display.
// Could do something more sophisticated here if required.
for (JCheckBox checkBox : checkBoxes) {
if (checkBox.isSelected()) {
sb.append(checkBox.getText()).append(' ');
}
}
JOptionPane.showMessageDialog(null, sb);
}
}
// 1. Create ActionListener implementation.
MyActionListener al = new MyActionListener();
// 2. Register JCheckBoxes with ActionListener.
al.addCheckBox(checkBox);
// etc.
// 3. Finally register ActionListener with Submit button.
submitButton.addActionListener(al);
回答by Mark
You need to add an ActionListner to your button and then query the state of the check boxes.
您需要将 ActionListner 添加到您的按钮,然后查询复选框的状态。
I suggest you read How to Use Buttons, Check Boxes and Radio Buttonsfrom the Swing Tutorial.
我建议您阅读Swing 教程中的如何使用按钮、复选框和单选按钮。
回答by finnw
If you don't want to create a new Actionfor every checkbox (e.g. if the checkboxes are added dynamically) you can do something like this:
如果您不想Action为每个复选框创建一个新复选框(例如,如果复选框是动态添加的),您可以执行以下操作:
for (Component child: panel.getComponents()) {
if (child instanceof JCheckBox) {
JCheckBox checkBox = (JCheckBox) child;
if (checkBox.isSelected()) {
System.out.println(checkBox.getAction().getValue(Action.NAME));
}
}
}

