0

我是 Java Swing的新手,尝试使用ActionPerformed方法处理按钮上的单击事件时遇到了一些问题,如文档中所示:http: //docs.oracle.com/javase/tutorial/uiswing/组件/button.html#abstractbutton

所以我有这个LoginFrame类:

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 !!!");

        }
    }

}

正如你在这个类中看到的,我有一个名为loginButton的JButton并且在这个对象上我设置了一个ActionCommand,这样:

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

然后我创建了以下必须处理此事件的actionPerformed

// 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 !!!");

    }
}

因此,此方法采用事件中的ActionCommand,如果它等于loginAction ,则打印一条消息。

问题是不要进入actionPerformed()方法(不要在Eclipse控制台打印“Inside LoginFrame ---> actionPerformed()”字符串),所以我无法处理这个点击事件。

为什么?我错过了什么?

肿瘤坏死因子

安德烈亚

4

3 回答 3

3

您忘记将动作侦听器添加到按钮。添加下面的代码startup(),它应该可以正常工作。

 loginButton.addActionListener(this);

还将实现 ActionListener 声明添加到您的类。

public class LoginFrame extends SingleFrameApplication implements ActionListener{..
于 2013-11-12T11:07:19.363 回答
0

最好创建自己的 ActionListener 并稍后实现方法 actionPerformed。我已经正确地完成了一个非常相似的示例,这是我的代码。我希望它可以帮助你:

1-创建您的 ActionListener:

ActionListener chooseMe = createChoiceAction();

2-创建按钮:

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

3- 实现 createChoiceAction() 方法:

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


            @Override
            public void actionPerformed(ActionEvent e) {

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

            }
        };
        return chooseMe;
    }
于 2014-12-03T16:05:46.840 回答
0

尝试这个

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 !!!");

        }
    }

}
于 2013-11-12T11:17:11.883 回答