我是 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()”字符串),所以我无法处理这个点击事件。
为什么?我错过了什么?
肿瘤坏死因子
安德烈亚