使用弹出窗口将项添加到arraylist

时间:2013-12-07 04:45:18

标签: java netbeans arraylist

我正在尝试使用弹出式面板输入信息以添加到我的arraylist。我找到了这段代码

    import javax.swing.*;

public class JOptionPaneMultiInput {

    public static void main(String[] args) {
        JTextField xField = new JTextField(15);
        JTextField yField = new JTextField(15);
        JTextField zField = new JTextField(15);

        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("Item Name:"));
        myPanel.add(xField);

        myPanel.add(Box.createVerticalStrut(15)); // a spacer
        myPanel.add(new JLabel("Number in inventory:"));
        myPanel.add(yField);

        myPanel.add(Box.createVerticalStrut(15)); // a spacer
        myPanel.add(new JLabel("Unit Price:"));
        myPanel.add(zField);

        int result = JOptionPane.showConfirmDialog(null, myPanel,
                "Please Enter data into all boxes", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {

        }

    }
}

它可以自己运行并显示我需要显示的内容,但是如何让它与我的actionListner一起使用?我希望当用户点击添加按钮时弹出它。

我也不确定如何将此信息输入到我之前构建的存储的arraylist中,但如果需要,我可以将其作为单独的问题。

2 个答案:

答案 0 :(得分:1)

尝试,

JButton but = new JButton("Popup");
but.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 final JPanel myPanel = new PopupPanel();// Create a separate class extends JPanel
     int result = JOptionPane.showConfirmDialog(null, myPanel,
          "Please Enter data into both boxes", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {

            }
        }
    });

答案 1 :(得分:0)

通过创建一个单独的类并在动作侦听器中调用该方法,我能够通过动作侦听器弹出窗口。这个类看起来像这样:

public class JOptionPaneMultiInput {


    public static void main(String[] args) {

        buildWindow();

    }

    public static void buildWindow() {
        JTextField xField = new JTextField(15);
        JTextField yField = new JTextField(15);
        JTextField zField = new JTextField(15);

        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("Item Name:"));
        myPanel.add(xField);

        myPanel.add(Box.createVerticalStrut(15)); // a spacer
        myPanel.add(new JLabel("Number in inventory:"));
        myPanel.add(yField);

        myPanel.add(Box.createVerticalStrut(15)); // a spacer
        myPanel.add(new JLabel("Unit Price:"));
        myPanel.add(zField);
        int result = JOptionPane.showConfirmDialog(null, myPanel,
                "Please Enter data into all boxes", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {

            String newItemName = String.valueOf(xField);
            String newInventoryNumber = String.valueOf(yField);
            int newNumber = Integer.parseInt(newInventoryNumber);
            String newUnitPrice = String.valueOf(zField);
            double newPrice = Double.parseDouble(newUnitPrice);


        }

    }
}

我的动作监听器看起来像这样:

 addButton.addActionListener(
            new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e
                ) {
                    JOptionPaneMultiInput.buildWindow();


                }

            }
    );