gpt4 book ai didi

java - 从 GUI 读取文本文件

转载 作者:行者123 更新时间:2023-11-30 08:20:47 25 4
gpt4 key购买 nike

我有一个 GUI 可以验证用户名和密码。作业的另一部分是读取包含用户名和密码的文件,并检查它是否与用户在文本字段中输入的内容相匹配。如果它确实匹配,那么它会隐藏登录页面,并且会出现另一个带有“欢迎”消息的页面。我对文本文件的经验为零,我应该把那段代码放在哪里?我假设它会进入 ActionListener 方法而不是主要方法,但我只是迷路了。我只需要朝正确的方向稍微插入一下。这是我到目前为止所拥有的。任何帮助将不胜感激。谢谢!

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import javax.swing.*;
import java.io.*;
/**
*/
public class PassWordFrame extends JFrame
{
private static final int FIELD_WIDTH = 10;
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 300;
private JLabel fileRead;
private JLabel instruct;
private JLabel username;
private JLabel password;
private JTextField usertext;
private JTextField passtext;
private JButton login;
private ActionListener listener;
//String text = "";

public PassWordFrame()
{
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
listener = new ClickListener();
}
class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String inputFileName = ("users.txt");
File userFile = new File(inputFileName);

}
}
public void createComponents()
{
Color blue = new Color(0,128,155);
Font font = new Font("Times New Roman", Font.BOLD, 14);

instruct = new JLabel("Please enter your username and password.");
instruct.setFont(font);

username = new JLabel("Username: ");
username.setFont(font);

password = new JLabel("Password: ");
password.setFont(font);

usertext = new JTextField(FIELD_WIDTH);
passtext = new JTextField(FIELD_WIDTH);

login = new JButton("Login");
login.setFont(font);

instruct.setForeground(Color.BLACK);
login.setForeground(Color.BLACK);
username.setForeground(Color.BLACK);
password.setForeground(Color.BLACK);

login.addActionListener(listener);

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();

panel1.setBackground(blue);
panel2.setBackground(blue);
panel3.setBackground(blue);
panel4.setBackground(blue);

panel1.add(instruct);
panel2.add(username);
panel2.add(usertext);
panel3.add(password);
panel3.add(passtext);
panel4.add(login);

add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.WEST);
add(panel3, BorderLayout.CENTER);
add(panel4, BorderLayout.SOUTH);

pack();
}
}

import javax.swing.*;
import java.awt.*;

/**

*/
public class PassWordFrameViewer
{
public static void main(String[] args)
{
JFrame frame = new PassWordFrame();
frame.setTitle("Password Verification");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}
}

最佳答案

首先,您在调用 #createComponents() 方法后初始化监听器 (listener = new ClickListener()),这意味着您将添加一个 null 登录按钮的监听器。所以你的构造函数应该是这样的:

    public PassWordFrame() {
listener = new ClickListener();
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}

然后,因为您想用欢迎消息更改 GUI,您应该使用 SwingWorker ,一个旨在在后台线程中执行 GUI 交互任务的类。在 javadoc 中您可以找到很好的示例,但这里也是一个很好的教程:Worker Threads and SwingWorker .

下面我只写监听器实现(使用 swing worker):

    class ClickListener implements ActionListener {
public void actionPerformed(ActionEvent event) {

new SwingWorker<Boolean, Void>() {

@Override
protected Boolean doInBackground() throws Exception {

String inputFileName = ("users.txt");
File userFile = new File(inputFileName);

BufferedReader reader = new BufferedReader(new FileReader(userFile));

String user;
String pass;

try {
user = reader.readLine();
pass = reader.readLine();
}

catch (IOException e) {

//
// in case something is wrong with the file or his contents
// consider login failed

user = null;
pass = null;

//
// log the exception

e.printStackTrace();
}

finally {
try {
reader.close();
} catch (IOException e) {
// ignore, nothing to do any more
}
}

if (usertext.getText().equals(user) && passtext.getText().equals(pass)) {
return true;
} else {
return false;
}
}

@Override
protected void done() {

boolean match;

try {
match = get();
}

//
// this is a learning example so
// mark as not matching
// and print exception to the standard error stream

catch (InterruptedException | ExecutionException e) {
match = false;
e.printStackTrace();
}

if (match) {
// show another page with a "Welcome" message
}
}
}.execute();
}
}

另一个提示:不要将组件添加到 JFrame,因此将其替换为:

    add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.WEST);
add(panel3, BorderLayout.CENTER);
add(panel4, BorderLayout.SOUTH);

与:

    JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(panel1, BorderLayout.NORTH);
contentPane.add(panel2, BorderLayout.WEST);
contentPane.add(panel3, BorderLayout.CENTER);
contentPane.add(panel4, BorderLayout.SOUTH);

setContentPane(contentPane);

关于java - 从 GUI 读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25716718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com