swing Java登录屏幕如何读取和保存到本地数据库
嗨,我正在尝试制作一个GUI login screen
可以读取和保存密码(纯文本很好,我不需要加密)和用户名。
但我不知道如何做到这一点,到目前为止,我有这个。
我更喜欢将它们存储在本地文件中,而不是mysql中,但这样就可以了
package com.edu4java.swing.tutrial4;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginView3 {
public static void main(String[] args) {
JFrame frame = new JFrame("Login");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
placeComponents(frame);
frame.setVisible(true);
}
private static void placeComponents(JFrame frame) {
frame.setLayout(null);
JLabel userLabel = new JLabel("User");
userLabel.setBounds(10, 10, 80, 25);
frame.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 10, 160, 25);
frame.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 40, 80, 25);
frame.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 40, 160, 25);
frame.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(10, 80, 80, 25);
frame.add(loginButton);
JButton registerButton = new JButton("register");
registerButton.setBounds(180, 80, 80, 25);
frame.add(registerButton);
ActionListener loginButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
JOptionPane.showMessageDialog(source, source.getText()
+ " button has been pressed");
}
};
loginButton.addActionListener(loginButtonListener);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog((Component) e.getSource(),
"button has been pressed");
}
});
}
}
package com.edu4java.swing.tutrial4;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class MyButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
JOptionPane.showMessageDialog(source, source.getText() + " button has been pressed");
}
}
package com.edu4java.swing.tutrial4;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class LoginButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "login button has been pressed");
}
}
# 1 楼答案
你已经有数据库了吗? 如果不是,我建议使用MySQL工作台来创建它
之后,您可以使用JDBCMySQL驱动程序访问数据库。Java还提供了一种称为Java持久性API(JPA)的非常酷的技术,它使访问具有更多表的数据库比使用“普通方法”更容易
# 2 楼答案
代码有几个问题。您有LoginButtonListener和MyButtonListener,这在LoginView3中没有使用
此外,如果需要写入文件,文件的结构是什么(即逗号分隔、制表符分隔等)
在这一点上,这是一个IO问题,而不是UI问题
如果需要在一行中存储用户/密码(逗号分隔)。 然后使用FileOutPutStream,您可以将带有用户/密码的字符串写入一行
例: