我是Java Swing 的新手,我在尝试使用 ActionPerformed 方法处理按钮上的click事件时遇到了一些问题,就像在文档中一样: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
安德烈
答案 0 :(得分:3)
您忘了将动作侦听器添加到按钮。在startup()
中添加以下代码,它可以正常工作。
loginButton.addActionListener(this);
还可以向您的类添加ActionListener声明。
public class LoginFrame extends SingleFrameApplication implements ActionListener{..
答案 1 :(得分: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 !!!");
}
}
}
答案 2 :(得分: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;
}