![](/img/trans.png)
[英]stale element reference: element is not attached to the page, while clicking a hidden button using xpath
[英]Hidden button while initializating interface
我正在使用 Netbeans,我正在制作一個應用程序。 問題是你可以注冊或登錄到應用程序,所以我想在你登錄的情況下設置一個條件,登錄和注冊按鈕被隱藏,但似乎無法編輯生成代碼來設置作為不可見的。 有沒有辦法在您登錄的情況下不必創建新界面就可以做到這一點? 謝謝。
如果不閱讀您的代碼,我不確定什么能更好地解決您的問題。 使 UI 組件響應事件不可見似乎是一個需要解決的小問題。 當然,下面的代碼可以有按鈕組件的 getter 方法來訪問它們的 class 並使它們不可見:
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.event.*;
public class Frame extends JFrame {
private JPanel contentPane;
private JTextField txtUser;
private JPasswordField pwdField;
private JButton btnLogin, btnRegister;
private String username = "overflower";
private String myPassword = "sosecret";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame frame = new Frame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200, 200, 330, 240);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(45, 44, 60, 14);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(45, 88, 60, 14);
contentPane.add(lblPassword);
txtUser = new JTextField();
txtUser.setBounds(145, 41, 115, 20);
contentPane.add(txtUser);
txtUser.setColumns(10);
pwdField = new JPasswordField();
pwdField.setBounds(145, 85, 115, 20);
contentPane.add(pwdField);
btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (txtUser.getText().equals(username) && pwdField.getText().equals(myPassword)) {
btnLogin.setVisible(false);
btnRegister.setVisible(false);
}
}
});
btnLogin.setBounds(45, 143, 89, 23);
contentPane.add(btnLogin);
btnRegister = new JButton("Register");
btnRegister.setBounds(172, 143, 89, 23);
contentPane.add(btnRegister);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.