如何根据按钮选择JRadioButton

时间:2013-12-08 18:39:39

标签: java swing event-handling actionlistener jradiobutton

看到我的问题从这段代码开始,我为按钮添加了所有addActionListener 但当它来到单选按钮时它使用addItemListenet,但我只实现了ActionListener我将如何实现ItemListener所以我可以设置Law当用户选择sw形成单选按钮并单击添加项目〜它会将项目添加到右侧我之前制作的数组

     exitButton.addActionListener(new ButtonWatcher());
     addButton.addActionListener(new ButtonWatcher());
     copyButton.addActionListener(new ButtonWatcher());
     showButton.addActionListener(new ButtonWatcher());
     rButton.addItemListenet(new ButtonWatcher());

    }

     private class ButtonWatcher implements ActionListener{

         public void actionPerformed(ActionEvent a){
             Object buttonPressed=a.getSource();
             if(buttonPressed.equals(exitButton))
             {
             System.exit(0);
             }

             if(buttonPressed.equals(addButton) && rButton1.isSelected())
             {

                 //do the action
             } 

enter image description here

完整代码

   package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author isslam
 */
public class MyFrameMain extends JFrame{
    Equipment newq = new Equipment();
    private final JLabel iLabel;
    private final JLabel nLabel;
    private final JTextField iJTextField;
    private final JTextField nJTextField;
    private final JTextField swTextField;
    private final JTextField hwTextField;
    private final JLabel jItemCounter;
    private final JTextArea reSoulte;
    private final JButton addButton;
    private final JButton showButton;
    private final JButton copyButton;
    private final JButton exitButton;

    public MyFrameMain(String title){
    setSize(500, 500);
    setTitle(title);
    setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);

    iJTextField = new JTextField();
    nJTextField = new JTextField();
    swTextField = new JTextField();
    hwTextField = new JTextField();
    nLabel = new JLabel("ID: ");
    iLabel = new JLabel("Name: ");
    jItemCounter = new JLabel("Number of current Item");

    reSoulte = new JTextArea(15,20);
    reSoulte.setEditable(false);
    reSoulte.setText("Array is empty");

    addButton = new  JButton("Add an item into the Array");
    showButton = new JButton("Show all items in the Array");
    copyButton = new JButton("Copy Array into File");
    exitButton = new JButton("Exite");


    JRadioButton rButton1 = new JRadioButton("SW Version",false);
    JRadioButton rButton2 = new JRadioButton("HW Type",false);
    JRadioButton rButton3 = new JRadioButton("General",true);    

     ButtonGroup BGroup = new ButtonGroup();
     BGroup.add(rButton1);
     BGroup.add(rButton2);
     BGroup.add(rButton3);

     JPanel rbPanel = new JPanel(new GridLayout(5,1));
     rbPanel.add(nLabel);
     rbPanel.add(iLabel);
     rbPanel.add(rButton1);
     rbPanel.add(rButton2);
     rbPanel.add(rButton3);

     JPanel bpanel = new JPanel(new GridLayout(2,2));
     bpanel.add(addButton);
     bpanel.add(showButton);
     bpanel.add(copyButton);
     bpanel.add(exitButton);

     JPanel jtfPanel = new JPanel(new GridLayout(5,1));
        jtfPanel.add(iJTextField);
        jtfPanel.add(nJTextField);
        jtfPanel.add(swTextField);
        jtfPanel.add(hwTextField);
        jtfPanel.add(jItemCounter);

     JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(rbPanel, BorderLayout.WEST);
        topPanel.add(jtfPanel, BorderLayout.CENTER);

     JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(bpanel, BorderLayout.SOUTH);
        mainPanel.add(reSoulte, BorderLayout.CENTER);
        mainPanel.add(topPanel, BorderLayout.NORTH);


     Container pane = getContentPane();
     pane.add(mainPanel);


     exitButton.addActionListener(new ButtonWatcher());
     addButton.addActionListener(new ButtonWatcher());
     copyButton.addActionListener(new ButtonWatcher());
     showButton.addActionListener(new ButtonWatcher());
    //rButton.addItemListenet(new ButtonWatcher());

    }

     private class ButtonWatcher implements ActionListener{

         public void actionPerformed(ActionEvent a){
             Object buttonPressed=a.getSource();
             if(buttonPressed.equals(exitButton))
             {
             System.exit(0);
             }

             if(buttonPressed.equals(addButton) && rButton1.isSelected())
             {

                 //do the action
             } 

        }
     }     
}

3 个答案:

答案 0 :(得分:0)

我不确定您要填充哪个数组但是使用getText()

获取文本
if(buttonPressed.equals(addButton) && rButton1.isSelected())
{
    String s1 = iJTextField.getText();
    String s2 = nJTextField.getText();
    String s3 = swTextField.getText();
    String s4 = hwTextField.getText();

    // something with these strings
}

如果任何输入是数字并且您想要数值,则需要解析。

此外,这些需要被声明为班级成员。你在构造函数

中声明了它们
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true); 

在构造函数中声明,它们不在侦听器类的范围内

public class MyFrameMain extends JFrame{
    private final JLabel iLabel;
    private final JLabel nLabel;
    private final JTextField iJTextField;
    private final JTextField nJTextField;
    private final JTextField swTextField;
    private final JTextField hwTextField;
    private final JLabel jItemCounter;
    private final JTextArea reSoulte;
    private final JButton addButton;
    private final JButton showButton;
    private final JButton copyButton;
    private final JButton exitButton;
    JRadioButton rButton1 = new JRadioButton("SW Version",false);
    JRadioButton rButton2 = new JRadioButton("HW Type",false);
    JRadioButton rButton3 = new JRadioButton("General",true); 

    public MyFrameMain(String title){

此外,看起来你不需要收音机按钮的监听器,因为不需要事件。 JButton侦听一个事件,并在actionPerformed中检查是否选中了单选按钮。因此,JButton不需要单选按钮来监听任何事件。

答案 1 :(得分:0)

尝试以下代码。我添加了List item,并在用户选择swTextField并点击rButton1按钮

时,将addButton文字字段中的值添加到项目
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

/**
 *
 * @author isslam
 */
public class Test extends JFrame {

    private final JLabel iLabel;
    private final JLabel nLabel;
    private final JTextField iJTextField;
    private final JTextField nJTextField;
    private final JTextField swTextField;
    private final JTextField hwTextField;
    private final JLabel jItemCounter;
    private final JTextArea reSoulte;
    private final JButton addButton;
    private final JButton showButton;
    private final JButton copyButton;
    private final JButton exitButton;
    JRadioButton rButton1;
    java.util.List<String> item = new ArrayList<String>();

    public Test(String title) {
        setSize(500, 500);
        setTitle(title);
        setDefaultCloseOperation(Test.EXIT_ON_CLOSE);

        iJTextField = new JTextField();
        nJTextField = new JTextField();
        swTextField = new JTextField();
        hwTextField = new JTextField();
        nLabel = new JLabel("ID: ");
        iLabel = new JLabel("Name: ");
        jItemCounter = new JLabel("Number of current Item");

        reSoulte = new JTextArea(15, 20);
        reSoulte.setEditable(false);
        reSoulte.setText("Array is empty");

        addButton = new JButton("Add an item into the Array");
        showButton = new JButton("Show all items in the Array");
        copyButton = new JButton("Copy Array into File");
        exitButton = new JButton("Exite");


        rButton1 = new JRadioButton("SW Version", false);
        JRadioButton rButton2 = new JRadioButton("HW Type", false);
        JRadioButton rButton3 = new JRadioButton("General", true);

        ButtonGroup BGroup = new ButtonGroup();
        BGroup.add(rButton1);
        BGroup.add(rButton2);
        BGroup.add(rButton3);

        JPanel rbPanel = new JPanel(new GridLayout(5, 1));
        rbPanel.add(nLabel);
        rbPanel.add(iLabel);
        rbPanel.add(rButton1);
        rbPanel.add(rButton2);
        rbPanel.add(rButton3);

        JPanel bpanel = new JPanel(new GridLayout(2, 2));
        bpanel.add(addButton);
        bpanel.add(showButton);
        bpanel.add(copyButton);
        bpanel.add(exitButton);

        JPanel jtfPanel = new JPanel(new GridLayout(5, 1));
        jtfPanel.add(iJTextField);
        jtfPanel.add(nJTextField);
        jtfPanel.add(swTextField);
        jtfPanel.add(hwTextField);
        jtfPanel.add(jItemCounter);

        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(rbPanel, BorderLayout.WEST);
        topPanel.add(jtfPanel, BorderLayout.CENTER);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(bpanel, BorderLayout.SOUTH);
        mainPanel.add(reSoulte, BorderLayout.CENTER);
        mainPanel.add(topPanel, BorderLayout.NORTH);


        Container pane = getContentPane();
        pane.add(mainPanel);


        exitButton.addActionListener(new ButtonWatcher());
        addButton.addActionListener(new ButtonWatcher());
        copyButton.addActionListener(new ButtonWatcher());
        showButton.addActionListener(new ButtonWatcher());
        //rButton.addItemListenet(new ButtonWatcher());

    }

    private class ButtonWatcher implements ActionListener {

        public void actionPerformed(ActionEvent a) {
            Object buttonPressed = a.getSource();
            if (buttonPressed.equals(exitButton)) {
                System.exit(0);
            }

            if (buttonPressed.equals(addButton) && rButton1.isSelected()) {

                item.add(swTextField.getText());
                System.out.println(item);

            }

        }
    }

    public static void main(String args[]) {
        Test t = new Test("Test");
        t.setVisible(true);
    }
}

答案 2 :(得分:0)

  • ButtonGroup与您的上下文JRadioButton一起使用。
  • 使用jRadioButton.setActionCommand(String)设置相应的操作名称:适用于您的上下文"SW Version"等等。
  • 使用ArrayList添加上下文的项目。尝试使用HashMap<Key, Val> i.e., HashMap<String, ArrayList<Equipment>>映射每个此类数组列表,其中"SW Version"或任何此类名称将是关键
  • 尝试使用匿名类的方式在线添加每个操作按钮的监听器。

因此,添加操作的示例编码将描述ButtonGroup的用法(有用性):

addButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                 String actionCommand = buttonGroup1.getSelection()
                                                         .getActionCommand();

                  // actionCommand = "SW Version"
                 map.get(actionCmmand).add(equipment);      
              }
        });

教程和参考:

  1. How to use Radio Button,查看ButtonGroup
  2. 的演示
  3. ButtonGroup class
  4. HashMap