我在看一个关于如何用java构建登录系统的教程,当我重新创建它时,它就不起作用了。当我启动程序时,我可以看到所有的jlabel、jbuttons和jpasswordfield,但是当我按下reset按钮时,控制台中出现了这个错误消息
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)
所以在这一点上,我认为我拼错了一些东西,所以我复制了代码的家伙,使教程,当我粘贴它,我得到了同样的信息。在这一点上,我是相当绝望,我会采取任何帮助,我可以得到。感谢任何帮助你的人。
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("");
}
}
1条答案
按热度按时间e4yzc0pl1#
而不是打电话
e.getSource()
,使用arg0.getSource()
.