【问题标题】:How to call actionPerformed() within actionPerfomed()如何在 actionPerfomed() 中调用 actionPerformed()
【发布时间】:2012-11-10 11:36:30
【问题描述】:

下面的代码有问题。

添加信息菜单中有三个菜单项(客户、商品和员工)。单击它们(使用addActionListener)应显示各种文本字段/单选按钮/组合框(需要填写信息)和一个提交按钮。

提交所需信息并单击提交按钮后,它应将信息打印到弹出窗口。

我被困在最后一点,它应该再次调用actionPerformed 方法并将值打印到弹出窗口。有人可以帮忙吗?

EDITED##我已经编辑了我的代码。我的问题从第 1 行开始。 216到线号。 225. 当我点击Customer menuitem的按钮“submit3”时,弹出窗口出现但不显示包含“txt1”内容的字符串的内容。如何将组件的值传递给 actionPerformed,以便它可以在新的弹出窗口中打印它们?

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Retail extends JFrame implements ActionListener 
{
/**
 * 
 */
private static final long serialVersionUID = 1L;
JMenuBar menuBar = new JMenuBar();
JMenu addmenu = new JMenu("Add Information");
JMenu delmenu = new JMenu("Delete Information");
JMenu savemenu = new JMenu("Save Information");
JMenuItem emp = new JMenuItem("Employee");
JMenuItem merc = new JMenuItem("Merchandise");
JMenuItem cust = new JMenuItem("Customer");
Container contentPane = getContentPane();
JPanel p2 = new JPanel();

public Retail()
{
    super();
    contentPane.setLayout(new BorderLayout());
    JPanel p1 = new JPanel();
    p1.setBorder(new TitledBorder("Select Menu"));
    p1.setPreferredSize(new Dimension(500, 100));
    contentPane.add(p1,BorderLayout.NORTH);

    p2.setBorder(new TitledBorder("Entry Screen"));
    p2.setPreferredSize(new Dimension(500,500));    
    contentPane.add(p2,BorderLayout.CENTER);
    p2.setLayout(new BorderLayout());

    p1.add(menuBar);
    menuBar.add(addmenu);
    menuBar.add(delmenu);
    menuBar.add(savemenu);
    addmenu.add(emp);
    addmenu.addSeparator();
    addmenu.add(merc);
    addmenu.addSeparator();
    addmenu.add(cust);
    addmenu.addSeparator();


    emp.addActionListener(this);
    merc.addActionListener(this);
    cust.addActionListener(this);

}
public void actionPerformed(ActionEvent e)
{
    JButton submit1 = new JButton("Submit Employee Information"); 
    JButton submit2 = new JButton("Submit Merchandise Information");
    JButton submit3 = new JButton("Submit Customer Information");
    if(e.getSource() == emp)
    {   
        p2.removeAll();
        p2.updateUI();
        String[] states={"MA","AZ","CA"};
        JLabel lb1 = new JLabel("First Name:");
        JTextField txt1 = new JTextField(12);
        JLabel lb2 = new JLabel("Last Name:");
        JTextField txt2 = new JTextField(12);
        JLabel lb3 = new JLabel("Address:");
        JTextField txt3 = new JTextField(12);
        JLabel lb4 = new JLabel("City:");
        JTextField txt4 = new JTextField(12);
        JLabel lb5 = new JLabel("State");
        JComboBox cb1 = new JComboBox(states);
        JLabel lb6 = new JLabel("ZipCode");
        JTextField txt5 = new JTextField(12);
        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(8,1));
        JPanel p4 = new JPanel();
        p4.setLayout(new GridLayout(1,2));
        JLabel lb7= new JLabel("Gender:");
        JRadioButton rb1 = new JRadioButton("Male");
        JRadioButton rb2 = new JRadioButton("Female");
        ButtonGroup bgroup = new ButtonGroup();
        bgroup.add(rb1);
        bgroup.add(rb2);
        JLabel lb8 = new JLabel("Submit Information:");
        JPanel p5 = new JPanel();
        p5.setLayout(new GridLayout(8,1));
        p3.add(lb1);
        p3.add(lb2);
        p3.add(lb3);
        p3.add(lb4);
        p3.add(lb5);
        p3.add(lb6);
        p3.add(lb7);
        p3.add(lb8);

        p5.add(txt1);
        p5.add(txt2);
        p5.add(txt3);
        p5.add(txt4);
        p4.add(rb1);
        p4.add(rb2);
        p5.add(cb1);
        p5.add(txt5);
        p5.add(p4);
        p5.add(submit1);

        p2.add(p3,BorderLayout.WEST);
        p2.add(p5,BorderLayout.EAST);

        submit1.addActionListener(this);
    }

    if(e.getSource() == merc)
    {
        p2.removeAll();
        p2.updateUI();
        String[] states={"MA","AZ","CA"};
        JLabel lb1 = new JLabel("First Name:");
        JTextField txt1 = new JTextField(12);
        JLabel lb2 = new JLabel("Last Name:");
        JTextField txt2 = new JTextField(12);
        JLabel lb3 = new JLabel("Address:");
        JTextField txt3 = new JTextField(12);
        JLabel lb4 = new JLabel("City:");
        JTextField txt4 = new JTextField(12);
        JLabel lb5 = new JLabel("State");
        JComboBox cb1 = new JComboBox(states);
        JLabel lb6 = new JLabel("ZipCode");
        JTextField txt5 = new JTextField(12);
        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(8,1));
        JPanel p4 = new JPanel();
        p4.setLayout(new GridLayout(1,2));
        JLabel lb7= new JLabel("Gender");
        JRadioButton rb1 = new JRadioButton("Male");
        JRadioButton rb2 = new JRadioButton("Female");
        JLabel lb8 = new JLabel("Submit Information:");

        JPanel p5 = new JPanel();
        p5.setLayout(new GridLayout(8,1));
        p3.add(lb1);
        p3.add(lb2);
        p3.add(lb3);
        p3.add(lb4);
        p3.add(lb5);
        p3.add(lb6);
        p3.add(lb7);
        p3.add(lb8);
        p5.add(txt1);
        p5.add(txt2);
        p5.add(txt3);
        p5.add(txt4);
        p4.add(rb1);
        p4.add(rb2);
        p5.add(cb1);
        p5.add(txt5);
        p5.add(p4);
        p5.add(submit2);

        p2.add(p3,BorderLayout.WEST);
        p2.add(p5,BorderLayout.EAST);

        submit2.addActionListener(this);

    }
    if(e.getSource() == cust)
    {
        p2.removeAll();
        p2.updateUI();
        String[] states={"MA","AZ","CA"};
        JLabel lb1 = new JLabel("First Name:");
        JTextField txt1 = new JTextField(12);
        JLabel lb2 = new JLabel("Last Name:");
        JTextField txt2 = new JTextField(12);
        JLabel lb3 = new JLabel("Address:");
        JTextField txt3 = new JTextField(12);
        JLabel lb4 = new JLabel("City:");
        JTextField txt4 = new JTextField(12);
        JLabel lb5 = new JLabel("State");
        JComboBox cb1 = new JComboBox(states);
        JLabel lb6 = new JLabel("ZipCode");
        JTextField txt5 = new JTextField(12);
        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(8,1));
        JPanel p4 = new JPanel();
        p4.setLayout(new GridLayout(1,2));
        JLabel lb7= new JLabel("Gender");
        JRadioButton rb1 = new JRadioButton("Male");
        JRadioButton rb2 = new JRadioButton("Female");
        JLabel lb8 = new JLabel("Submit Information:");
        JPanel p5 = new JPanel();
        p5.setLayout(new GridLayout(8,1));
        p3.add(lb1);
        p3.add(lb2);
        p3.add(lb3);
        p3.add(lb4);
        p3.add(lb5);
        p3.add(lb6);
        p3.add(lb7);
        p3.add(lb8);

        p5.add(txt1);
        p5.add(txt2);
        p5.add(txt3);
        p5.add(txt4);
        p4.add(rb1);
        p4.add(rb2);
        p5.add(cb1);
        p5.add(txt5);
        p5.add(p4);
        p5.add(submit3);

        p2.add(p3,BorderLayout.WEST);
        p2.add(p5,BorderLayout.EAST);
        final String s;
        s = txt1.getText();
        submit3.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent args0) 
            {
                JOptionPane.showMessageDialog(rootPane,s);
            }
        });
    }
}
public static void main(String[] args)
{

    Retail frame = new Retail();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Retail Information");
    frame.pack();
    frame.setResizable(true);
    frame.setVisible(true);
}


}

【问题讨论】:

    标签: java swing dialog popup action


    【解决方案1】:

    最后一次测试:

    if(e.getSource()==submit1)
    

    永远不会成功,因为submit1 是您刚刚在actionPerformed 开头构造的JButton,因此不能成为当前事件的源。

    我建议您使用CardLayout 代替p2,而不是像这样构建新的布局组件,然后在您的操作处理程序中翻转到适当的卡片。这样您就可以为所有按钮注册一次侦听器,并且您将正确收到所有事件的通知。

    此外,您应该为每个 UI 组件注册单独的 ActionListeners,而不是使用一个巨大的 actionPerformed 来测试源代码。这使逻辑(和代码)更加清晰。

    编辑

    例如,而不是这个:

    emp.addActionListener(this);
    merc.addActionListener(this);
    cust.addActionListener(this);
    

    你可以这样做:

    emp.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // logic for click on emp button
        }
    });
    merc.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // logic for click on merc button
        }
    });
    // etc.
    

    那么你的主类就不需要implements ActionListener了。

    然后,如果您将CardLayout 用于p2,则可以在程序开始时将动作侦听器附加到每个界面元素。然后,响应任何特定操作的逻辑变得更加简单——只需更新适当的 UI 元素并切换要在p2 中显示的“卡片”。有关最后一部分的更多信息,请参阅the docs for CardLayout

    【讨论】:

    • 如何使用不同的 ActionListener?你能给我一个例子吗?我是这个主题的新手(你可以从我的代码中看出)。
    • 但是我所有需要在运行时显示的组件都在 actionperformed 方法中定义,因为当单击菜单项时,面板 p2 上会显示一组特定的组件。这些组件不能在主类构造函数中访问,因为它们是在 actionperformed.. 中本地定义的。
    • @user1844024 - 我建议您将这些项目的定义移出actionPerformed。没有充分的理由让所有 UI 构建代码都使用该方法。通过使用 CardLayout,您可以定义所有这些组件,但在任何时候只能看到一些组件。将 UI 代码移到 actionPerformed 外部有几个好处:它将对事件做出反应的逻辑与构建 UI 的代码分开;它将简化动作监听器;它可以让你消除笨拙的switch 语句;它将更加模块化和可维护。
    • @TedHopp 嗨,先生。是否可以从方法 actionPerformed() 调用方法?例如:- 我想调用一个 void 方法 add() 并将 3 个变量传递给它add(int a, int b, int c),然后获取这些变量并执行我想要的过程在 add() 方法中。提前致谢。
    • @TedHopp 先生,我破解了它。不管怎么说,多谢拉。 :)
    【解决方案2】:

    您可以调用适当的下一个菜单项的 .doClick()

    定义从适当的 actionPerformed() 方法调用的单独方法(例如 doEmpAction() 和 doCustAction() ),并从另一个调用它们。所以 doCustAction 只需调用 doEmpAction。

    【讨论】:

    • 我建议不要调用 doClick... 你应该在按钮、菜单等后面有 Action 对象,它们不知道(或关心)哪些 UI 元素触发了它们。
    • @sjr: doClick() Action 配合使用效果很好,如herehere 所示。
    • @sjr:我已经编辑了我的代码,你能检查一下并帮助我解决新问题吗..??
    【解决方案3】:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class Retail extends JFrame implements ActionListener {
    
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        JMenuBar menuBar = new JMenuBar();
        JMenu addmenu = new JMenu("Add Information");
        JMenu delmenu = new JMenu("Delete Information");
        JMenu savemenu = new JMenu("Save Information");
        JMenuItem emp = new JMenuItem("Employee");
        JMenuItem merc = new JMenuItem("Merchandise");
        JMenuItem cust = new JMenuItem("Customer");
        Container contentPane = getContentPane();
        JPanel p2 = new JPanel();
    
        public Retail() {
            super();
            contentPane.setLayout(new BorderLayout());
            JPanel p1 = new JPanel();
            p1.setBorder(new TitledBorder("Select Menu"));
            p1.setPreferredSize(new Dimension(500, 100));
            contentPane.add(p1, BorderLayout.NORTH);
    
            p2.setBorder(new TitledBorder("Entry Screen"));
            p2.setPreferredSize(new Dimension(500, 500));
            contentPane.add(p2, BorderLayout.CENTER);
            p2.setLayout(new BorderLayout());
    
            p1.add(menuBar);
            menuBar.add(addmenu);
            menuBar.add(delmenu);
            menuBar.add(savemenu);
            addmenu.add(emp);
            addmenu.addSeparator();
            addmenu.add(merc);
            addmenu.addSeparator();
            addmenu.add(cust);
            addmenu.addSeparator();
    
    
            emp.addActionListener(this);
            merc.addActionListener(this);
            cust.addActionListener(this);
    
        }
    
        public void actionPerformed(ActionEvent e) {
            JButton submit1 = new JButton("Submit Employee Information");
            JButton submit2 = new JButton("Submit Merchandise Information");
            JButton submit3 = new JButton("Submit Customer Information");
            if (e.getSource() == emp) {
                p2.removeAll();
                p2.updateUI();
                String[] states = {"MA", "AZ", "CA"};
                JLabel lb1 = new JLabel("First Name:");
                JTextField txt1 = new JTextField(12);
                JLabel lb2 = new JLabel("Last Name:");
                JTextField txt2 = new JTextField(12);
                JLabel lb3 = new JLabel("Address:");
                JTextField txt3 = new JTextField(12);
                JLabel lb4 = new JLabel("City:");
                JTextField txt4 = new JTextField(12);
                JLabel lb5 = new JLabel("State");
                JComboBox cb1 = new JComboBox(states);
                JLabel lb6 = new JLabel("ZipCode");
                JTextField txt5 = new JTextField(12);
                JPanel p3 = new JPanel();
                p3.setLayout(new GridLayout(8, 1));
                JPanel p4 = new JPanel();
                p4.setLayout(new GridLayout(1, 2));
                JLabel lb7 = new JLabel("Gender:");
                JRadioButton rb1 = new JRadioButton("Male");
                JRadioButton rb2 = new JRadioButton("Female");
                ButtonGroup bgroup = new ButtonGroup();
                bgroup.add(rb1);
                bgroup.add(rb2);
                JLabel lb8 = new JLabel("Submit Information:");
                JPanel p5 = new JPanel();
                p5.setLayout(new GridLayout(8, 1));
                p3.add(lb1);
                p3.add(lb2);
                p3.add(lb3);
                p3.add(lb4);
                p3.add(lb5);
                p3.add(lb6);
                p3.add(lb7);
                p3.add(lb8);
    
                p5.add(txt1);
                p5.add(txt2);
                p5.add(txt3);
                p5.add(txt4);
                p4.add(rb1);
                p4.add(rb2);
                p5.add(cb1);
                p5.add(txt5);
                p5.add(p4);
                p5.add(submit1);
    
                p2.add(p3, BorderLayout.WEST);
                p2.add(p5, BorderLayout.EAST);
    
    
                submit1.addActionListener(this);//instead of this line use next line to add actionlistener.
    
            }
            if (e.getSource() == merc) {
                p2.removeAll();
                p2.updateUI();
                String[] states = {"MA", "AZ", "CA"};
                JLabel lb1 = new JLabel("First Name:");
                JTextField txt1 = new JTextField(12);
                JLabel lb2 = new JLabel("Last Name:");
                JTextField txt2 = new JTextField(12);
                JLabel lb3 = new JLabel("Address:");
                JTextField txt3 = new JTextField(12);
                JLabel lb4 = new JLabel("City:");
                JTextField txt4 = new JTextField(12);
                JLabel lb5 = new JLabel("State");
                JComboBox cb1 = new JComboBox(states);
                JLabel lb6 = new JLabel("ZipCode");
                JTextField txt5 = new JTextField(12);
                JPanel p3 = new JPanel();
                p3.setLayout(new GridLayout(8, 1));
                JPanel p4 = new JPanel();
                p4.setLayout(new GridLayout(1, 2));
                JLabel lb7 = new JLabel("Gender");
                JRadioButton rb1 = new JRadioButton("Male");
                JRadioButton rb2 = new JRadioButton("Female");
                JLabel lb8 = new JLabel("Submit Information:");
    
                JPanel p5 = new JPanel();
                p5.setLayout(new GridLayout(8, 1));
                p3.add(lb1);
                p3.add(lb2);
                p3.add(lb3);
                p3.add(lb4);
                p3.add(lb5);
                p3.add(lb6);
                p3.add(lb7);
                p3.add(lb8);
    
                p5.add(txt1);
                p5.add(txt2);
                p5.add(txt3);
                p5.add(txt4);
                p4.add(rb1);
                p4.add(rb2);
                p5.add(cb1);
                p5.add(txt5);
                p5.add(p4);
                p5.add(submit2);
    
                p2.add(p3, BorderLayout.WEST);
                p2.add(p5, BorderLayout.EAST);
    
                submit2.addActionListener(this);//instead of this line use next line to add actionlistener.
    
            }
            if (e.getSource() == cust) {
                p2.removeAll();
                p2.updateUI();
                String[] states = {"MA", "AZ", "CA"};
                JLabel lb1 = new JLabel("First Name:");
                final JTextField txt1 = new JTextField(12);
                JLabel lb2 = new JLabel("Last Name:");
                JTextField txt2 = new JTextField(12);
                JLabel lb3 = new JLabel("Address:");
                JTextField txt3 = new JTextField(12);
                JLabel lb4 = new JLabel("City:");
                JTextField txt4 = new JTextField(12);
                JLabel lb5 = new JLabel("State");
                JComboBox cb1 = new JComboBox(states);
                JLabel lb6 = new JLabel("ZipCode");
                JTextField txt5 = new JTextField(12);
                JPanel p3 = new JPanel();
                p3.setLayout(new GridLayout(8, 1));
                JPanel p4 = new JPanel();
                p4.setLayout(new GridLayout(1, 2));
                JLabel lb7 = new JLabel("Gender");
                JRadioButton rb1 = new JRadioButton("Male");
                JRadioButton rb2 = new JRadioButton("Female");
                JLabel lb8 = new JLabel("Submit Information:");
                JPanel p5 = new JPanel();
                p5.setLayout(new GridLayout(8, 1));
                p3.add(lb1);
                p3.add(lb2);
                p3.add(lb3);
                p3.add(lb4);
                p3.add(lb5);
                p3.add(lb6);
                p3.add(lb7);
                p3.add(lb8);
    
                p5.add(txt1);
                p5.add(txt2);
                p5.add(txt3);
                p5.add(txt4);
                p4.add(rb1);
                p4.add(rb2);
                p5.add(cb1);
                p5.add(txt5);
                p5.add(p4);
                p5.add(submit3);
    
                p2.add(p3, BorderLayout.WEST);
                p2.add(p5, BorderLayout.EAST);
    
                //submit3.addActionListener(this);//instead of this line use next line to add actionlistener.Commment this line.
                submit3.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("------>" + txt1.getText());
                        //JOptionPane.showMessageDialog(rootPane, txt1.getText());
                        new MyDialog(txt1.getText());
                    }
                });
                // submit3.addActionListener(new SubmitActionListener(txt1.getText()));//out action listener
    
            }
            if (e.getSource() == submit1) {
                JOptionPane.showMessageDialog(rootPane, " button is clicked");
    
            }
    
        }
    
        class MyDialog extends JDialog {
    
            public MyDialog(String textbox1) {
                JLabel label = new JLabel(textbox1);
                add(label);
                setModalityType(ModalityType.APPLICATION_MODAL);
    
                setTitle("Info");
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                setLocationRelativeTo(null);
                setSize(300, 200);
                setVisible(true);
    
            }
        }
    
        public static void main(String[] args) {
    
            Retail frame = new Retail();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Retail Information");
            frame.pack();
            frame.setResizable(true);
            frame.setVisible(true);
        }
    }
    

    这适用于 submit3 按钮。根据需要修改其他按钮。

    【讨论】:

    • 如何从组件传递值? (我想重申一下,我对这个主题相当陌生..)如果您不想编写代码,可以用文字向我解释。谢谢:-)
    • 在 SubmitListener 的构造函数中将它们作为参数传递。所以你可以在 actionPerformed 方法中使用提交的值。希望我解释清楚。
    • 我可以简单地调用构造函数而不是在 addactionlistener 方法中调用它吗?所以在调用 SubmitActionListener 构造函数之前,我不必添加 actionlistener 到 submit1 我可以在接收的构造函数中添加监听器组件..好吗?
    • 我已经编辑了我的代码,请您检查并帮助我解决新问题..??
    • 我已经编辑了我的代码。它适用于 submit3 按钮。干杯。