[英]ActionListener in Java doesn't work for me
I was watching a tutorial on how to crate a login system in Java and when I recreated it, it just wouldn't work.我正在观看有关如何在 Java 中创建登录系统的教程,当我重新创建它时,它就无法正常工作。 When I start the program I can see all my JLabels, JButtons, and my JPasswordField but when I press the reset button I get hit with this error message in the console
当我启动程序时,我可以看到我所有的 JLabels、JButtons 和我的 JPasswordField,但是当我按下重置按钮时,我在控制台中收到此错误消息
in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke java.util.EventObject.getSource()" because "this.e" is null
at LoginPage.actionPerformed(LoginPage.java:67)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
So at this point I thought I had misspelled something and so I copied the code from the guy that was making the tutorial and when I pasted it I got the same message.所以在这一点上,我以为我拼错了一些东西,所以我从制作教程的人那里复制了代码,当我粘贴它时,我得到了同样的信息。 At this point I am pretty desperate and I would take any help I can get.
在这一点上,我非常绝望,我会寻求任何我能得到的帮助。 TIA to anyone who helps.
TIA 给任何帮助的人。
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginPage implements ActionListener {
JFrame frame = new JFrame();
JButton loginButton = new JButton("Login");
JButton resetButton = new JButton("Reset");
JTextField userIDField = new JTextField();
JPasswordField userPasswordField = new JPasswordField();
JLabel userIDLabel = new JLabel("userID:");
JLabel userPasswordLabel = new JLabel("Password:");
JLabel messageLabel = new JLabel("");
HashMap<String,String> logininfo = new HashMap<String,String>();
private EventObject e;
LoginPage(HashMap<String,String> loginInfoOriginal){
logininfo = loginInfoOriginal;
//Labels
userIDLabel.setBounds(50, 100, 75, 25);
userPasswordLabel.setBounds(50, 150, 75, 25);
messageLabel.setBounds(125, 250, 250, 35);
messageLabel.setFont(new Font(null,Font.ITALIC,25));
//Fields
userIDField.setBounds(125,100,200,25);
userPasswordField.setBounds(125,150,200,25);
//Buttons
loginButton.setBounds(125, 200, 100, 25);
loginButton.addActionListener(this);
loginButton.setFocusable(false);
resetButton.setBounds(225, 200, 100, 25);
resetButton.setFocusable(false);
resetButton.addActionListener(this);
//Window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420,420);
frame.setLayout(null);
frame.setVisible(true);
frame.add(userIDLabel);
frame.add(userPasswordLabel);
frame.add(messageLabel);
frame.add(userIDField);
frame.add(userPasswordField);
frame.add(loginButton);
frame.add(resetButton);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(e.getSource()==resetButton);
userIDField.setText("");
userPasswordField.setText("");
}
}
Instead of calling e.getSource()
, use arg0.getSource()
.不要调用
e.getSource()
,而是使用arg0.getSource()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.