0

当我单击 Swing 按钮时,我想获取所有选中的复选框。我尝试了很多方法,例如getSelectedObjects()and getStateChange()。此外,我试图打电话isSelected()ItemStateChanged. 最后,我尝试调用getSelectedObjects(),但这仅适用于一个复选框。

下面是我的代码。我应该如何进行?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.plaf.*;
import javax.swing.SwingUtilities.*;

class RegForm extends JFrame implements ItemListener,ActionListener
{
    //component initialize
    JLabel fn=new JLabel("First Name:");
    JLabel ln=new JLabel("Last Name:");
    JLabel ad=new JLabel("Address:");
    JLabel cn=new JLabel("Contact No:");
    JLabel sex=new JLabel("Sex:");
    JLabel sub=new JLabel("Subject:");

    JButton ok=new JButton("Ok");
    JButton can=new JButton("Cancel");

    JRadioButton m=new JRadioButton("Male");
    JRadioButton f=new JRadioButton("Female");
    JRadioButton r1=new JRadioButton("Metal");
    JRadioButton r2=new JRadioButton("Motif");
    JRadioButton r3=new JRadioButton("Window");

    ButtonGroup bglf=new ButtonGroup();

    JCheckBox cl=new JCheckBox("c");
    JCheckBox cpp=new JCheckBox("c++");
    JCheckBox java=new JCheckBox("JAVA");

    TextField tfn=new TextField(10);
    TextField tln=new TextField(10);
    TextArea tadd=new TextArea(10,3);
    TextField tcn=new TextField(10);


    ButtonGroup bg;
    TextArea s=new TextArea(20,5);

    Container c;
    RegForm()
    {
    c=this.getContentPane();
    c.setLayout(null);

    bg=new ButtonGroup();
    bg.add(m);
    bg.add(f);  

    bglf.add(r1);
    bglf.add(r2);
    bglf.add(r3);
    //set position
    fn.setBounds(50,50,100,30);
    ln.setBounds(50,100,100,30);
    ad.setBounds(50,150,100,30);
    cn.setBounds(50,300,100,30);
    sex.setBounds(50,350,100,30);
    sub.setBounds(50,400,100,30);

    tfn.setBounds(200,50,150,30);
    tln.setBounds(200,100,150,30);
    tadd.setBounds(200,150,150,100);
    tcn.setBounds(200,300,150,30);

    ok.setBounds(50,450,100,40);
    can.setBounds(200,450,100,40);

    cl.setBounds(200,400,100,30);
    cpp.setBounds(350,400,100,30);
    java.setBounds(500,400,100,30);

    m.setBounds(200,350,100,30);
    f.setBounds(350,350,100,30);

    s.setBounds(50,500,300,100);    

    r1.setBounds(50,10,100,30);
    r2.setBounds(150,10,100,30);
    r3.setBounds(250,10,100,30);
    //add to container
    c.add(r1);
    c.add(r2);
    c.add(r3);

    c.add(fn);
    c.add(ln);
    c.add(ad);
    c.add(cn);
    c.add(sex);
    c.add(sub);

    c.add(tfn);
    c.add(tln);
    c.add(tadd);
    c.add(tcn);

    c.add(ok);
    c.add(can);
    c.add(cl);
    c.add(cpp);
    c.add(java);

    c.add(m);
    c.add(f);

    c.add(s);   

    m.addItemListener(this);
    f.addItemListener(this);

    cl.addItemListener(this);
    cpp.addItemListener(this);
    java.addItemListener(this);

    ok.addActionListener(this);
    can.addActionListener(this);

    r1.addItemListener(this);
    r2.addItemListener(this);
    r3.addItemListener(this);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public void actionPerformed(ActionEvent ae)
    {
        s.setText(ae.getActionCommand());
        if(ae.getActionCommand().equals("Ok"))
        {
            s.setText(tfn.getText()+"\n"+tln.getText()+"\n"+tadd.getText()+"\n"+tcn.getText());
        }

        if(ae.getActionCommand().equals("Cancel"))
        {
            tfn.setText("");
            tln.setText("");
            tadd.setText("");
            tcn.setText("");
        }
    }
    //JRadioButton rb;
    //JCheckBox cb;
    JToggleButton tb;
    public void itemStateChanged(ItemEvent ie)
    {
        //rb=(JRadioButton)ie.getItem();    
        //cb=(JCheckBox)ie.getItem();
        tb=(JToggleButton)ie.getItem();
        s.setText(tb.getText());


         try
         {
            if(s.equals("Metal"))
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            else if(s.equals("Motif"))
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            else
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(c);
         }
            catch(Exception e)
            {
            }
    }
    public static void main(String ar[])
    {
        RegForm r=new RegForm();
        r.setSize(800,700);
        r.setVisible(true);
    }
}
4

0 回答 0