在 java 中使用 Swing 进行文件处理编程

File handling programming with Swing in java

我需要第 2 步的帮助才能理解 java 中文件处理的工作原理

这就是我正在做的.... :- 问题:异常 注意:我是这个概念的新手,请告诉我我的代码有什么问题。

-使用 swing 创建一个迷你 Java 应用程序,它必须要求输入用户名和密码并有一个创建按钮。

-按下创建按钮后,必须使用用户名创建一个新目录,并且必须将密码保存为该目录中的 password.txt 文件。

-如果该目录已经存在,则弹出窗口 windows 应该显示 "User already exists".

我已经试了几个小时了,但还是做不到,非常感谢您的帮助。

我需要查看一些代码,同时我一直在尝试修复我的代码。

更新代码:

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

class CreateUser implements ActionListener
{   
    JFrame fr;  //Frame
    JButton b1;  //Create Button
    JLabel lb1, lb2;    //Username and password
    JTextField tf1, tf2;    //Username and password input fields
    JPanel p1;

    CreateUser()
    {
        //Setting the frame
        fr=new JFrame();
        fr.setLayout(null);
        fr.setSize(400,400);

        //setting panel
        p1=new JPanel();
        p1.setBounds(0,0,400,400);
        p1.setLayout(null);

        //setting Username Label
        JLabel lb1=new JLabel("Username: ");
        lb1.setBounds(50,50,70,30);
        p1.add(lb1);

        //setting Username Text Field
        JTextField tf1 = new JTextField();
        tf1.setBounds(150,50,150,30);
        p1.add(tf1);


        //setting Password Label
        JLabel lb2=new JLabel("Password: ");
        lb2.setBounds(50,100,70,30);
        p1.add(lb2);

        //setting Password Text Field
        JTextField tf2 = new JTextField();
        tf2.setBounds(150,100,150,30);
        p1.add(tf2);

        //setting Button
        b1=new JButton("Create");
        b1.setBounds(100,200,100,40);   
        p1.add(b1);

        fr.add(p1);
        fr.setVisible(true);    
        b1.addActionListener(this);
        tf1.addActionListener(this);
        tf2.addActionListener(this);

    }

    public static void main(String s[])
    {
        new CreateUser();
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {   
            {
                String uid = tf1.getText();
                String pass = tf2.getText();

                String dir = System.getProperty("user.dir");

                //Creating a new folder Users

                File file = new File(dir+"\users");
                if (!file.exists()) 
                {
                    if (file.mkdir()) 
                    {
                        System.out.println("Directory is created!");
                    } 
                    else 
                    {
                        System.out.println("Failed to create directory!");
                    }
                }
                dir = dir+"\users";

                //Creating a folder named with username inside Users folder

                File file1 = new File(dir+"\"+uid);
                if (!file1.exists()) 
                {
                    if (file1.mkdir()) 
                    {
                        System.out.println("Directory is created!");
                    } 
                    else 
                    {
                        System.out.println("Failed to create directory!");
                    }
                }

                //Storing Password.txt inside users/username folder

                try
                {
                    FileOutputStream fout=new FileOutputStream("password.txt");
                    byte b[]=pass.getBytes();
                    fout.write(b);
                }
                catch(Exception ee)
                {}
            }
        }
    }           
}

这里有很多潜在的问题所以让我们看一下代码:

class CreateUser implements ActionListener {

小问题:避免让您的 GUI classes 实现您的控制界面。这给了 class 太多的责任,使 class 混乱,导致难以调试代码。最好使用匿名内部 class 或非匿名内部 class.

JFrame fr; // Frame
JButton b1; // Create Button
JLabel lb1, lb2; // Username and password
JTextField tf1, tf2; // Username and password input fields
JPanel p1;

小问题:字段应该始终是私有的,除非您有特殊原因需要 public。你不知道,所以在这里将它们设为私有。

CreateUser() {
    // Setting the frame
    fr = new JFrame();
    fr.setLayout(null);
    fr.setSize(400, 400);

小问题:从不,从不,从不使用 null 布局。虽然空布局和 setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用它们时 运行 就会遇到更严重的困难。它们不会在 GUI 调整大小时调整您的组件的大小,它们是皇家 来增强或维护的,当放置在滚动窗格中时它们会完全失败,当在所有平台或不同的屏幕分辨率上查看时它们看起来很糟糕从原来的。

    // setting panel
    p1 = new JPanel();
    p1.setBounds(0, 0, 400, 400);
    p1.setLayout(null);

同样的问题。了解布局管理器,然后使用 布局管理器。

    // setting Username Text Field
    JTextField tf1 = new JTextField();

主要问题:您正在遮挡此处的字段。通过在 class 构造函数中重新声明 tf1 变量,您将 JTextField 对象分配给 local 变量,这意味着class 中的 tf1 字段仍然是 empty/unassigned/null。如果您尝试使用此空字段,这可能会导致调用 NullPointerException。

因此,如果您要在构造函数或 init 方法中进行对象创建和赋值,那么

    JTextField tf1 = new JTextField();

做:

    tf1 = new JTextField(); 


    // setting Password Text Field
    JTextField tf2 = new JTextField();

同样的问题。此外,这应该是 JPasswordField 而不是 JTextField。

    // setting Button
    b1 = new JButton("Create");

出于某种原因,您正确分配了按钮。去图吧。

public static void main(String s[]) {
    new CreateUser();
}

始终在 Swing 事件线程或 EDT(对于事件调度线程)上启动 Swing 应用程序。也一样:

public static void main(String s[]) {
    SwingUtilities.invokeLater(() -> new CreateUser());        
}

try {
    FileOutputStream fout = new FileOutputStream("password.txt");
    byte b[] = pass.getBytes();
    fout.write(b);
} catch (Exception ee) {

}

主要问题:永远不会有一个空的 catch 块,这相当于尝试蒙着眼罩驾驶汽车的编码。至少打印出堆栈跟踪,以便在错误发生时通知您。此外,如果您要写出文本,请使用 PrintWriter 等编写器。我不会讨论写出密码文本的危险,但显然这是你在现实生活中永远不会做的事情。