取得JCheckBox选中的个数

yuyu622 2007-06-06 01:24:30
比如说我有四个JCheckBox,我想在点击一个JButton的时候,统计出我选择了多个个JCheckBox项
...全文
573 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
candy123360 2007-06-12
  • 打赏
  • 举报
回复
给你个例子参考,应该是有帮助的
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class GUIExample1 extends JFrame implements ItemListener,ActionListener{

private static final String courses[]={"CS 101","CS 102","CS 210","CS 215","CS 217","CS 302"};
private Set selectedCourses;
private JTextArea display;
public GUIExample1(String title){
super(title);
selectedCourses=new TreeSet();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,250);
getContentPane().setLayout(new BorderLayout());
JLabel l1=new JLabel("Please select the courses you wish to take.Click on the Finished button when done.");
getContentPane().add(l1,BorderLayout.NORTH);
JPanel checkPanel=new JPanel();
checkPanel.setLayout(new GridLayout(courses.length,0));
checkPanel.setBorder(BorderFactory.createEtchedBorder());
for(int i=0;i<courses.length;i++){
JCheckBox box=new JCheckBox(courses[i]);
box.addItemListener(this);
checkPanel.add(box);
}
getContentPane().add(checkPanel,BorderLayout.WEST);
JButton done=new JButton("Finished");
done.addActionListener(this);
getContentPane().add(done,BorderLayout.SOUTH);
display=new JTextArea(10,20);
display.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Courses Selected"));
display.setBackground(getBackground());
getContentPane().add(display,BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent event){
String courseName=((JCheckBox)event.getItem()).getText();
if(event.getStateChange()==ItemEvent.SELECTED){
selectedCourses.add(courseName);
}
else{
selectedCourses.remove(courseName);
}
}
public void actionPerformed(ActionEvent event){
String text="";
if(selectedCourses.size()<1||selectedCourses.size()>3){
text="You must select between 1 and 3 courses";
}
else{
Iterator i=selectedCourses.iterator();
while(i.hasNext()){
text=text+i.next()+"\n";
}
}
display.setText(text);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
GUIExample1 screen=new GUIExample1("Course Selection");
screen.setVisible(true);
}

}
jaguarlsl 2007-06-12
  • 打赏
  • 举报
回复
int cnt = 0;
for(int i=0;i<jpanel.getComponentCount();i++){
if(jpanel.getComponent(i) instanceof JCombobox){
if(((JCombobox)jpanel.getComponent(i)).isSelected())
cnt++;
}
}
airplane33 2007-06-11
  • 打赏
  • 举报
回复
再说仔细一点,最好弄点代码出来学习学习
yh10231033 2007-06-09
  • 打赏
  • 举报
回复
哈哈,最直接最看的懂的方法
yh10231033 2007-06-09
  • 打赏
  • 举报
回复
声明一个Vector<JCheckBox>变量jcbSelectedList,给每个你用到的JCheckBox都加上addActionListener()事件,然后每次不论点了哪个JCheckBox都做如下操作:
1.清空jcbSelectedList;
2.都把所有的JCheckBox判断一下,如果选中的,加入jcbSelectedList中
3.获得jcbSelectedList的size()值
yuyu622 2007-06-08
  • 打赏
  • 举报
回复
如何才能在装载JCheckBox的容器里循环每一个CheckBox呢?
帮人帮到底,送佛送到西嘛.先谢各位了...
jaguarlsl 2007-06-07
  • 打赏
  • 举报
回复
得到装载checkbox的容器,写个循环判断哪些checkbox被选中即可
Supernpc 2007-06-06
  • 打赏
  • 举报
回复
我晕死你。你不用数组就

if (jcheck1.selected())
i ++;

if (jcheck2.selected())
i ++;

..

怎么不知道变通呢。晕s
yuyu622 2007-06-06
  • 打赏
  • 举报
回复
能不能不用数组而实现呢,我是想要不用数组那种
Supernpc 2007-06-06
  • 打赏
  • 举报
回复
是的话,我就逛奔来接50分 ^^



import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class SimpleTest extends JFrame
{

/**
* @param args
*/
public SimpleTest()
{
final JCheckBox check[] = new JCheckBox[3];

for (int i = 0 ; i < check.length ; i ++)
{
check[i] = new JCheckBox("第" + (i+1) + "个");
}

JButton button = new JButton("确定");

setLayout(new FlowLayout());
for (int i = 0 ; i < check.length ; i ++)
{
add(check[i]);
}
add(button);

button.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
int index = 0;
for (int i = 0 ; i < check.length ; i ++)
{
if (check[i].isSelected())
index ++;
}
System.out.println("共选中了" + index + "个");
}

});

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200, 200, 400, 100);
setVisible(true);

}
public static void main(String[] args)
{
// TODO Auto-generated method stub
new SimpleTest();
}

}
Supernpc 2007-06-06
  • 打赏
  • 举报
回复
多个个JCheckBox项 是什么意思?

意思是一共选中了多少个吗?
yuyu622 2007-06-06
  • 打赏
  • 举报
回复
在线急等,问题解决立即给分

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧