see my problem start form this piece of code i add all the addActionListener for the button but when it come to the Radio button it use addItemListenet but i implements ActionListener only how i will implements ItemListener so i can set Law when ever the user Select sw form the radio button and click on add item~ it will add the item to the right array i made before

     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

full code

   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
             } 

        }
     }     
}
有帮助吗?

解决方案

I'm not sure what array you want to fill but get the text with 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
}

If any of the inputs are numbers and you want the numerical value, you need to parse.

Also, these need to be declared as class memebers. You have them declared in the constructor

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

Declared in the constructor, they are not within the scope of the listener class

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){

Also, doesn't really look like you need a listener for the radio button, since an event is not necessary. The JButton listens for an event, and in the actionPerformed, it checks if the radio button is selected. Therefore no need for the radio button to listen for any event, the JButton does that.

其他提示

Try following code. I ahve added a List item and adding values from swTextField TextFiled to item when user select rButton1 and click on addButton button

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);
    }
}
  • Use ButtonGroup with the JRadioButton of your context.
  • Use jRadioButton.setActionCommand(String) to set their corresponding action name: for your context "SW Version" and anything such.
  • Make use of an ArrayList to add the item of your context. Try mapping each such array list using a HashMap<Key, Val> i.e., HashMap<String, ArrayList<Equipment>> where the "SW Version" or anything such name will be the key
  • Try adding listeners to each action button in-line using the means of anonymous class.

So a sample coding for add action would become depicting the usage(usefulness) of ButtonGroup:

addButton.addActionListener(new ActionListener() {

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

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

Tutorial and reference:

  1. How to use Radio Button, check the demo for ButtonGroup
  2. ButtonGroup class
  3. HashMap
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top