命令 运行 两次 Java actionperformed

Commands are running twice in Java actionperformed

我创建了一个实用程序,将在防火墙区域内使用,以使用 Java 和 Swing 获取 Websphere MQ 内容,因为我不确定缺陷在哪里,我已经发布了几乎整个代码除了冗余部分:

        package testbox;

        import java.awt.GridBagConstraints;
        import java.awt.GridBagLayout;
        import java.awt.GridLayout;
        import java.awt.Insets;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JOptionPane;
        import javax.swing.JPanel;
        import javax.swing.JPasswordField;
        import javax.swing.JScrollPane;
        import javax.swing.JTabbedPane;
        import javax.swing.JTextArea;
        import javax.swing.JTextField;
        import javax.swing.SwingUtilities;

        @SuppressWarnings("serial")
        public class MainFrame extends JFrame implements ActionListener
        {   
            JLabel lblqname = new JLabel("Please enter the queue name");    
            JTextField txtqname = new JTextField(25);                       
            JLabel lblqcur = new JLabel("where curdeth greater than");      
            JTextField txtqcurdfil = new JTextField(5);                     

            JLabel lblchlname = new JLabel("Please enter the Channel name");
            JTextField txtchlname = new JTextField(30);     
            JLabel lblchs = new JLabel("where status is: ");
            JTextField txtchs = new JTextField(8);      

            public String ID;
            public String pwdValue;

            public String qname;    
            public int cdepth;

            public String chlname;
            public String chlstatus;

            public String cmdissue;

            JTextArea out = new JTextArea();

            JButton QMGR1 = new JButton("QMGR1");
            JButton QMGR2 = new JButton("QMGR2");


            public MainFrame()
            {   
                JLabel jUserName = new JLabel("ID");
                JTextField userName = new JTextField();
                JLabel jPassword = new JLabel("Password");
                JTextField password = new JPasswordField();
                Object[] ob = {jUserName, userName, jPassword, password};
                int result = JOptionPane.showConfirmDialog(null, ob, "Please input password for Login", JOptionPane.OK_CANCEL_OPTION);

                if (result == JOptionPane.OK_OPTION) 
                {
                    ID = userName.getText(); 
                    pwdValue = password.getText(); 

                final JFrame frame = new JFrame("Environment Choice");
                frame.setSize(500, 400);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(new GridLayout(1, 1));
                JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

                tabbedPane.addTab("QAQmgrList", makeQAPanel());

                frame.getContentPane().add(tabbedPane);
                }

            }

            public JPanel makeQAPanel() 
            {
                JPanel p = new JPanel();
                p.setLayout(new GridBagLayout());

                GridBagConstraints gbc_QMGR1 = new GridBagConstraints();
                gbc_QMGR1.insets = new Insets(0, 0, 5, 5);
                gbc_QMGR1.gridx = 1;
                gbc_QMGR1.gridy = 1;
                p.add(QMGR1, gbc_QMGR1);                
                QMGR1.addActionListener(this);


                GridBagConstraints gbc_QMGR2 = new GridBagConstraints();
                gbc_QMGR2.insets = new Insets(0, 0, 5, 5);
                gbc_QMGR2.gridx = 1;
                gbc_QMGR2.gridy = 2;
                p.add(QMGR2, gbc_QMGR2);        
                QMGR2.addActionListener(this);


                return p;
            }

            public void createSubframe()
            {   
                final JFrame subframe = new JFrame("Object Choice");
                subframe.setSize(1000, 500);        
                subframe.getContentPane().setLayout(new GridLayout(1, 1));
                out.setText(null);      
                out.setLineWrap(true);
                out.setCaretPosition(out.getDocument().getLength());
                out.setEditable (false);
                JScrollPane jp = new  JScrollPane(out);
                jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

                JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

                JPanel queue = new JPanel();

                queue.add(lblqname);
                txtqname.setText(null);
                queue.add(txtqname);        
                queue.add(lblqcur);
                txtqcurdfil.setText(null);
                queue.add(txtqcurdfil);     

                txtqname.addActionListener(this);
                txtqcurdfil.addActionListener(this);

                JPanel chl = new JPanel();

                chl.add(lblchlname);
                txtchlname.setText(null);
                chl.add(txtchlname);
                chl.add(lblchs);
                txtchs.setText(null);
                chl.add(txtchs);

                txtchlname.addActionListener(this);
                txtchs.addActionListener(this);


                tabbedPane.addTab("Queues", queue);
                tabbedPane.addTab("Channels", chl);     
                subframe.getContentPane().add(tabbedPane);
                subframe.getContentPane().add(jp);
                tabbedPane.setVisible(true);
                subframe.setVisible(true);      
            }   

            public static void main(String[] args) 
            {SwingUtilities.invokeLater(new Runnable(){ public void run() { @SuppressWarnings("unused") MainFrame m = new MainFrame();}});}

            @Override
            public void actionPerformed(ActionEvent e) 
            {               
                if (e.getSource() == QMGR1|| e.getSource() == QMGR2) 
                {createSubframe();}     

                if (e.getSource() == txtqname){qname = txtqname.getText();}

                if (e.getSource() == txtqcurdfil)
                {   
                    cdepth = Integer.parseInt(txtqcurdfil.getText());       
                    cmdissue = "qn has value messages";
                    cmdissue = cmdissue.replace("qn", ""+qname+"");
                    cmdissue = cmdissue.replace("value", ""+cdepth+"");
                    System.out.println(cmdissue);
                    cmdissue = null;
                }

                if (e.getSource() == txtchlname){chlname = txtchlname.getText(); chlname=null;}

                if (e.getSource() == txtchs)
                {   
                    chlstatus = txtchs.getText();
                    cmdissue = "chln is  chls";
                    cmdissue = cmdissue.replace("chln", ""+chlname+"");
                    cmdissue = cmdissue.replace("chls", ""+chlstatus+"");
                    System.out.println(cmdissue);
                }                   
            }
        }

我得到了代码的预期结果: Running for first time

假设我关闭了这个对象选择面板并打开了一个新实例,无论我选择哪个选项我都执行了两次命令 运行s: Running for the second time

重复迭代。假设我第四次或第五次做出选择,命令 运行s 4/5 次。

我了解它以某种方式初始化对象的次数我 运行 它,并且需要在我关闭面板后重置它。但我不确定 how/where 能否完成此操作。

对于发布的冗长代码深表歉意,因为我想确保人们能够指出所犯的错误。

GridBagConstraints gbc_QMGR1 = new GridBagConstraints();
gbc_QMGR1.insets = new Insets(0, 0, 5, 5);
gbc_QMGR1.gridx = 1;
gbc_QMGR1.gridy = 1;
p.add(QMGR1, gbc_QMGR1);                
QMGR1.addActionListener(this);


GridBagConstraints gbc_QMGR1 = new GridBagConstraints();
gbc_QMGR1.insets = new Insets(0, 0, 5, 5);
gbc_QMGR1.gridx = 1;
gbc_QMGR1.gridy = 2;
p.add(QMGR1, gbc_QMGR1);        
QMGR1.addActionListener(this);

您似乎正试图在两个不同的网格位置将同一组件添加到面板两次。你不能这样做。

您需要:

  1. 创建两个不同的组件,或者
  2. 删除其中一个组件。

编辑:

        JButton QMGR1 = new JButton("QMGR1");
        JButton QMGR2 = new JButton("QMGR2");

您将按钮创建为实例变量。

然后在 makeQAPanel() 方法中将 actionListener 添加到按钮。

QMGR1.addActionListener(this);

...

QMGR2.addActionListener(this);

所以每次调用该方法时,都会再次添加 actionListener。

actionListener应该添加到你的构造函数中class所以它只添加一次。

@jesper,

有人向我解释了其中的构造函数部分:

打包测试箱;

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;

    @SuppressWarnings("serial")
    public class MainFrame extends JFrame implements ActionListener
    {   
        JLabel lblqname = new JLabel("Please enter the queue name");    
        JTextField txtqname = new JTextField(25);                       
        JLabel lblqcur = new JLabel("where curdeth greater than");      
        JTextField txtqcurdfil = new JTextField(5);                     

        JLabel lblchlname = new JLabel("Please enter the Channel name");
        JTextField txtchlname = new JTextField(30);     
        JLabel lblchs = new JLabel("where status is: ");
        JTextField txtchs = new JTextField(8);      

        public String ID;
        public String pwdValue;

        public String qname;    
        public int cdepth;

        public String chlname;
        public String chlstatus;

        public String cmdissue;

        JTextArea out = new JTextArea();

        JButton QMGR1 = new JButton("QMGR1");
        JButton QMGR2 = new JButton("QMGR2");


        public MainFrame()
        {   
            QMGR1.addActionListener(this);
            QMGR2.addActionListener(this);
            txtqname.addActionListener(this);
            txtqcurdfil.addActionListener(this);
            txtchlname.addActionListener(this);
            txtchs.addActionListener(this);


            JLabel jUserName = new JLabel("ID");
            JTextField userName = new JTextField();
            JLabel jPassword = new JLabel("Password");
            JTextField password = new JPasswordField();
            Object[] ob = {jUserName, userName, jPassword, password};
            int result = JOptionPane.showConfirmDialog(null, ob, "Please input password for Login", JOptionPane.OK_CANCEL_OPTION);

            if (result == JOptionPane.OK_OPTION) 
            {
                ID = userName.getText(); 
                pwdValue = password.getText(); 

            final JFrame frame = new JFrame("Environment Choice");
            frame.setSize(500, 400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(new GridLayout(1, 1));
            JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

            tabbedPane.addTab("QAQmgrList", makeQAPanel());

            frame.getContentPane().add(tabbedPane);
            }

        }

        public JPanel makeQAPanel() 
        {
            JPanel p = new JPanel();
            p.setLayout(new GridBagLayout());

            GridBagConstraints gbc_QMGR1 = new GridBagConstraints();
            gbc_QMGR1.insets = new Insets(0, 0, 5, 5);
            gbc_QMGR1.gridx = 1;
            gbc_QMGR1.gridy = 1;
            p.add(QMGR1, gbc_QMGR1);                


            GridBagConstraints gbc_QMGR2 = new GridBagConstraints();
            gbc_QMGR2.insets = new Insets(0, 0, 5, 5);
            gbc_QMGR2.gridx = 1;
            gbc_QMGR2.gridy = 2;
            p.add(QMGR2, gbc_QMGR2);        


            return p;
        }

        public void createSubframe()
        {   
            final JFrame subframe = new JFrame("Object Choice");
            subframe.setSize(1000, 500);        
            subframe.getContentPane().setLayout(new GridLayout(1, 1));
            out.setText(null);      
            out.setLineWrap(true);
            out.setCaretPosition(out.getDocument().getLength());
            out.setEditable (false);
            JScrollPane jp = new  JScrollPane(out);
            jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

            JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

            JPanel queue = new JPanel();

            queue.add(lblqname);
            txtqname.setText(null);
            queue.add(txtqname);        
            queue.add(lblqcur);
            txtqcurdfil.setText(null);
            queue.add(txtqcurdfil);     


            JPanel chl = new JPanel();

            chl.add(lblchlname);
            txtchlname.setText(null);
            chl.add(txtchlname);
            chl.add(lblchs);
            txtchs.setText(null);
            chl.add(txtchs);



            tabbedPane.addTab("Queues", queue);
            tabbedPane.addTab("Channels", chl);     
            subframe.getContentPane().add(tabbedPane);
            subframe.getContentPane().add(jp);
            tabbedPane.setVisible(true);
            subframe.setVisible(true);      
        }   

        public static void main(String[] args) 
        {SwingUtilities.invokeLater(new Runnable(){ public void run() { @SuppressWarnings("unused") MainFrame m = new MainFrame();}});}

        @Override
        public void actionPerformed(ActionEvent e) 
        {               
            if (e.getSource() == QMGR1|| e.getSource() == QMGR2) 
            {createSubframe();}     

            if (e.getSource() == txtqname){qname = txtqname.getText();}

            if (e.getSource() == txtqcurdfil)
            {   
                cdepth = Integer.parseInt(txtqcurdfil.getText());       
                cmdissue = "qn has value messages";
                cmdissue = cmdissue.replace("qn", ""+qname+"");
                cmdissue = cmdissue.replace("value", ""+cdepth+"");
                System.out.println(cmdissue);
                cmdissue = null;
            }

            if (e.getSource() == txtchlname){chlname = txtchlname.getText(); chlname=null;}

            if (e.getSource() == txtchs)
            {   
                chlstatus = txtchs.getText();
                cmdissue = "chln is  chls";
                cmdissue = cmdissue.replace("chln", ""+chlname+"");
                cmdissue = cmdissue.replace("chls", ""+chlstatus+"");
                System.out.println(cmdissue);
            }                   
        }
    }

工作得很好..感谢您指出