简体   繁体   中英

I can't handle a JButton click event using actionPerformed() method, don't enter in the method, why?

I am pretty new in Java Swing and I have some problem trying to handle the click event on a button by the use pf the ActionPerformed method, like in the documentation: http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#abstractbutton

So I have this LoginFrame class:

package com.test.login;

import javax.swing.JButton;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu.Separator;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import net.miginfocom.swt.MigLayout;

import org.jdesktop.application.SingleFrameApplication;

public class LoginFrame extends SingleFrameApplication {

    private static final int FIXED_WIDTH = 550;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);

    private boolean loginResult = true;

    /*
    public static void main(String[] args) {
        System.out.println("DENTRO: LoginFrame() ---> main()");
        launch(LoginFrame.class, args);
    }
    */

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        System.out.println("Inside LoginFrame ---> startup()");


        JFrame loginFrame = this.getMainFrame();            // main JFrame that represents the Windows
        loginFrame.setTitle("XCloud Login");

        loginFrame.setPreferredSize(INITAL_SIZE);
        loginFrame.setResizable(false);

        Container mainContainer = loginFrame.getContentPane();      // main Container into the main JFrame


        // JPanel creation and settings of the MigLayout on it:
        // JPanel externalPanel = new JPanel();
        JPanelWithBackground externalPanel = null;

        try {
            externalPanel = new JPanelWithBackground("resources/logo.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"), "w 50%, wrap");

        JTextField userNameTextField = new JTextField(20);

        externalPanel.add(userNameTextField, "w 90%, wrap");

        externalPanel.add(new JLabel("Password"), "w 50%, wrap");
        JTextField pswdTextField = new JTextField(20);
        externalPanel.add(pswdTextField, "w 90%, wrap");

        JButton loginButton = new JButton("Login");
        loginButton.setActionCommand("loginAction");

        externalPanel.add(loginButton, "w 25%, wrap");

        mainContainer.add(externalPanel);
        //mainFrame.add(mainContainer);

        show(loginFrame);


    }

    // Operation performed when the loginButton is clicked:
    public void actionPerformed(ActionEvent e) {
        System.out.println("Inside LoginFrame ---> actionPerformed()");
        if ("loginAction".equals(e.getActionCommand())) {
            System.out.println("loginButton clcked !!!");

        }
    }

}

As you can see in this class I have a JButton named loginButton and on this object I have set an ActionCommand , in this way:

JButton loginButton = new JButton("Login");
loginButton.setActionCommand("loginAction");

Then I have create the following actionPerformed that have to handle this event:

// Operation performed when the loginButton is clicked:
public void actionPerformed(ActionEvent e) {
    System.out.println("Inside LoginFrame ---> actionPerformed()");
    if ("loginAction".equals(e.getActionCommand())) {
        System.out.println("loginButton clcked !!!");

    }
}

So this method take the ActionCommand inside the event and if it is equals to loginAction print a message.

The problem is that don't enter in the actionPerformed() method (don't print the "Inside LoginFrame ---> actionPerformed()" string in the Eclipse console), so I can't handle this click event.

Why? what am I missing?

Tnx

Andrea

You forgot to add Action Listener to button. Add below code in startup() and it shall work fine.

 loginButton.addActionListener(this);

Also add implements ActionListener declaration to your class.

public class LoginFrame extends SingleFrameApplication implements ActionListener{..

try this

public class LoginFrame extends SingleFrameApplication implements ActionListener{

    private static final int FIXED_WIDTH = 550;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);

    private boolean loginResult = true;

    /*
    public static void main(String[] args) {
        System.out.println("DENTRO: LoginFrame() ---> main()");
        launch(LoginFrame.class, args);
    }
    */

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        System.out.println("Inside LoginFrame ---> startup()");


        JFrame loginFrame = this.getMainFrame();            // main JFrame that represents the Windows
        loginFrame.setTitle("XCloud Login");

        loginFrame.setPreferredSize(INITAL_SIZE);
        loginFrame.setResizable(false);

        Container mainContainer = loginFrame.getContentPane();      // main Container into the main JFrame


        // JPanel creation and settings of the MigLayout on it:
        // JPanel externalPanel = new JPanel();
        JPanelWithBackground externalPanel = null;

        try {
            externalPanel = new JPanelWithBackground("resources/logo.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"), "w 50%, wrap");

        JTextField userNameTextField = new JTextField(20);

        externalPanel.add(userNameTextField, "w 90%, wrap");

        externalPanel.add(new JLabel("Password"), "w 50%, wrap");
        JTextField pswdTextField = new JTextField(20);
        externalPanel.add(pswdTextField, "w 90%, wrap");

        JButton loginButton = new JButton("Login");
        loginButton.setActionCommand("loginAction");
        loginButton.addActionListener(this);
        externalPanel.add(loginButton, "w 25%, wrap");

        mainContainer.add(externalPanel);
        //mainFrame.add(mainContainer);

        show(loginFrame);


    }

    // Operation performed when the loginButton is clicked:
    public void actionPerformed(ActionEvent e) {
        System.out.println("Inside LoginFrame ---> actionPerformed()");
        if ("loginAction".equals(e.getActionCommand())) {
            System.out.println("loginButton clcked !!!");

        }
    }

}

It's better create your own ActionListener and implement the method actionPerformed later. I've done correctly one example very similar and it's my code. I expect it can help you:

1- Create your ActionListener:

ActionListener chooseMe = createChoiceAction();

2- Create the Button:

button = new JButton("Aceptar");
button.addActionListener(chooseMe);   
p.add(button);

3- Implement the createChoiceAction() method:

private ActionListener createChoiceAction() {
        ActionListener chooseMe = new ActionListener() {


            @Override
            public void actionPerformed(ActionEvent e) {

                System.out.println("Hola"+inputs.get(1));

            }
        };
        return chooseMe;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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