Java GUI 登录屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26665538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java GUI login screen
提问by ThomasMcDonald
I have created a login screen for my Java GUI program, The login screen on a button click checks if the username and password that was entered in the textfields was equal to the lines in the text file and if it does it allows the user to move to the next screen, if not a JOptionPane.showMessageDialog
will display. I also have another screen that allows the user to write to the file which inserts a new username and password.
我为我的 Java GUI 程序创建了一个登录屏幕,单击按钮上的登录屏幕检查在文本字段中输入的用户名和密码是否等于文本文件中的行,如果它允许用户移动到下一个屏幕,如果不是一个JOptionPane.showMessageDialog
将显示。我还有另一个屏幕,允许用户写入插入新用户名和密码的文件。
Right so my problem is that it reads only the last 2 lines of code, so i am only able to login with the last created username and password. How do i go about making it read every single line to check for the username and password?
对,所以我的问题是它只读取最后两行代码,所以我只能使用上次创建的用户名和密码登录。我如何让它读取每一行以检查用户名和密码?
The main Login screen
主登录屏幕
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class login extends JFrame {
JButton blogin;
JPanel loginpanel;
JTextField txuser;
JTextField pass;
JButton newUSer;
JLabel username;
JLabel password;
public login(){
super("Login Autentification");
blogin = new JButton("Login");
loginpanel = new JPanel();
txuser = new JTextField(15);
pass = new JPasswordField(15);
newUSer = new JButton("New User?");
username = new JLabel("User - ");
password = new JLabel("Pass - ");
setSize(300,200);
setLocation(500,280);
loginpanel.setLayout (null);
txuser.setBounds(70,30,150,20);
pass.setBounds(70,65,150,20);
blogin.setBounds(110,100,80,20);
newUSer.setBounds(110,135,80,20);
username.setBounds(20,28,80,20);
password.setBounds(20,63,80,20);
loginpanel.add(blogin);
loginpanel.add(txuser);
loginpanel.add(pass);
loginpanel.add(newUSer);
loginpanel.add(username);
loginpanel.add(password);
getContentPane().add(loginpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Writer writer = null;
File check = new File("userPass.txt");
if(check.exists()){
//Checks if the file exists. will not add anything if the file does exist.
}else{
try{
File texting = new File("userPass.txt");
writer = new BufferedWriter(new FileWriter(texting));
writer.write("message");
}catch(IOException e){
e.printStackTrace();
}
}
blogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
File file = new File("userPass.txt");
Scanner scan = new Scanner(file);;
String line = null;
FileWriter filewrite = new FileWriter(file, true);
String usertxt = " ";
String passtxt = " ";
String puname = txuser.getText();
String ppaswd = pass.getText();
while (scan.hasNext()) {
usertxt = scan.nextLine();
passtxt = scan.nextLine();
}
if(puname.equals(usertxt) && ppaswd.equals(passtxt)) {
MainMenu menu =new MainMenu();
dispose();
}
else if(puname.equals("") && ppaswd.equals("")){
JOptionPane.showMessageDialog(null,"Please insert Username and Password");
}
else {
JOptionPane.showMessageDialog(null,"Wrong Username / Password");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}
} catch (IOException d) {
d.printStackTrace();
}
}
});
newUSer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
NewUser user = new NewUser();
dispose();
}
});
}
}
And the new user file
和新的用户文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class NewUser extends JFrame {
JButton create;
JPanel newUserPanel;
JTextField txuserer;
JTextField passer;
public NewUser(){
super("Registration");
create = new JButton("Create");
newUserPanel = new JPanel();
txuserer = new JTextField(15);
passer = new JPasswordField(15);
setSize(300,200);
setLocation(500,280);
newUserPanel.setLayout (null);
txuserer.setBounds(70,30,150,20);
passer.setBounds(70,65,150,20);
create.setBounds(110,100,80,20);
newUserPanel.add(create);
newUserPanel.add(txuserer);
newUserPanel.add(passer);
getContentPane().add(newUserPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Writer writer = null;
File check = new File("userPass.txt");
if(check.exists()){
//Checks if the file exists. will not add anything if the file does exist.
}else{
try{
File texting = new File("userPass.txt");
writer = new BufferedWriter(new FileWriter(texting));
writer.write("message");
}catch(IOException e){
e.printStackTrace();
}
}
create.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
File file = new File("userPass.txt");
Scanner scan = new Scanner(file);;
FileWriter filewrite = new FileWriter(file, true);
String usertxter = " ";
String passtxter = " ";
String punamer = txuserer.getText();
String ppaswder = passer.getText();
while (scan.hasNext()) {
usertxter = scan.nextLine();
passtxter = scan.nextLine();
}
if(punamer.equals(usertxter) && ppaswder.equals(passtxter)) {
JOptionPane.showMessageDialog(null,"Username is already in use");
txuserer.setText("");
passer.setText("");
txuserer.requestFocus();
}
else if(punamer.equals("") && ppaswder.equals("")){
JOptionPane.showMessageDialog(null,"Please insert Username and Password");
}
else {
filewrite.write(punamer+"\r\n" +ppaswder+ "\r\n");
filewrite.close();
JOptionPane.showMessageDialog(null,"Account has been created.");
dispose();
login log = new login();
}
} catch (IOException d) {
d.printStackTrace();
}
}
});
}
}
采纳答案by Hovercraft Full Of Eels
This:
这个:
if(puname.equals(usertxt) && ppaswd.equals(passtxt)) {
MainMenu menu =new MainMenu();
dispose();
}
Needs to go inside of this:
需要深入了解:
while (scan.hasNext()) {
usertxt = scan.nextLine();
passtxt = scan.nextLine();
}
You're looping through the file with the while loop, but only testing the equality of the Strings afterthe while loop ends. This won't work as you're finding out.
您正在使用 while 循环遍历文件,但仅在 while 循环结束后测试字符串的相等性。你发现这行不通。