我有一個用戶密碼應用程序寫入文件 - 您在文本框中輸入信息,單擊一個按鈕,它應該寫入創建的文件的下一行。更新文本框的值
有兩個問題:
- 文件只寫數據
- 的一行它只是你進入最後寫道。我試過
validate()
和repaint()
但沒有什麼區別。
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();
}
}
驗證後,預計有什麼區別? – 2015-06-21 10:26:46
我看到它是爲了更新您輸入的文本 –
可能的重複[如何將文本附加到Java中的現有文件](http://stackoverflow.com/questions/1625234/how-to-append-text-to -an-existing-file-in-java) – Tom