使用swing时如何确定JCheckBox的选中情况? 现在我有一个窗口上 有几个 JCheckBox 和一个 确定 按钮如何在点击按钮时,得到JCheckBox的选中情况的信息。请高手指点。先谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上一下代码: JFrame f=new JFrame("xxx"); Container cont=f.getContentPane(); JPanel pan=new JPanel(); ButtonGroup bg=new ButtonGroup(); JCheckBox box1=new JCheckBox("1"); JCheckBox box2=new JCheckBox("2"); pan.add(box1); pan.add(box2); pan.setBounds(5, 5, 680, 380); cont.add(pan); f.setLayout(null); but1.setBounds(300,400,100,40); Font fnt=new Font("Sereif",Font.BOLD,14); but1.setFont(fnt); but1.addActionListener(this); f.add(but1); f.setSize(700,500); f.setBackground(Color.WHITE); f.setLocation(100,100); f.setVisible(true); 如何在点击按钮后知道哪个复选框是选中的呢? import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JCheckBox;import javax.swing.JFrame;import javax.swing.JLabel;public class FindCheckBox extends JFrame { private JCheckBox box1; private JCheckBox box2; private JLabel lab1; public FindCheckBox() { initialize(); } public String checkSelBox() { if (box1.isSelected()) { return "选中的为" + box1.getText(); } else if (box2.isSelected()) { return "选中的为" + box2.getText(); } else { return "未选中"; } } private void initialize() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(300, 300); this.setLocationRelativeTo(null); Container cont = this.getContentPane(); cont.setLayout(null); box1 = new JCheckBox("1"); box2 = new JCheckBox("2"); lab1 = new JLabel("未选中"); box1.setBounds(100, 20, 100, 20); box2.setBounds(100, 50, 100, 20); lab1.setBounds(100, 80, 100, 20); box1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { box1.setSelected(true); box2.setSelected(false); lab1.setText(checkSelBox()); } }); box2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { box2.setSelected(true); box1.setSelected(false); lab1.setText(checkSelBox()); } }); cont.add(box1); cont.add(box2); cont.add(lab1); } public static void main(String[] args) { FindCheckBox frame = new FindCheckBox(); frame.setVisible(true); }}给楼主给程序参考下,看下是不是你要的那种效果 通过 box1 和 box2的 public boolean isSelected()的方法 返回按钮的状态。如果选定了切换按钮,则返回 true,否则返回 false。 如果没有定义具体的checkbox1,checkbox2怎么办?如何得到一个checkbox的集合。再进行遍历? 什么叫没有定义具体的checkbox1,checkbox2 ?是指不固定?还是说你直接add(new JCheckBox());这样的? 你如果add(new JCheckBox());这样那肯定不行啊为了可以获得checkbox的引用还是肯定得先声明一个对象再实例化了add进去啊 可以通过getComponents获得所有的组件,然后遍历组件,如果是checkbox类的,就调用isSelected方法比如for (Component c : f.getComponents()) { if (c instanceOf JCheckBox) { if (((JCheckBox)c).isSelected()) { System.out.println("selected"); } }} 利用ActionListener接口,实现对JCheckBox对象的监听,然后进行判断,可以查查API, 有这个方法isSelected() package swing;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class TestJCheckBox implements ActionListener { JFrame frame; JPanel panel1,panel2,panel3; JCheckBox box1,box2,box3,box4; JLabel l1,l2; JTextField txt; JRadioButton button1,button2; String str = "",s = ""; ButtonGroup group; TestJCheckBox(){ frame = new JFrame(); panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); box1 = new JCheckBox("读书、"); box2 = new JCheckBox("写字、"); box3 = new JCheckBox("游泳、"); box4 = new JCheckBox("上网、"); button1 = new JRadioButton("男"); button2 = new JRadioButton("女"); group = new ButtonGroup(); l1 = new JLabel("",JLabel.CENTER); l2 = new JLabel("",JLabel.CENTER); txt = new JTextField(20); } public void display(){ group.add(button1); group.add(button2); panel3.setLayout(new GridLayout(2,1,0,0)); panel3.add(l1); panel3.add(l2); panel1.setLayout(new GridLayout(5,1,0,2)); panel1.add(box1); panel1.add(box2); panel1.add(box3); panel1.add(box4); panel2.setLayout(new GridLayout(2,1,2,5)); panel2.add(button1); panel2.add(button2); frame.getContentPane().setLayout(new BorderLayout()); frame.add(panel3,BorderLayout.NORTH); frame.add(panel1,BorderLayout.WEST); frame.add(panel2,BorderLayout.EAST); frame.setLocation(200, 100); frame.setSize(300,300); frame.setResizable(false); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); box1.addActionListener(this); box2.addActionListener(this); box3.addActionListener(this); box4.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); } public void actionPerformed(ActionEvent arg0) { // TODO 自动生成方法存根 if (arg0.getSource().equals(box1)){ if (box1.isSelected()){ l1.setText(str = str + "读书 "); }else{ l1.setText(str = str.replace("读书", "")); } } if (arg0.getSource().equals(box2)){ if (box2.isSelected()){ l1.setText(str = str + " 写字 "); }else{ l1.setText(str = str.replace("写字", "")); } } if (arg0.getSource().equals(box3)){ if (box3.isSelected()){ l1.setText(str = str + " 游泳 "); }else{ l1.setText(str = str.replace("游泳", "")); } } if (arg0.getSource().equals(box4)){ if (box4.isSelected()){ l1.setText(str = str + " 上网 "); }else{ l1.setText(str = str.replace("上网", "")); } } if (arg0.getSource().equals(button1)){ if (button1.isSelected()){ l2.setText(s = s + "男"); } } if (arg0.getSource().equals(button2)){ if (button2.isSelected()){ l2.setText(s = s + "女"); } } } public static void main(String[] args) { // TODO 自动生成方法存根 new TestJCheckBox().display(); }} 就是直接遍历所有的checkbox,他有个属性是isselected或者其他 反正是和select有关的 comboBox = new JComboBox(); final DefaultComboBoxModel dcbm = new DefaultComboBoxModel(str); comboBox.setModel(dcbm); comboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { comboBoxObject= (String) dcbm.getSelectedItem(); textField_1.setText(comboBoxObject); Object month = prop.get(comboBoxObject); calendar.set(Calendar.MONTH, Integer.parseInt(month.toString())); textField_2.setText(calendar.get(Calendar.YEAR)+":"+calendar.get(Calendar.MONTH)); textField.setText(calendar.getTime()+""); tableClear(); tablePrint(); } }); comboBox.setBounds(10, 10, 104, 22); contentPane.add(comboBox); 正解。拿到上层容器,然后遍历容器中所有的组件,找到类型是JCheckBox的。 如何从一个文本文件读取数据并写入数组并进行查询? 怎么在一个标签上显示另一个标签 静态与泛型可以放一起用么。 帮我解释一下这个回声程序,谢谢! 一个java包中两个应用程序靠什么联系起来?在一个程序里建立另一个的实例对象吗? java int 取值最大是 2的31次密减1么?? 如何用JAVA实现一个数组的快速排序,求代码 希望高手相助! 如何实现目录选择控件!类似于JFileChooser控件! 求教,开发JAVA程序最好用何开发软件? 帮忙看一下几行代码,赋值出错! java开源只开了api的代码,native方法和jvm代码没有开源吧?
Container cont=f.getContentPane();
JPanel pan=new JPanel();
ButtonGroup bg=new ButtonGroup();
JCheckBox box1=new JCheckBox("1");
JCheckBox box2=new JCheckBox("2");
pan.add(box1);
pan.add(box2);
pan.setBounds(5, 5, 680, 380);
cont.add(pan);
f.setLayout(null);
but1.setBounds(300,400,100,40);
Font fnt=new Font("Sereif",Font.BOLD,14);
but1.setFont(fnt);
but1.addActionListener(this);
f.add(but1);
f.setSize(700,500);
f.setBackground(Color.WHITE);
f.setLocation(100,100);
f.setVisible(true);
如何在点击按钮后知道哪个复选框是选中的呢?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;public class FindCheckBox extends JFrame {
private JCheckBox box1;
private JCheckBox box2;
private JLabel lab1;
public FindCheckBox() {
initialize();
}
public String checkSelBox() {
if (box1.isSelected()) {
return "选中的为" + box1.getText();
} else if (box2.isSelected()) {
return "选中的为" + box2.getText();
} else {
return "未选中";
}
}
private void initialize() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setLocationRelativeTo(null);
Container cont = this.getContentPane();
cont.setLayout(null);
box1 = new JCheckBox("1");
box2 = new JCheckBox("2");
lab1 = new JLabel("未选中");
box1.setBounds(100, 20, 100, 20);
box2.setBounds(100, 50, 100, 20);
lab1.setBounds(100, 80, 100, 20);
box1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
box1.setSelected(true);
box2.setSelected(false);
lab1.setText(checkSelBox());
}
});
box2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
box2.setSelected(true);
box1.setSelected(false);
lab1.setText(checkSelBox());
}
});
cont.add(box1);
cont.add(box2);
cont.add(lab1);
}
public static void main(String[] args) {
FindCheckBox frame = new FindCheckBox();
frame.setVisible(true);
}
}
给楼主给程序参考下,看下是不是你要的那种效果
public boolean isSelected()的方法 返回按钮的状态。如果选定了切换按钮,则返回 true,否则返回 false。
如何得到一个checkbox的集合。再进行遍历?
是指不固定?
还是说你直接add(new JCheckBox());这样的?
这样那肯定不行啊
为了可以获得checkbox的引用
还是肯定得先声明一个对象
再实例化了add进去啊
比如
for (Component c : f.getComponents()) {
if (c instanceOf JCheckBox) {
if (((JCheckBox)c).isSelected()) {
System.out.println("selected");
}
}
}
可以查查API, 有这个方法isSelected()
package swing;import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;public class TestJCheckBox implements ActionListener { JFrame frame;
JPanel panel1,panel2,panel3;
JCheckBox box1,box2,box3,box4;
JLabel l1,l2;
JTextField txt;
JRadioButton button1,button2;
String str = "",s = "";
ButtonGroup group; TestJCheckBox(){
frame = new JFrame();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
box1 = new JCheckBox("读书、");
box2 = new JCheckBox("写字、");
box3 = new JCheckBox("游泳、");
box4 = new JCheckBox("上网、");
button1 = new JRadioButton("男");
button2 = new JRadioButton("女"); group = new ButtonGroup(); l1 = new JLabel("",JLabel.CENTER);
l2 = new JLabel("",JLabel.CENTER);
txt = new JTextField(20);
} public void display(){
group.add(button1);
group.add(button2); panel3.setLayout(new GridLayout(2,1,0,0));
panel3.add(l1);
panel3.add(l2); panel1.setLayout(new GridLayout(5,1,0,2));
panel1.add(box1);
panel1.add(box2);
panel1.add(box3);
panel1.add(box4); panel2.setLayout(new GridLayout(2,1,2,5));
panel2.add(button1);
panel2.add(button2); frame.getContentPane().setLayout(new BorderLayout());
frame.add(panel3,BorderLayout.NORTH);
frame.add(panel1,BorderLayout.WEST);
frame.add(panel2,BorderLayout.EAST); frame.setLocation(200, 100);
frame.setSize(300,300);
frame.setResizable(false);
frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
box1.addActionListener(this);
box2.addActionListener(this);
box3.addActionListener(this);
box4.addActionListener(this); button1.addActionListener(this);
button2.addActionListener(this);
} public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
if (arg0.getSource().equals(box1)){
if (box1.isSelected()){
l1.setText(str = str + "读书 ");
}else{
l1.setText(str = str.replace("读书", ""));
}
}
if (arg0.getSource().equals(box2)){
if (box2.isSelected()){
l1.setText(str = str + " 写字 ");
}else{
l1.setText(str = str.replace("写字", ""));
}
}
if (arg0.getSource().equals(box3)){
if (box3.isSelected()){
l1.setText(str = str + " 游泳 ");
}else{
l1.setText(str = str.replace("游泳", ""));
}
}
if (arg0.getSource().equals(box4)){
if (box4.isSelected()){
l1.setText(str = str + " 上网 ");
}else{
l1.setText(str = str.replace("上网", ""));
}
} if (arg0.getSource().equals(button1)){
if (button1.isSelected()){
l2.setText(s = s + "男");
}
}
if (arg0.getSource().equals(button2)){
if (button2.isSelected()){
l2.setText(s = s + "女");
}
}
} public static void main(String[] args) {
// TODO 自动生成方法存根
new TestJCheckBox().display();
}
}
comboBox = new JComboBox();
final DefaultComboBoxModel dcbm = new DefaultComboBoxModel(str);
comboBox.setModel(dcbm);
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
comboBoxObject= (String) dcbm.getSelectedItem();
textField_1.setText(comboBoxObject); Object month = prop.get(comboBoxObject);
calendar.set(Calendar.MONTH, Integer.parseInt(month.toString()));
textField_2.setText(calendar.get(Calendar.YEAR)+":"+calendar.get(Calendar.MONTH));
textField.setText(calendar.getTime()+"");
tableClear();
tablePrint();
}
});
comboBox.setBounds(10, 10, 104, 22);
contentPane.add(comboBox);