kehren Sie zur aufrufenden Methode zurück oder rufen Sie die Ergebnisse der Auswahl einer JCheckBox ab

StackOverflow https://stackoverflow.com/questions/9015624

Frage

das ist das Problem..Dies ist eine vereinfachte Version dessen, was ich gerne tun würde:

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);
       }
    }
}

Ich muss eine externe Klasse für die GUI aufrufen..Das Problem ist, dass ich es nicht schaffe, das System zu synchronisieren.aus.drucken Sie hauptsächlich mit der Aktion, die im JRadioButtonExample ausgeführt wurde.

Die JRadioButtonExample-Klasse lautet wie folgt:

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());
    }

}

}

Danke im Voraus!

ps.wenn alle Kontrollkästchen aktiviert sind, muss ich boo=[true;true]

War es hilfreich?

Lösung

JRadioButtonExample is Observable, Ihrer Class ist ein Observer

In der JRadioButtonExample Sie sollten eine Liste der Beobachter führen, die Sie benachrichtigen möchten, wenn sich der Status dieses Objekts ändert.Sie implementieren eine Methode wie die folgende, notifyObservers() um alle registrierten Beobachter zu benachrichtigen.

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());
        }

    }
}

Ihrer Class sollte implementieren PropertyChangeListener, und und sollte sich als al Listener für die registrieren JRadioButtonExample.Und implementieren propertyChange(..) methode, dort möchten Sie drucken().

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);
         }      
    }
}

Andere Tipps

Sie müssen sich registrieren ActionListeners aktivieren Sie jedes der Kontrollkästchen im Array, um etwas zu tun, wenn Sie darauf klicken.Wie 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());
        }
      }
    };
  }

Außerdem würde ich nachsehen, ob Sie Kompilierungsfehler in Ihrem Code hatten.Wenn ich versuchte, Ihren Beispielcode auszuführen, wurden mir Fehler wie nicht deklarierte Variablen usw. angezeigt.

Hier ist das vollständige Arbeitsbeispiel:

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);
    }
  }

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top