更新文本字段值

时间:2015-06-21 10:23:20

标签: java

我有一个写入文件的用户密码应用程序 - 您在文本字段中输入信息,单击一个按钮,它应该写入创建的文件的下一行。

有两个问题:

  1. 该文件只写一行数据
  2. 它只会写入您最后输入的内容。我尝试了validate()repaint(),但没有区别。
  3. public class Main {
    
    public JFrame frame;
    public JTextField textField;
    public JTextField textField_1;
    public JTextField textField_2;
    public JTextField textField_3;
    
    public static void main(String[] args) {
    
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main window = new Main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    
    
    }
    
    public Main() {
        initialize();
    }
    
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 588, 156);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
    
        JButton btnNewButton = new JButton("Add Record");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                openFile();
                printFormat();
                addRecords();
                closeFile();
    
            }
        });
        btnNewButton.setBounds(237, 83, 91, 23);
        frame.getContentPane().add(btnNewButton);
    
        textField = new JTextField();
        textField.setColumns(10);
        textField.setBounds(10, 44, 128, 20);
        frame.getContentPane().add(textField);
    
        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(148, 44, 128, 20);
        frame.getContentPane().add(textField_1);
    
        textField_2 = new JTextField();
        textField_2.setFont(UIManager.getFont("PasswordField.font"));
        textField_2.setColumns(10);
        textField_2.setBounds(286, 44, 128, 20);
        frame.getContentPane().add(textField_2);
    
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(424, 44, 128, 20);
        frame.getContentPane().add(textField_3);
    
        JLabel lblNewLabel = new JLabel("Username");
        lblNewLabel.setBounds(10, 19, 128, 14);
        frame.getContentPane().add(lblNewLabel);
    
        JLabel lblPassword = new JLabel("Email");
        lblPassword.setBounds(148, 19, 128, 14);
        frame.getContentPane().add(lblPassword);
    
        JLabel lblMojangPassword = new JLabel("Mojang Password");
        lblMojangPassword.setBounds(286, 19, 128, 14);
        frame.getContentPane().add(lblMojangPassword);
    
        JLabel lblEmailPassword = new JLabel("Email Password");
        lblEmailPassword.setBounds(424, 19, 128, 14);
        frame.getContentPane().add(lblEmailPassword);
    
    }
    
    
    private Formatter x;
    
    public void openFile(){
        try{
            x = new Formatter("okey.fil");
    
        }
        catch(Exception e){
            System.out.println("error get it right");
        }
    }
    public void printFormat(){
        x.format("%-25s %-25s %-25s %-25s \n", "Username", "Email", "Mojang Password", "Email Password");
    }
    public String getUsername(){
        String username = textField.getText();
        return username;
    }
    public void addRecords(){
            x.format("%-25s %-25s %-25s %-25s \n", getUsername(),textField_1.getText(),textField_2.getText(),textField_3.getText());
            x.format("\n"); 
            resetFields();
        }
    
    
    public void resetFields(){
        textField.setText("");
        textField_1.setText("");
        textField_2.setText("");
        textField_3.setText("");
        textField.validate();
        textField_1.validate();
        textField_2.validate();
        textField_3.validate();
    }
    public void closeFile(){
        x.close();
    }
    }
    

2 个答案:

答案 0 :(得分:1)

问题如下:每次操作" btnNewButton"执行 - 文件由java.lang.Formatter打开" w"模式,这意味着它被截断,并且在开头只写入新行。实际上,您将每行写入文件,但只留下最后一行。

解决方案1:您必须只打开一次Formatter(或FileOutputStream或类似的东西) - 并在那里添加行。您可以调用flush()而不是close()来实际将数据写入文件。

解决方案2:如果您需要,您仍然可以在每次操作时重新打开文件 - 但是您必须打开它"追加"模式 - How to append text to an existing file in Java

例如,您可以使用此构造函数(第二个参数必须为true)http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#FileOutputStream%28java.io.File,%20boolean%29

答案 1 :(得分:0)

您可以使用

x = new Formatter(new FileOutputStream("okey.fil",true));