我創建了一個包含多個 GUI 類的專案。一切似乎都運行良好,但是當我嘗試實作 dispose(); 它提供以下錯誤的函式:執行緒“AWT-EventQueue-0”中的例外 java.lang.NullPointerException:無法呼叫“javax.swing.JFrame.dispose()”,因為“Login.loginframe”在 Login.actionPerformed(Login) 處為空.java:123)
這是以下代碼:
公共類主要{
public static void main(String[] args) {
Login loginframe = new Login();
}
}
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login implements ActionListener {
private static JLabel userlabel , success , logo;
private static JTextField userText;
private static JLabel passwordlabel;
private static JPasswordField passwordtext;
private static JButton loginbutton , continuebutton;//, continuebutton2;
private static JFrame loginframe;
private static JPanel panel;
Login(){
JFrame loginframe = new JFrame ("Login");
JPanel panel = new JPanel();
loginframe.setSize(300, 300);
loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginframe.add(panel);
loginframe.setLocationRelativeTo(null);
panel.setLayout(null);
userlabel = new JLabel("User :");
userlabel.setBounds(10, 20, 80, 25);
panel.add(userlabel);
userText = new JTextField();
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
passwordlabel = new JLabel ("Password : ");
passwordlabel.setBounds(10, 50, 80, 25);
panel.add(passwordlabel);
passwordtext = new JPasswordField ();
passwordtext.setBounds(100, 50, 165, 25);
panel.add(passwordtext);
logo = new JLabel();
logo.setIcon(new ImageIcon("emblem.jpg"));
logo.setBounds(115, 90, 50, 50);
panel.add(logo);
loginbutton = new JButton("Submit");
loginbutton.setBounds(100, 200, 80, 25);
loginbutton.setEnabled(true);
loginbutton.addActionListener(this);
panel.add(loginbutton);
success = new JLabel("");
success.setBounds(85, 130, 150, 80);
panel.add(success);
loginframe.setVisible(true);
continuebutton = new JButton("Continue");
continuebutton.setBounds(85, 200, 100, 25);
continuebutton.addActionListener(this);
continuebutton.setVisible(false);
continuebutton.setEnabled(false);
panel.add(continuebutton);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
String user = userText.getText();
String password = passwordtext.getText();
System.out.println(user ", " password);
if(user.equals("admin") && password.equals("password1")) {
success.setText("Log in successful!");
loginbutton.setVisible(false);
loginbutton.setEnabled(false);
continuebutton.setVisible(true);
continuebutton.setEnabled(true);
if(e.getSource()==continuebutton) {
userText.setText("");
passwordtext.setText("");
new adminmain();
loginframe.dispose();
}
}
if(user.equals("user") && password.equals("password")) {
success.setText("Log in successful!");
loginbutton.setVisible(false);
loginbutton.setEnabled(false);
continuebutton.setVisible(true);
continuebutton.setEnabled(true);
if(e.getSource()==continuebutton) {
userText.setText("");
passwordtext.setText("");
new usermain();
loginframe.dispose();
}
}
}
}
uj5u.com熱心網友回復:
您的主要方法中的這一行是您的問題 JFrame loginframe = new JFrame ("Login");
您正在將值分配給loginframemain 方法中的區域變數,而 actionPerformed 方法指的是類loginframe頂部的類變數Login。
通過簡單地改變這一行:
JFrame loginframe = new JFrame ("Login");
為此,您現在將值分配為類變數,而不會遇到空指標問題:
loginframe = new JFrame ("Login");
警告:如果您將擁有多個登錄物件/類的實體,那么使用這樣的靜態變數不是一個好主意。
uj5u.com熱心網友回復:
當我嘗試運行代碼時,它給了我這個錯誤。所以,如果我是正確的 adminmain() 或 usermain() 來自不同的類。如果嘗試將類匯入到主類。如果您找到了解決方案,請回復我可以從中學習的解決方案。謝謝。
Login.java:29: error: cannot find symbol
login();
^
符號:方法 login() 位置:類登錄 Login.java:118:錯誤:找不到符號 new adminmain();^ 符號:類 adminmain 位置:類登錄 Login.java:134:錯誤:找不到符號 new usermain(); ^ 符號:類用戶主位置:類登錄 Login.java:142:錯誤:不兼容型別:意外回傳值回傳 ex;^ 4 個錯誤
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/422239.html
標籤:
上一篇:JavaParser洗掉空行
