java 复选框问题 如何取出下面选中的复选框
JCheckBoxbox1=newJCheckBox();JCheckBoxbox2=newJCheckBox();JCheckBoxbox3=newJCheckBox(...
JCheckBox box1 = new JCheckBox();
JCheckBox box2 = new JCheckBox();
JCheckBox box3 = new JCheckBox();
JCheckBox box4 = new JCheckBox();
JCheckBox box5 = new JCheckBox();
JCheckBox box6 = new JCheckBox();box1.setText("读书");
box2.setText("电脑");
box3.setText("音乐");
box4.setText("电影");
box5.setText("游戏");
box6.setText("运动"); 展开
JCheckBox box2 = new JCheckBox();
JCheckBox box3 = new JCheckBox();
JCheckBox box4 = new JCheckBox();
JCheckBox box5 = new JCheckBox();
JCheckBox box6 = new JCheckBox();box1.setText("读书");
box2.setText("电脑");
box3.setText("音乐");
box4.setText("电影");
box5.setText("游戏");
box6.setText("运动"); 展开
展开全部
你可以用对象名.isSelected()判断是否被选中,然后用对象名.getText获取文本
给你改一下:
JCheckBox box[] = new JCheckBox[5];
String hobbies[] = new String []{"读书","电脑","音乐","电影","游戏","运动"};
String hobbyInfo = null ;
for(int index = 0;index < 5 ;index++){
box[index] = new JCheckBox();
box[index].setText(hobbies[index]);
}
for(JCheckBox ch :box){
if(ch.isSelected()){
hobbyInfo += ch.getText();
}
}
System.out.println(hobbyInfo);
给你改一下:
JCheckBox box[] = new JCheckBox[5];
String hobbies[] = new String []{"读书","电脑","音乐","电影","游戏","运动"};
String hobbyInfo = null ;
for(int index = 0;index < 5 ;index++){
box[index] = new JCheckBox();
box[index].setText(hobbies[index]);
}
for(JCheckBox ch :box){
if(ch.isSelected()){
hobbyInfo += ch.getText();
}
}
System.out.println(hobbyInfo);
展开全部
不知,下面程序合你的题意不?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class A2{
public static void main(String arg[]){JCheckBox box1 = new JCheckBox();
JFrame jf=new JFrame();
jf.setLayout(new FlowLayout());
ButtonGroup bg=new ButtonGroup();
JCheckBox box2 = new JCheckBox();
JCheckBox box3 = new JCheckBox();
JCheckBox box4 = new JCheckBox();
JCheckBox box5 = new JCheckBox();
JCheckBox box6 = new JCheckBox();
box1.setText("读书");
box2.setText("电脑");
box3.setText("音乐");
box4.setText("电影");
box5.setText("游戏");
box6.setText("运动");
bg.add(box1); bg.add(box2); bg.add(box3);
bg.add(box4); bg.add(box5); bg.add(box6);
jf.add(box1); jf.add(box2); jf.add(box3);
jf.add(box4); jf.add(box5); jf.add(box6);
box1.addItemListener(new it());
box2.addItemListener(new it());
box3.addItemListener(new it());
box4.addItemListener(new it());
box5.addItemListener(new it());
box6.addItemListener(new it());
jf.setSize(333,333);
jf.setVisible(true);
}
}
class it implements ItemListener
{
public void itemStateChanged(ItemEvent e) {
JCheckBox jc=(JCheckBox)e.getItem(); //getItem()反回被选中的按钮
String sr=jc.getText(); //反回被选中按钮的标签。
if(e.getStateChange()==ItemEvent.SELECTED) //getStateChange()反回按钮是被选中还是未选中。
{
System.out.println("按钮"+ "\""+sr +"\""+"选中");
}
if(e.getStateChange()==ItemEvent.DESELECTED){
System.out.println("按钮"+ "\""+sr +"\""+"未选中");
}
}}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class A2{
public static void main(String arg[]){JCheckBox box1 = new JCheckBox();
JFrame jf=new JFrame();
jf.setLayout(new FlowLayout());
ButtonGroup bg=new ButtonGroup();
JCheckBox box2 = new JCheckBox();
JCheckBox box3 = new JCheckBox();
JCheckBox box4 = new JCheckBox();
JCheckBox box5 = new JCheckBox();
JCheckBox box6 = new JCheckBox();
box1.setText("读书");
box2.setText("电脑");
box3.setText("音乐");
box4.setText("电影");
box5.setText("游戏");
box6.setText("运动");
bg.add(box1); bg.add(box2); bg.add(box3);
bg.add(box4); bg.add(box5); bg.add(box6);
jf.add(box1); jf.add(box2); jf.add(box3);
jf.add(box4); jf.add(box5); jf.add(box6);
box1.addItemListener(new it());
box2.addItemListener(new it());
box3.addItemListener(new it());
box4.addItemListener(new it());
box5.addItemListener(new it());
box6.addItemListener(new it());
jf.setSize(333,333);
jf.setVisible(true);
}
}
class it implements ItemListener
{
public void itemStateChanged(ItemEvent e) {
JCheckBox jc=(JCheckBox)e.getItem(); //getItem()反回被选中的按钮
String sr=jc.getText(); //反回被选中按钮的标签。
if(e.getStateChange()==ItemEvent.SELECTED) //getStateChange()反回按钮是被选中还是未选中。
{
System.out.println("按钮"+ "\""+sr +"\""+"选中");
}
if(e.getStateChange()==ItemEvent.DESELECTED){
System.out.println("按钮"+ "\""+sr +"\""+"未选中");
}
}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询