0

私は Java Swingの初心者であり、ドキュメントのようにActionPerformedメソッドを使用してボタンのクリック イベントを処理しようとすると問題が発生します: http://docs.oracle.com/javase/tutorial/uiswing/ components/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()」という文字列を出力しないでください)。そのため、このクリック イベントを処理できません。

なんで?私は何が欠けていますか?

TNX

アンドレア

4

3 に答える 3

3

アクション リスナーをボタンに追加するのを忘れました。以下のコードを追加するstartup()と、正常に動作します。

 loginButton.addActionListener(this);

また、implements ActionListener 宣言をクラスに追加します。

public class LoginFrame extends SingleFrameApplication implements ActionListener{..
于 2013-11-12T11:07:19.363 に答える
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 に答える