返回调用方法或获取 JCheckBox 选择的结果

发布于 2024-12-29 07:55:49 字数 3547 浏览 1 评论 0原文

这就是问题所在.. 这是我想做的事情的简化版本:

public class Class{
    public void main(){
       Vector<Boolean> boo=new Vector<Boolean>;
       System.out.println("Hi all");
       ArrayList<String> a=new ArrayList<String>()
       a.add("hi");
       a.add("all");
       JRadioButtonExample b=new JRadioButtonExample(2,a);
       boo=b.getCheck();
       for(Boolean b:boo){
         System.out.println(b);
       }
    }
}

我必须为 GUI 调用外部类.. 问题是我无法设法将 main 中的 system.out.println 与 JRadioButtonExample 中执行的操作同步。

JRadioButtonExample 类如下:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Vector;

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


public class JRadiobuttonExample extends JFrame {

static JCheckBox b[]; 
static Vector<Boolean> check=new Vector<Boolean>();
JButton altervista=new JButton("RUN");
JButton selectall=new JButton("select all");
JButton deselectall=new JButton("deselect all");
static int num;
int i=0;

public static JCheckBox[] getB() {
    return b;
}
public void setB(JCheckBox[] b2) {
    b = b2;
}
public Vector<Boolean> getCheck() {
    return check;
}
public void setCheck(Vector<Boolean> check2) {
    check = check2;
}
public JRadiobuttonExample(int num, ArrayList<String> lbl) {

    super("JRadiobuttonExample");

    getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));

    b= new JCheckBox[num];

    for(i=0; i<num; i++) {
        //creo i bottoni
        b[i] = new JCheckBox(lbl.get(i));
        getContentPane().add(b[i]);
    }

    //adding buttons
    getContentPane().add(selectall);
    getContentPane().add(deselectall);
    getContentPane().add(altervista);

    //adding listeners
    AscoltatoreSel asc1=new AscoltatoreSel();
    selectall.addActionListener(asc1);
    setVisible(true);

    AscoltatoreDesel asc2=new AscoltatoreDesel();
    deselectall.addActionListener(asc2);
    setVisible(true);

    Ascoltatore asc=new Ascoltatore();
    altervista.addActionListener(asc);
    setVisible(true);

    this.pack();
}

class Ascoltatore extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==altervista){
            setVisible(false);
            boh(b);
        }
    }
}

class AscoltatoreSel extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==selectall){
            for(i=0; i<num; i++) {
                b[i].setSelected(true);
                setVisible(true);
            }
        }
    }
}

class AscoltatoreDesel extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==deselectall){
            for(i=0; i<num; i++) {
                b[i].setSelected(false);
            }
        }
    }
}


public static void boh(JCheckBox[] b){
    JCheckBox[] buttons=getB();

    for (JCheckBox c:buttons){
        check.add(c.isSelected());
    }

}

}

提前致谢!

ps 如果选择所有复选框我需要得到 boo=[true;true]

that's the problem..
This is a simplified version of what i would like do:

public class Class{
    public void main(){
       Vector<Boolean> boo=new Vector<Boolean>;
       System.out.println("Hi all");
       ArrayList<String> a=new ArrayList<String>()
       a.add("hi");
       a.add("all");
       JRadioButtonExample b=new JRadioButtonExample(2,a);
       boo=b.getCheck();
       for(Boolean b:boo){
         System.out.println(b);
       }
    }
}

I must call an external class for the GUI..
The problem is that i can't manage to syncronize the system.out.println in the main with the actionperformed in the JRadioButtonExample.

The JRadioButtonExample class is as follows:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Vector;

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


public class JRadiobuttonExample extends JFrame {

static JCheckBox b[]; 
static Vector<Boolean> check=new Vector<Boolean>();
JButton altervista=new JButton("RUN");
JButton selectall=new JButton("select all");
JButton deselectall=new JButton("deselect all");
static int num;
int i=0;

public static JCheckBox[] getB() {
    return b;
}
public void setB(JCheckBox[] b2) {
    b = b2;
}
public Vector<Boolean> getCheck() {
    return check;
}
public void setCheck(Vector<Boolean> check2) {
    check = check2;
}
public JRadiobuttonExample(int num, ArrayList<String> lbl) {

    super("JRadiobuttonExample");

    getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));

    b= new JCheckBox[num];

    for(i=0; i<num; i++) {
        //creo i bottoni
        b[i] = new JCheckBox(lbl.get(i));
        getContentPane().add(b[i]);
    }

    //adding buttons
    getContentPane().add(selectall);
    getContentPane().add(deselectall);
    getContentPane().add(altervista);

    //adding listeners
    AscoltatoreSel asc1=new AscoltatoreSel();
    selectall.addActionListener(asc1);
    setVisible(true);

    AscoltatoreDesel asc2=new AscoltatoreDesel();
    deselectall.addActionListener(asc2);
    setVisible(true);

    Ascoltatore asc=new Ascoltatore();
    altervista.addActionListener(asc);
    setVisible(true);

    this.pack();
}

class Ascoltatore extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==altervista){
            setVisible(false);
            boh(b);
        }
    }
}

class AscoltatoreSel extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==selectall){
            for(i=0; i<num; i++) {
                b[i].setSelected(true);
                setVisible(true);
            }
        }
    }
}

class AscoltatoreDesel extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==deselectall){
            for(i=0; i<num; i++) {
                b[i].setSelected(false);
            }
        }
    }
}


public static void boh(JCheckBox[] b){
    JCheckBox[] buttons=getB();

    for (JCheckBox c:buttons){
        check.add(c.isSelected());
    }

}

}

Thanks in advance!

p.s. if all checkboxes are selected i need to get boo=[true;true]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2025-01-05 07:55:49

JRadioButtonExampleObservable,您的 ClassObserver

JRadioButtonExample 中您应该保留当该对象的状态发生变化时您想要通知的观察者列表。您实现如下方法,notifyObservers() 来通知所有注册的观察者。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observer;
import java.util.Vector;

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

public class JRadioButtonExample extends JFrame   {

    static JCheckBox b[];
    static Vector<Boolean> check = new Vector<Boolean>();
    JButton altervista = new JButton("RUN");
    JButton selectall = new JButton("select all");
    JButton deselectall = new JButton("deselect all");
    static int num;

    private ArrayList<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();

    public static JCheckBox[] getB() {
        return b;
    }

    public void setB(JCheckBox[] b2) {
        b = b2;
    }

    public Vector<Boolean> getCheck() {
        return check;
    }

    public void setCheck(Vector<Boolean> check2) {
        check = check2;
    }

    public JRadioButtonExample(int num, ArrayList<String> lbl) {

        super("JRadioButtonExample");

        getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));

        b = new JCheckBox[num];

        for (int i = 0; i < num; i++) {
            // creo i bottoni
            b[i] = new JCheckBox(lbl.get(i));
            getContentPane().add(b[i]);
        }

        // adding buttons
        getContentPane().add(selectall);
        getContentPane().add(deselectall);
        getContentPane().add(altervista);

        // adding listeners
        AscoltatoreSel asc1 = new AscoltatoreSel();
        selectall.addActionListener(asc1);
        setVisible(true);

        AscoltatoreDesel asc2 = new AscoltatoreDesel();
        deselectall.addActionListener(asc2);
        setVisible(true);

        Ascoltatore asc = new Ascoltatore();
        altervista.addActionListener(asc);
        setVisible(true);

        this.pack();
    }
    public void addPropertyChangeListener(PropertyChangeListener listener){
        this.listeners.add(listener);
    }
    public void notifyObservers(){
        for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
            PropertyChangeListener name = (PropertyChangeListener) iterator
                    .next();
            name.propertyChange(null);

        }
    }

    class Ascoltatore extends WindowAdapter implements ActionListener {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == altervista) {
                setVisible(false);
                boh(b);
            }
            notifyObservers();
        }
    }

    class AscoltatoreSel extends WindowAdapter implements ActionListener {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == selectall) {
                for (int i = 0; i < num; i++) {
                    b[i].setSelected(true);
                    setVisible(true);
                }
            }
            notifyObservers();
        }
    }

    class AscoltatoreDesel extends WindowAdapter implements ActionListener {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == deselectall) {
                for (int i = 0; i < num; i++) {
                    b[i].setSelected(false);
                }
            }
            notifyObservers();
        }
    }

    public static void boh(JCheckBox[] b) {
        JCheckBox[] buttons = getB();

        for (JCheckBox c : buttons) {
            check.add(c.isSelected());
        }

    }
}

您的Class应该实现PropertyChangeListener,并且应该将自身注册为JRadioButtonExample的监听器。
并实现 propertyChange(..) 方法,这就是您想要 print() 的地方。

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

import java.util.Vector;

public class Class implements PropertyChangeListener{
    private JRadioButtonExample b;

    public Class(JRadioButtonExample b){
        this.b = b;
        b.addPropertyChangeListener(this);
    }

    public static void main(String[] args){
        ArrayList<String> a = new ArrayList<String>();
        a.add("hi");
        a.add("all");
        JRadioButtonExample myButton = new JRadioButtonExample(2,a);
        Class myClass = new Class(myButton);         
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        Vector<Boolean> boo = b.getCheck();
         for(Boolean bool : boo){
               System.out.println(bool);
         }      
    }
}

JRadioButtonExample is Observable, your Class is an Observer

In the JRadioButtonExample you should keep a list of the Observer's which you want to notify when this object's state changes. You implement a method like below, notifyObservers() to notify all the registered observers.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observer;
import java.util.Vector;

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

public class JRadioButtonExample extends JFrame   {

    static JCheckBox b[];
    static Vector<Boolean> check = new Vector<Boolean>();
    JButton altervista = new JButton("RUN");
    JButton selectall = new JButton("select all");
    JButton deselectall = new JButton("deselect all");
    static int num;

    private ArrayList<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();

    public static JCheckBox[] getB() {
        return b;
    }

    public void setB(JCheckBox[] b2) {
        b = b2;
    }

    public Vector<Boolean> getCheck() {
        return check;
    }

    public void setCheck(Vector<Boolean> check2) {
        check = check2;
    }

    public JRadioButtonExample(int num, ArrayList<String> lbl) {

        super("JRadioButtonExample");

        getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));

        b = new JCheckBox[num];

        for (int i = 0; i < num; i++) {
            // creo i bottoni
            b[i] = new JCheckBox(lbl.get(i));
            getContentPane().add(b[i]);
        }

        // adding buttons
        getContentPane().add(selectall);
        getContentPane().add(deselectall);
        getContentPane().add(altervista);

        // adding listeners
        AscoltatoreSel asc1 = new AscoltatoreSel();
        selectall.addActionListener(asc1);
        setVisible(true);

        AscoltatoreDesel asc2 = new AscoltatoreDesel();
        deselectall.addActionListener(asc2);
        setVisible(true);

        Ascoltatore asc = new Ascoltatore();
        altervista.addActionListener(asc);
        setVisible(true);

        this.pack();
    }
    public void addPropertyChangeListener(PropertyChangeListener listener){
        this.listeners.add(listener);
    }
    public void notifyObservers(){
        for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
            PropertyChangeListener name = (PropertyChangeListener) iterator
                    .next();
            name.propertyChange(null);

        }
    }

    class Ascoltatore extends WindowAdapter implements ActionListener {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == altervista) {
                setVisible(false);
                boh(b);
            }
            notifyObservers();
        }
    }

    class AscoltatoreSel extends WindowAdapter implements ActionListener {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == selectall) {
                for (int i = 0; i < num; i++) {
                    b[i].setSelected(true);
                    setVisible(true);
                }
            }
            notifyObservers();
        }
    }

    class AscoltatoreDesel extends WindowAdapter implements ActionListener {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == deselectall) {
                for (int i = 0; i < num; i++) {
                    b[i].setSelected(false);
                }
            }
            notifyObservers();
        }
    }

    public static void boh(JCheckBox[] b) {
        JCheckBox[] buttons = getB();

        for (JCheckBox c : buttons) {
            check.add(c.isSelected());
        }

    }
}

Your Class should implement PropertyChangeListener, and and should register itself as al listener to the JRadioButtonExample.
And implement propertyChange(..) method, that's where you want to print().

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

import java.util.Vector;

public class Class implements PropertyChangeListener{
    private JRadioButtonExample b;

    public Class(JRadioButtonExample b){
        this.b = b;
        b.addPropertyChangeListener(this);
    }

    public static void main(String[] args){
        ArrayList<String> a = new ArrayList<String>();
        a.add("hi");
        a.add("all");
        JRadioButtonExample myButton = new JRadioButtonExample(2,a);
        Class myClass = new Class(myButton);         
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        Vector<Boolean> boo = b.getCheck();
         for(Boolean bool : boo){
               System.out.println(bool);
         }      
    }
}
我三岁 2025-01-05 07:55:49

您需要在数组中的每个复选框上注册 ActionListeners ,以便在单击它们时执行某些操作。像这样:

// In your constructor:
  public JRadiobuttonExample(int num, ArrayList<String> lbl) {
    // ...
    for(int i=0; i<num; i++) {
      b[i] = new JCheckBox(lbl.get(i));
      // Add this line below:
      b[i].addActionListener(createCheckboxListener());
      getContentPane().add(b[i]);
    }
    // ...
  }

  // Then add this method 
  /** Do something when a checkbox is checked */
  private ActionListener createCheckboxListener() {
    return new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JCheckBox) {
          JCheckBox source = (JCheckBox)e.getSource();
          System.out.println("You clicked on: " + source.getText());
        }
      }
    };
  }

此外,我会查看您的代码中是否有任何编译错误。当我尝试运行您的示例代码时,它会给我错误,例如未声明的变量等。

这是完整的工作示例:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Vector;

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

public class JRadiobuttonExample extends JFrame {

  static JCheckBox b[];
  static Vector<Boolean> check=new Vector<Boolean>();
  JButton altervista=new JButton("RUN");
  JButton selectall=new JButton("select all");
  JButton deselectall=new JButton("deselect all");
  static int num;




  public static JCheckBox[] getB() {
    return b;
  }
  public void setB(JCheckBox[] b2) {
    b = b2;
  }
  public Vector<Boolean> getCheck() {
    return check;
  }
  public void setCheck(Vector<Boolean> check2) {
    check = check2;
  }
  public JRadiobuttonExample(int num, ArrayList<String> lbl) {

    super("JRadiobuttonExample");

    getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));

    b= new JCheckBox[num];

    for(int i=0; i<num; i++) {
      //creo i bottoni
      b[i] = new JCheckBox(lbl.get(i));
      b[i].addActionListener(createCheckboxListener());
      getContentPane().add(b[i]);
    }

    //adding buttons
    getContentPane().add(selectall);
    getContentPane().add(deselectall);
    getContentPane().add(altervista);

    //adding listeners
    AscoltatoreSel asc1=new AscoltatoreSel();
    selectall.addActionListener(asc1);
    setVisible(true);

    AscoltatoreDesel asc2=new AscoltatoreDesel();
    deselectall.addActionListener(asc2);
    setVisible(true);

    Ascoltatore asc=new Ascoltatore();
    altervista.addActionListener(asc);
    setVisible(true);

    this.pack();
  }

  /** Do something when a checkbox is checked */
  private ActionListener createCheckboxListener() {
    return new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JCheckBox) {
          JCheckBox source = (JCheckBox)e.getSource();
          System.out.println("You clicked on: " + source.getText());
        }
      }
    };
  }

  class Ascoltatore extends WindowAdapter implements ActionListener{
    @Override
    public void windowClosing(WindowEvent e){
      System.exit(0);
    }
    @Override
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==altervista){
        setVisible(false);
        System.out.println(b);
      }
    }
  }

  class AscoltatoreSel extends WindowAdapter implements ActionListener{
    @Override
    public void windowClosing(WindowEvent e){
      System.exit(0);
    }
    @Override
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==selectall){
        for(int i=0; i<num; i++) {
          b[i].setSelected(true);
          setVisible(true);
        }
      }
    }
  }

  class AscoltatoreDesel extends WindowAdapter implements ActionListener{
    @Override
    public void windowClosing(WindowEvent e){
      System.exit(0);
    }
    @Override
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==deselectall){
        for(int i=0; i<num; i++) {
          b[i].setSelected(false);
        }
      }
    }
  }


  public static void main(String []args){
    System.out.println("Hi all");
    ArrayList<String> a=new ArrayList<String>();
    a.add("hi");
    a.add("all");
    JRadiobuttonExample b=new JRadiobuttonExample(2,a);
    Vector<Boolean> boo=b.getCheck();
    for(Boolean b2:boo){
      System.out.println(b2);
    }
  }

}

You need to register ActionListeners on each of the checkboxes in the array in order to do something when they are clicked. Like so:

// In your constructor:
  public JRadiobuttonExample(int num, ArrayList<String> lbl) {
    // ...
    for(int i=0; i<num; i++) {
      b[i] = new JCheckBox(lbl.get(i));
      // Add this line below:
      b[i].addActionListener(createCheckboxListener());
      getContentPane().add(b[i]);
    }
    // ...
  }

  // Then add this method 
  /** Do something when a checkbox is checked */
  private ActionListener createCheckboxListener() {
    return new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JCheckBox) {
          JCheckBox source = (JCheckBox)e.getSource();
          System.out.println("You clicked on: " + source.getText());
        }
      }
    };
  }

Additionally, I would look to see if you had any compilation errors in your code. When I tried to run your sample code, it would give me errors such as undeclared variables, etc.

Here's the full working example:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Vector;

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

public class JRadiobuttonExample extends JFrame {

  static JCheckBox b[];
  static Vector<Boolean> check=new Vector<Boolean>();
  JButton altervista=new JButton("RUN");
  JButton selectall=new JButton("select all");
  JButton deselectall=new JButton("deselect all");
  static int num;




  public static JCheckBox[] getB() {
    return b;
  }
  public void setB(JCheckBox[] b2) {
    b = b2;
  }
  public Vector<Boolean> getCheck() {
    return check;
  }
  public void setCheck(Vector<Boolean> check2) {
    check = check2;
  }
  public JRadiobuttonExample(int num, ArrayList<String> lbl) {

    super("JRadiobuttonExample");

    getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));

    b= new JCheckBox[num];

    for(int i=0; i<num; i++) {
      //creo i bottoni
      b[i] = new JCheckBox(lbl.get(i));
      b[i].addActionListener(createCheckboxListener());
      getContentPane().add(b[i]);
    }

    //adding buttons
    getContentPane().add(selectall);
    getContentPane().add(deselectall);
    getContentPane().add(altervista);

    //adding listeners
    AscoltatoreSel asc1=new AscoltatoreSel();
    selectall.addActionListener(asc1);
    setVisible(true);

    AscoltatoreDesel asc2=new AscoltatoreDesel();
    deselectall.addActionListener(asc2);
    setVisible(true);

    Ascoltatore asc=new Ascoltatore();
    altervista.addActionListener(asc);
    setVisible(true);

    this.pack();
  }

  /** Do something when a checkbox is checked */
  private ActionListener createCheckboxListener() {
    return new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JCheckBox) {
          JCheckBox source = (JCheckBox)e.getSource();
          System.out.println("You clicked on: " + source.getText());
        }
      }
    };
  }

  class Ascoltatore extends WindowAdapter implements ActionListener{
    @Override
    public void windowClosing(WindowEvent e){
      System.exit(0);
    }
    @Override
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==altervista){
        setVisible(false);
        System.out.println(b);
      }
    }
  }

  class AscoltatoreSel extends WindowAdapter implements ActionListener{
    @Override
    public void windowClosing(WindowEvent e){
      System.exit(0);
    }
    @Override
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==selectall){
        for(int i=0; i<num; i++) {
          b[i].setSelected(true);
          setVisible(true);
        }
      }
    }
  }

  class AscoltatoreDesel extends WindowAdapter implements ActionListener{
    @Override
    public void windowClosing(WindowEvent e){
      System.exit(0);
    }
    @Override
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==deselectall){
        for(int i=0; i<num; i++) {
          b[i].setSelected(false);
        }
      }
    }
  }


  public static void main(String []args){
    System.out.println("Hi all");
    ArrayList<String> a=new ArrayList<String>();
    a.add("hi");
    a.add("all");
    JRadiobuttonExample b=new JRadiobuttonExample(2,a);
    Vector<Boolean> boo=b.getCheck();
    for(Boolean b2:boo){
      System.out.println(b2);
    }
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文