문제

아마도 간단한 질문 (그리고 끔찍한 레이아웃 방법)을 실례합니다.입력 된 데이터가 입력 된 데이터를 TXT 파일에 성공적으로 작성하고 "제출"을 클릭하면 입력 창을 닫고 "메뉴"를 엽니 다. 옵션이 사용자 (이 코드) 또는 검색 등록 정보 (무관없는)를 추가 할 수 있습니다.문제가없는 TXT 파일에 하나의 세부 정보를 입력 할 수 있지만 상자에 입력 한 내용을 누르면 동일한 데이터가 파일에 입력 된 데이터가 이전 시간으로 파일에 입력 되더라도 다음 데이터가 파일에 입력됩니다.나는 창을 다시 열기 전에 어떤 변수를 제거하는 것과 관련이있는 것이 있다고 생각합니다 (바닥을 향해 시도한 것처럼 시도해보십시오).감사합니다

AddUser.java

package assignment; 
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.lang.*;

public class AddUser extends JFrame {

    //Declare the array values

    private String[] Name;
    private String[] Username;
    private String[] Password;
    private String[] StaffID;

    public String inputStaff;
    public String inputUser;
    public String inputPass;
    public String inputID;

    static public String inputData;


    //Declare Text Fields
    public JTextField Field1;
    public JTextField Field2;
    public JTextField Field3;
    public JTextField Field4;
    public JTextField Field5;

    //Declare Labels

    private JLabel Label;
    private JLabel Label1;
    private JLabel Label2;
    private JLabel Label3;
    private JLabel Label4;
    private JLabel Label5;
    private JLabel Space1;
    private JLabel Space2;

public AddUser() {

    super("Add New Agent");     //Window Title
    setLayout(new FlowLayout(FlowLayout.LEFT));    //Set Layout Type as FlowLayout

    Label = new JLabel("Enter the Member of Staff's Details");
    Label1 = new JLabel("Staff Name");    //Label Values
    Label2 = new JLabel("Username");
    Label3 = new JLabel("Password");
    Label4 = new JLabel("Confirm Password");
    Label5 = new JLabel("Staff ID");
    Space1 = new JLabel("    ");
    Space2 = new JLabel("                                       ");

    Field1 = new JTextField (10);   //Create the Text Fields and Option Blocks & Arguments
    Field2 = new JTextField (10);
    Field3 = new JTextField (10);
    Field4 = new JTextField (10);
    Field5 = new JTextField (4);






    //Add the labels, textfields and option blocks to the JFrame

    add (Label); add(Space1); add (Label1); add (Field1); add (Label2); add (Field2); add (Label3); add (Field3); add (Label4);
    add (Field4); add (Label5); add (Field5); add (Space2);





    JButton button1 = new JButton("Submit");    //Add "Search" button to JFrame
    add (button1);

    onClick handler = new onClick();
    button1.addActionListener(handler);

    }

    private class onClick implements ActionListener{
        public void actionPerformed(ActionEvent event){

//Action to be performed

//Attempt to clear the fields

           inputStaff = ("");
           inputUser = ("");
           inputPass = ("");
           inputID = ("");




           inputStaff = Field1.getText();
           inputUser = Field2.getText();
           inputPass = Field3.getText();
           inputID = Field5.getText();

           inputData = inputStaff + " " + inputUser + " " + inputPass + " " + inputID;


           WriteFile Write = new WriteFile(); //Create instance of write-to-file

           setVisible(false);
            //Close the window on clicking submit



               }

            }


           }
.

파일 코드에 대한 쓰기 (writefile.java)는 다음과 같습니다.

package assignment;

import java.io.*;

public class WriteFile{
static String data = AddUser.inputData;
BufferedWriter out;

public WriteFile(){
    try {
        out = new BufferedWriter(new FileWriter("AddUser.txt", true));

        out.write(data);

        out.newLine();

        out.close();
    }
    catch(IOException e)
    {
        System.out.println("There was a problem:" + e);

    }
}


}
.

도움이 되었습니까?

해결책

이렇게하면 몇 가지 매너가 부족한 것을 달성하기 위해 다음을 고려하십시오.

public static void WriteFile(String data){
    try {
        out = new BufferedWriter(new FileWriter("AddUser.txt", true));
        out.write(data);
        out.newLine();
        out.close();
    }
    catch(IOException e)
    {
        System.out.println("There was a problem:" + e);
    }
}
.

와 같이 호출하십시오 :

WriteFile.WriteFile(inputData);
.

메소드의 이름을 변경하지만 원래 코드에 최대한 가깝게 유지하려고 노력했습니다.

이 방법으로 클래스의 필드에 SomeClass.someField가 필요하지 않고 정적 멤버를 피할 때 정적 멤버를 피하십시오.

다른 팁

라인

static String data = AddUser.inputData;
.

는 클래스가로드 될 때만 실행됩니다.그것이 모든 정적 변수가있는 경우입니다.( "DataFlow Programming"또는 스프레드 시트의 줄을 따라 생각하는 것처럼 보이지만 Java는 그렇게 작동하지 않습니다. 또는 문자열 객체가 업데이트 가능하다고 생각할 수 있습니다./ P>

클래스간에 전달되는 데이터를 구현하는 끔찍한 방법이며 볼 수 있듯이 작동하지 않습니다.클래스가 일찍 일어 났을 때 일어난 일이 일어난 일이 일어난 경우에는 한 번 작동하지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top