[英]Write data to file in a txt from a listener
我創建了一個 window 來接收用戶輸入的數據。 但我無法將該數據保存在 txt 文件中。 txt 文件有一個用戶列表,其中包含以下數據(姓名、姓氏、email 等),目的是向該文件添加新用戶。 使用的方法必須通過偵聽器。
我的項目結構如下:
window接口創建用戶:
public class buttonCreateUsersActionListener implements ActionListener {
private Window window;
public buttonCreateUsersActionListener () {
this.window = window;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
window 創建用戶(這里收集數據)
public class CreateUserWindow extends JFrame implements ActionListener {
private JLabel labelSubtitle, labelName, labelLastname, labelEmail, labelSex, labelStateCivil, lblFormatDate;
private static JTextField txtName;
private static JTextField txtLastname;
private static JTextField txtEmail;
private JTextField txtDate;
private JButton btnSave, btnCancel;
private Choice chStateCivil;
private final ButtonGroup buttonGroup = new ButtonGroup ();
publicWindowCreateUser () {
String userList = "";
components ();
setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);
setSize (400,450); // Window Size Width and Length
setLocationRelativeTo (null); // Center the window on the monitor
getContentPane (). setLayout (null);
setResizable (false); // prevents the window size from being modified
setVisible (true); // make the window visible
setTitle ("Create and add a user"); // Give the window a title
}
public void components () {
labelSubtitulo = new JLabel ();
labelSubtitulo.setFont (new Font ("Tahoma", Font.PLAIN, 13));
labelSubtitulo.setBounds (92, 20, 260, 20);
labelSubtitulo.setText ("Fill all the boxes");
getContentPane (). add (labelSubtitulo);
/ * --------- Fields --------- * /
labelName = new JLabel (); // label
labelName.setBounds (20, 50, 150,20);
labelName.setText ("Name");
getContentPane (). add (labelName);
txtName = new JTextField (); // box to fill
txtName.setBounds (165, 50, 215, 20);
getContentPane (). add (txtName);
labelLastname = new JLabel ();
labelLastname.setBounds (20, 80, 150, 20);
labelLastname.setText ("Lastname");
getContentPane (). add (labelLastname);
txtLastname = new JTextField ();
txtLastname.setBounds (165, 80, 215, 20);
getContentPane (). add (txtLastname);
labelEmail = new JLabel ();
labelEmail.setBounds (20, 110, 110, 20);
labelEmail.setText ("Email");
getContentPane (). add (labelEmail);
txtEmail = new JTextField ();
txtEmail.setBounds (165, 110, 215, 20);
getContentPane (). add (txtEmail);
labelSexo = new JLabel ();
labelSex.setBounds (20, 140, 50, 20);
labelSex.setText ("Sex");
getContentPane (). add (labelSex);
JRadioButton rdbtnMan = new JRadioButton ("Man");
buttonGroup.add (rdbtnMan);
rdbtnMan.setBounds (165, 139, 80, 25);
getContentPane (). add (rdbtnMan);
JRadioButton rdbtnMujer = new JRadioButton ("Female");
buttonGroup.add (rdbtnFemale);
rdbtnMujer.setBounds (249, 140, 69, 25);
getContentPane (). add (rdbtnFemale);
labelEstadoCivil = new JLabel ();
labelEstadoCivil.setBounds (20, 170, 80, 20);
labelEstadoCivil.setText ("Civil Status");
getContentPane (). add (labelStateCivil);
chEstadoCivil = new Choice ();
chEstadoCivil.setBounds (165, 168, 110, 20);
chEstadoCivil.add ("Single");
chEstadoCivil.add ("Married");
chEstadoCivil.add ("Divorced");
chEstadoCivil.add ("Widower");
getContentPane (). add (chStateCivil);
JLabel birthdate = new JLabel ("Date of birth");
birthdate.setBounds (20, 203, 128, 16);
getContentPane (). add (birthdate);
txtDate = new JTextField ();
txtDate.setBounds (165, 203, 110, 22);
getContentPane (). add (txtDate);
txtDate.setColumns (10);
lblFormatDate = new JLabel ("[format DD / MM / YYYY]");
lblFormatDate.setFont (new Font ("Tahoma", Font.PLAIN, 11));
lblFormatDate.setBounds (20, 219, 138, 16);
getContentPane (). add (lblFormatDate);
/*---------- Buttons ----------*/
btnSave = new JButton ();
btnSave.setBounds (100, 350, 100, 20);
btnSave.setText ("Save");
btnSave.addActionListener (this);
getContentPane (). add (btnSave);
btnCancel = new JButton ();
btnCancelar.setBounds (220, 350, 100, 20);
btnCancelar.setText ("Cancel");
btnCancelar.addActionListener (this);
getContentPane (). add (btnCancelar);
}
@Override
public void actionPerformed (ActionEvent e) {
if (e.getSource (). equals (btnSave)) {// We launch a routine to write the data
this.dispose ();
User user = null;
Window.listUsers.add (student);
}
}
public String getName () {
return txtName.getText ();
}
public static String getLastname () {
return txtLastname.getText ();
}
public static String getEmail () {
return txtEmail.getText ();
}
public String getSex () {
return labelSexo.getText ();
}
public String getStateCivil () {
return labelCivilState.getText ();
}
public String getBirthDate () {
return lblFormatDate.getText ();
}
}
這將為您編寫一個文件,前提是您可以獲得要寫入文件的字符串,並且您可以獲得文件的路徑/名稱。
@Override
public void actionPerformed(ActionEvent e) {
String content = getContent(); //You said you know how to do this.
try( BufferedWriter writer = Files.newBufferedWriter(
Paths.get( filename ),
StandardOpenOption.CREATE,
StandardOpenOption.APPEND );){
writer.write(content);
} catch (IOException e){
//You might want to do something useful here.
throw new RuntimeException(e);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.