為 GUI 創建登錄類,將所有 GUI 構造添加到方法中,以便我可以在啟動時從主類呼叫它。呼叫方法拋出錯誤“'GUILoginPage.this' 不能從靜態背景關系中參考”。
有什么解決方法?我理解無法使用 .this 背后的原因,但我還沒有看到任何解決方案
public static void main() {
JFrame frame = new JFrame();
JButton loginButton = new JButton("Login");
JTextField userIDField = new JTextField();
JPasswordField userPasswordField = new JPasswordField();
JLabel userIDLabel = new JLabel("Username:");
JLabel userPasswordLabel = new JLabel("Password:");
userIDLabel.setBounds(50, 100, 75, 25);
userPasswordLabel.setBounds(50, 150, 75, 25);
userIDField.setBounds(125, 100, 200, 25);
userPasswordField.setBounds(125, 150, 200, 25);
loginButton.setBounds(125, 200, 100, 25);
loginButton.addActionListener(this);
frame.add(userIDLabel);
frame.add(userPasswordLabel);
frame.add(userIDField);
frame.add(userPasswordField);
frame.add(loginButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
uj5u.com熱心網友回復:
this在靜態背景關系中沒有任何意義。你的主要是靜態的,所以這意味著沒有一個類的實體可以使用。
在這里,您需要定義和使用您自己的動作偵聽器來處理按鈕單擊,例如:
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
performLogin();
}
} );
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488153.html
