命令在Java actionperformed中运行两次

时间:2017-06-22 08:26:12

标签: java swing actionlistener

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

b

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

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

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

我理解了这样一个事实:它以某种方式初始化对象的次数我运行它,并且需要在关闭面板后重置它。但我不确定如何/在何处完成这项工作。

对于冗长的代码发布道歉,因为我想确保人们可以指出已经犯下的错误。

2 个答案:

答案 0 :(得分:1)

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. 摆脱其中一个组件。
  3. 编辑:

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

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

    但是在makeQAPanel()方法中,您将actionListener添加到按钮。

    QMGR1.addActionListener(this);
    
    ...
    
    QMGR2.addActionListener(this);
    

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

    应该在你的类的构造函数中添加actionListener,这样它只会添加一次。

答案 1 :(得分:0)

@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);
            }                   
        }
    }

工作就像一个魅力..指出它的感谢