簡體   English   中英

Java中使用Swing進行文件處理編程

[英]File handling programming with Swing in java

我需要第2步的幫助,以了解Java,...,文件處理的工作原理

這就是我正在做的....:-問題:異常注:我是這個概念的新手,請告訴我代碼中的錯誤。

-使用swing創建一個迷你Java應用程序,該應用程序必須詢問用戶名和密碼並具有創建按鈕。

-按下創建按鈕后,必須立即創建一個帶有用戶名的新目錄,並且密碼必須另存為該目錄內的password.txt文件。

-如果目錄已經存在,則將出現一個彈出窗口,提示“用戶已經存在”。

我已經嘗試了幾個小時,但無法正確完成,非常感謝您的幫助。

在我不斷嘗試修復我的問題時,我需要查看一些代碼。

更新的代碼:

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類實現您的控制接口。 這給了類太多的責任,使類​​混亂,導致難以調試代碼。 最好使用匿名內部類或非匿名內部類。

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);

次要問題:從不,從不,從不使用null布局。 盡管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();

主要問題 :您正在此處遮蓋一個字段。 通過在類構造函數中重新聲明tf1變量,可以將JTextField對象分配給局部變量,這意味着類中的tf1字段保持為空/未分配/空。 如果您嘗試使用此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) {

}

主要問題:永遠不會有空的擋塊,這相當於試圖開車蒙上眼罩的汽車。 至少打印出堆棧跟蹤,以便在發生錯誤時以及發生錯誤時通知您。 另外,如果要寫文本,請使用諸如PrintWriter之類的書寫器。 我不會寫出密碼文本的危險,但是顯然這是您在現實生活中從未做過的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM