我正在重新编写一个旧的登录屏幕代码,我正在更改:
JPasswordField.getText();
要:
JPasswordFieald.getPassword();
这更难,因为它输出了字符。
无论如何,JFrame.dispose()不适合我。我希望我的程序将旧的JFrame部署到新的JFrame(e.e JFrame2)。
这是我的代码:
public class Launcher {
//Define Variables
public static String VER = "1.1.0";
public static String STATE = " ALPHA ";
public static JFrame launcher;
public static char[] sPass;
public static String sUser;
//Create widgets
public static JTextField User = new JTextField();
public static JPasswordField Pass = new JPasswordField();
public static JButton Login = new JButton("Login");
//Runs when program starts
public static void main(String[] args) {
NewFrame("Infinite Doom Launcher");
//Checks if login has been pressed
Login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sUser = User.getText();
sPass = Pass.getPassword();
//checks if password is correct
if(CheckPass(sPass)) {
if(sUser.equals("genfy")){
Game.main(null);
launcher.dispose();
}
}
}
});
}
//Creates new frame
public static void NewFrame(String Name) {
JFrame launcher = new JFrame(Name + " " + STATE + VER);
launcher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
launcher.setSize(500,95);
launcher.setResizable(false);
//Add widgets to a Border Layout
launcher.setLayout(new BorderLayout());
launcher.add(User, BorderLayout.NORTH);
launcher.add(Pass, BorderLayout.CENTER);
launcher.add(Login, BorderLayout.SOUTH);
//Set visible
launcher.setVisible(true);
}
//Checks if the entered password is correct
private static boolean CheckPass(char[] input) {
boolean isCorrect = true;
char[] correctPassword = { 'g', 'e', 'n', 'f', 'y', 'g', 'e', 'n', 'y', 's' };
if (input.length != correctPassword.length) {
isCorrect = false;
} else {
isCorrect = Arrays.equals (input, correctPassword);
}
//Zero out the password.
Arrays.fill(correctPassword,'0');
return isCorrect;
}
}
所以问题似乎发生在:
//checks if password is correct
if(CheckPass(sPass)) {
if(sUser.equals("genfy")){
Game.main(null);
launcher.dispose();
}
具体是:
launcher.dispose();
因此,当我按下JButton(登录)时,会出现此错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.Launcher$1.actionPerformed(Launcher.java:42)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我为我的代码这么久而道歉。
答案 0 :(得分:1)
此行NewFrame
方法似乎存在范围问题:
JFrame launcher = new JFrame(Name + " " + STATE + VER);
此launcher
变量是NewFrame
方法的本地变量,并隐藏launcher
类成员。因此,当您在launcher.dispose()
方法中拨打actionPerformed()
时,您会收到NullPointerException
。要解决这个问题,你应该做这个改变:
launcher = new JFrame(Name + " " + STATE + VER);
还要看一下这个主题:The Use of Multiple JFrames, Good/Bad Practice?
正如@kleopatra建议的那样,了解java命名约定并坚持使用它们。请参阅本文档中的更多内容:Java Code Conventions