简体   繁体   English

从jtextfield提供数据并将它们存储到文件中

[英]give data from jtextfield and store them into file

I am going to write a library aplication program. 我打算写一个库应用程序。

I work with netbeans. 我和netbeans一起工作。

i have an interface like this: 我有这样的界面:

package Library;

 public interface UserInformation {

 public void setFName(String fn);
 public String getFName();
 public void setLName(String ln);
 public String getLName();
 public void setRegNum(int reg_num);
 public int getRegNum();
 public void setDate(int reg_date);
 public int getDate();
}

and my NewUserDialog implements this interface: 我的NewUserDialog实现了这个接口:

private String FirstName="";
private String LastName="";
private int Registration_Number=0;
private int Date=0;
private String fileadress="AllUserRecords.txt";

public NewUserDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();

}
// // <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Tunga", 1, 14)); // NOI18N
    jLabel1.setText("Add New User (Registration)");

    jLabel2.setText("First Name:");

    jLabel3.setText("Last Name:");

    jLabel4.setText("Date:");

    jTextField2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jTextField2ActionPerformed(evt);
        }
    });

jButton1.setText("Create");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("Back");

    jLabel5.setText("Registration Number is:");

    jLabel6.setText("            ");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(54, 54, 54)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel4)
                        .addComponent(jLabel3)
                        .addComponent(jLabel5))
                    .addGap(98, 98, 98)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField3)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField2))
                        .addComponent(jLabel1)))
                .addGroup(layout.createSequentialGroup()
                    .addGap(68, 68, 68)
                    .addComponent(jButton1)))
            .addContainerGap(141, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addGap(26, 26, 26)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel2)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(28, 28, 28)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addGap(34, 34, 34)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(jLabel6))
            .addGap(27, 27, 27)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jButton2))
            .addGap(116, 116, 116))
    );

    pack();
}// </editor-fold>                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try{
    setFName(jTextField1.getText());
    if(havedigit(FirstName)==true) throw new Exception();
    WriteToFile(getFName());
    setLName(jTextField2.getText());
    if(havedigit(LastName)==true) throw new Exception();
    WriteToFile(getLName());
    setDate(Integer.parseInt(jTextField3.getText()));
    WriteToFile(String.valueOf(getDate()));
    Random rnd1=new Random();
    Registration_Number=rnd1.nextInt(100);
    setRegNum(Registration_Number);
    WriteToFile(String.valueOf(getRegNum()));
    jLabel6.setText(String.valueOf(getRegNum()));
    }
    catch(Exception e){
        jLabel6.setText("Error!");
    }
}                                        

public boolean havedigit(String in){
    for(int i=0;i<in.length();i++){
        if(Character.isDigit(in.charAt(i)))  return true;
    }
    return false;
}

public void WriteToFile(String content){
    try{
        File f=new File("C:\\userrecords.txt");
        if(!f.exists()){
            f.createNewFile();
        }
        else{
            FileWriter fw=new FileWriter(f.getAbsoluteFile(), true);
            BufferedWriter bw=new BufferedWriter(fw);
            bw.write(content);
            bw.newLine();
            bw.close();
            System.out.println("Done");
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }

}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            NewUserDialog dialog = new NewUserDialog(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration                   

public void setFName(String fn) {
    FirstName.equals(fn);
}

public String getFName() {
    return FirstName;
}

public void setLName(String ln) {
    LastName.equals(ln);
}

public String getLName() {
    return FirstName;
}

public void setRegNum(int reg_num) {
    Registration_Number=reg_num;
}

public int getRegNum() {
    return Registration_Number;
}

public void setDate(int reg_date) {
    Date=reg_date;
}

 public int getDate() {
   return Date;
}
}

my purpose is that when we file the jtextfile fields and clicked the button, this data should save into a .txt file . 我的目的是当我们提交jtextfile字段并单击按钮时,此数据应保存为.txt文件。 but both Fname and Lname did not save to file, just the "Date" file save to file. 但是Fname和Lname都没有保存到文件,只有“Date”文件保存到文件。 and i want that Fname and Lname and Data must store regular in text file. 我希望Fname和Lname以及Data必须在文本文件中定期存储。

thanks! 谢谢!

Your setter methods, setFName(...) and setLName(...) don't do any setting at all. 你的setter方法setFName(...)setLName(...)根本不做任何设置。 Instead all they do is perform an unnecessary and inexplicable test of equality and then discard the result: 相反,他们所做的只是执行不必要且无法解释的平等测试,然后丢弃结果:

public void setLName(String ln) {
    LastName.equals(ln); // ???????
}

How about instead creating true setter methods that set the object held by a reference variable: 相反,如何创建真正的setter方法来设置引用变量持有的对象:

public void setLName(String ln) {
    lastName = ln; // note variable names should begin w/ a lowercase letter
}

If you don't set these fields, then you can't expect the information from the getter methods to be useful when writing to file. 如果您没有设置这些字段,那么在写入文件时,您不能指望getter方法中的信息有用。

Also you have a weak catch block that does not inform you of the contents of the stack trace, information that may help you figure out what is wrong. 此外,您还有一个弱的catch块,它不会通知您堆栈跟踪的内容,这些信息可以帮助您找出问题所在。

Also, what purpose is there for your GUI to implement a non-GUI interface that seems better suited for a model class, not a view (GUI) class? 另外,你的GUI有什么用途来实现一个看起来更适合模型类而不是视图(GUI)类的非GUI界面? I would favor composition here instead of inheritance. 我赞成这里的作文而不是继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM