简体   繁体   English

ActionListener actionPerformed调用了两次

[英]ActionListener actionPerformed called twice

i have this ActionListener: 我有这个ActionListener:

logOutButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(getTextFieldContent());

        }
    });

and here i add the button to the panel: 在这里,我将按钮添加到面板:

c.fill = GridBagConstraints.RELATIVE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.gridwidth = 1;
this.add(logOutButton, c);

if the content of the JTextField is test, the console output is: 如果JTextField的内容为test,则控制台输出为:

test
test

so i think the actionPerformed() method is called twice but why? 所以我认为actionPerformed()方法被调用了两次,但是为什么呢?

Edit 编辑

the hole code: 孔代码:

public GuiPanel(){
    super();
    this.setBorder(new EmptyBorder(10, 10, 10, 10) );
    //Layout:
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    this.setLayout(gridbag);
    setUpJButton();

    //label for textfield
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(labelForName, c);
    this.add(labelForName);

    c.insets = new Insets(5, 0, 0, 0);//padding to top

    //textField
    c.fill = GridBagConstraints.HORIZONTAL;
    setUpTextField();
    c.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(textField, c);
    c.gridwidth = 1;//reset gridwidth
    this.add(textField);

    c.insets = new Insets(10, 0, 0, 0);//padding to top

    //anmelden Button
    c.fill = GridBagConstraints.RELATIVE;
    setUpJButton();
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.LAST_LINE_START;
    this.add(logInButton, c);

    c.insets = new Insets(10, 5, 0, 0);//padding to left

    //abmelden Button
    c.fill = GridBagConstraints.RELATIVE;
    c.anchor = GridBagConstraints.LAST_LINE_END;
    c.gridwidth = 1;
    this.add(logOutButton, c);
}

private void setUpJButton() {
    logInButton.setSize(50, 50);
    logInButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(getTextFieldContent());
        }
    });
    logOutButton.setSize(50, 50);
    logOutButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(getTextFieldContent());

        }
    });
}

您在代码中两次调用setUpJButton() ,从而在同一按钮上两次添加了侦听器。

Try printing out e.getActionCommand() . 尝试打印出e.getActionCommand() I'm guessing it's two distinct actions on the button. 我猜这是按钮上的两个不同的动作。

Since you instantiate ActionListener() instance using new operator in the addActionListener, it passes two different instance (since you call setUpJButton twice) and it (addActionListener) doesn't know that they are same request. 由于您在addActionListener中使用new operator instantiate ActionListener()实例,因此它传递了two different instance (因为您两次调用setUpJButton),并且它(addActionListener)并不知道它们是相同的请求。

If you sure don't want to extend ActionListener and use like this, then reference it to an member object and pass that to addActionListener. 如果您确定不想扩展ActionListener并像这样使用,则将其reference it to an member object and pass给addActionListener。 Something like this: 像这样:

ActionListener acLs=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
           System.out.println(getTextFieldContent());  //getTextFieldContent??
        }
    };

Then 然后

private void setUpJButton() {
    logInButton.setSize(50, 50);
    logInButton.addActionListener(acLs);
    logOutButton.setSize(50, 50);
    logOutButton.addActionListener(acLs);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM