public LoginRegisterApp() { setTitle("登录/注册"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); setLayout(new GridLayout(3, 2)); JLabel usernameLabel = new JLabel("用户名:"); JLabel passwordLabel = new JLabel("密码:"); usernameField = new JTextField(); passwordField = new JPasswordField(); loginButton = new JButton("登录用户存档"); registerButton = new JButton("创建用户存档"); loginButton.addActionListener(e -> { String username = usernameField.getText(); String password = passwordField.getText(); System.out.println(username); String sql = "SELECT * FROM `use` WHERE useName = ? && usePassword = ?"; Use use = (Use) DBUtil.getObject(Use.class, sql, username, password); // 在这里执行登录操作 if (use != null) { JOptionPane.showMessageDialog(LoginRegisterApp.this, "存档成功"); dispose(); } else { JOptionPane.showMessageDialog(LoginRegisterApp.this, "用户不存在,请重新登录!"); } }); registerButton.addActionListener(e -> { String username = usernameField.getText(); String password = passwordField.getText(); String sql = "SELECT * FROM `use` WHERE useName = ?"; Use use = (Use) DBUtil.getObject(Use.class, sql, username); // 在这里执行注册操作 if (use != null) { JOptionPane.showMessageDialog(LoginRegisterApp.this, "该用户已存在,请重新操作!"); } else { if (passwordField.getText().isEmpty()) { DBUtil.executeUpdate("INSERT INTO `use`(useName)VALUES(?)", username); } else { DBUtil.executeUpdate("INSERT INTO `use`(useName,usePassword)VALUES(?,?)", username, password); } JOptionPane.showMessageDialog(LoginRegisterApp.this, "存档成功"); dispose(); } }); add(usernameLabel); add(usernameField); add(passwordLabel); add(passwordField); add(loginButton); add(registerButton); setVisible(true); }如何在另一个函数中得到use对象
时间: 2024-04-16 14:26:17 浏览: 31
要在另一个函数中获取 `use` 对象,你需要将 `use` 对象定义为类的成员变量,并提供一个公共的访问方法来获取该对象。以下是一个示例:
```java
public class LoginRegisterApp extends JFrame {
private Use use; // 声明 use 对象作为成员变量
// 其他代码...
public Use getUse() {
return use; // 提供一个公共的访问方法
}
// 其他代码...
}
```
在上述代码中,我们将 `use` 对象声明为 `LoginRegisterApp` 类的成员变量,并添加了一个名为 `getUse` 的公共方法来获取该对象。
然后,在其他函数中,你可以通过调用 `getUse` 方法来获取 `use` 对象。
```java
// 其他方法...
public void someOtherMethod() {
LoginRegisterApp app = new LoginRegisterApp();
// 其他代码...
// 获取 use 对象
Use use = app.getUse();
if (use != null) {
// 在这里可以使用 use 对象
System.out.println("useName: " + use.getUseName());
System.out.println("usePassword: " + use.getUsePassword());
// 其他属性...
}
}
```
请确保在调用 `getUse` 方法之前,已经完成了登录或注册操作,并且 `use` 对象已经被正确地赋值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)