ActionListener actionPerformed被调用两次

Yve*_*eth 0 java action onclicklistener

我有这个ActionListener:

logOutButton.addActionListener(new ActionListener() {

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

        }
    });
Run Code Online (Sandbox Code Playgroud)

在这里我将按钮添加到面板:

c.fill = GridBagConstraints.RELATIVE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.gridwidth = 1;
this.add(logOutButton, c);
Run Code Online (Sandbox Code Playgroud)

如果JTextField的内容是test,则控制台输出为:

test
test
Run Code Online (Sandbox Code Playgroud)

所以我认为actionPerformed()方法被调用两次,但为什么呢?

编辑

孔码:

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());

        }
    });
}
Run Code Online (Sandbox Code Playgroud)

Pre*_*ric 6

setUpJButton()在代码中调用两次,在相同的按钮上添加两次侦听器.