Checkbox cb1,cb2,cb3,cb4,cb5,cb6 6个复选框;
cb1=new Checkbox("数据库");
cb2=new Checkbox("数学");
cb3=new Checkbox("信息系统");
cb4=new Checkbox("操作系统");
cb5=new Checkbox("数据结构");
cb6=new Checkbox("C语言");
我想添加监听 如果被选上 那就把里面的字符串取出来,怎么做
每个cb 复选框都得加监听 加什么监听???
我的意思是 如果界面上 选择了 cb1 , cb2. 那我就用两个字符串变量 string 1 = “数据库”
string2 = “数学”;
java GUI 复选框 如何设置监听
答案:2 悬赏:80 手机版
解决时间 2021-03-19 11:59
- 提问者网友:沉默菋噵
- 2021-03-18 18:41
最佳答案
- 五星知识达人网友:天凉才是好个秋
- 2021-03-18 19:11
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
final class CheckBoxDemo extends JFrame {
public String getTitle() {return "CheckBoxDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
//把选中的内容放进 List
final private java.util.List<String> stringList=new ArrayList<String>(5);
private class CheckAction extends AbstractAction{
private CheckAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();
if (obj instanceof JToggleButton){
JToggleButton t = (JToggleButton)obj;
if(t.isSelected())
stringList.add(t.getText());
else
stringList.remove(t.getText());
}
//打印效果
for (String s:stringList) {
System.out.print(s + "\t");
}
System.out.println();
}
}
CheckBoxDemo() throws HeadlessException {
init();
attachListeners();
doLay();
}
private JCheckBox[] buttons=new JCheckBox[4];
private void init(){
for (int i = 0,j=buttons.length;i < j; i++) {
buttons[i]=new JCheckBox(new CheckAction(Integer.toString(i)));
}
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
for (JCheckBox button : buttons) {
container.add(button);
}
pack();
setVisible(true);
}
public static void main(String...args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new CheckBoxDemo();
}
}
);
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
final class CheckBoxDemo extends JFrame {
public String getTitle() {return "CheckBoxDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
//把选中的内容放进 List
final private java.util.List<String> stringList=new ArrayList<String>(5);
private class CheckAction extends AbstractAction{
private CheckAction(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();
if (obj instanceof JToggleButton){
JToggleButton t = (JToggleButton)obj;
if(t.isSelected())
stringList.add(t.getText());
else
stringList.remove(t.getText());
}
//打印效果
for (String s:stringList) {
System.out.print(s + "\t");
}
System.out.println();
}
}
CheckBoxDemo() throws HeadlessException {
init();
attachListeners();
doLay();
}
private JCheckBox[] buttons=new JCheckBox[4];
private void init(){
for (int i = 0,j=buttons.length;i < j; i++) {
buttons[i]=new JCheckBox(new CheckAction(Integer.toString(i)));
}
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
for (JCheckBox button : buttons) {
container.add(button);
}
pack();
setVisible(true);
}
public static void main(String...args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new CheckBoxDemo();
}
}
);
}
}
全部回答
- 1楼网友:北方的南先生
- 2021-03-18 20:46
你是要做数学计算吗?复选框是checkbox,那是无法录入的,他只能对你的原形和正方形做选择;你要的应该是文本框input text,输入你要计算的数据,核心东西就是你的数学公式算法,按这个思路我想你能想到。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯