我是java的新手,我正在编写发票计划,我想在txtContact,txtCompany,txtEmail,txtAddress和txtPhone和文本中保存用户输入,例如保存文件将具有Contact:txtContact并放入文本使用缓冲编写器的文件。
到目前为止,这是我的代码:
package InvoiceApp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.StringReader;
import javax.swing.JFileChooser;
/**
*
* @author dancastillo
*/
public class Customer extends javax.swing.JFrame {
/**
* Creates new form cus
*/
private String data;
JFileChooser chooser = new JFileChooser();
public Customer() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <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();
jLabel5 = new javax.swing.JLabel();
txtCompany = new javax.swing.JTextField();
txtContact = new javax.swing.JTextField();
txtAddress = new javax.swing.JTextField();
txtPhone = new javax.swing.JTextField();
txtEmail = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
btnReset = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Company:");
jLabel2.setText("Contact:");
jLabel3.setText("Address:");
jLabel4.setText("Phone:");
jLabel5.setText("E-Mail:");
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnReset.setText("Reset");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(evt);
}
});
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int retValue = chooser.showSaveDialog(null);
if(retValue == chooser.APPROVE_OPTION){
File f = chooser.getSelectedFile();
try{
BufferedReader reader = new BufferedReader(new StringReader(data));//data is string to save
BufferedWriter writer = new BufferedWriter(new FileWriter(f));
String str;
while((str = reader.readLine())!= null){
writer.write(str + System.getProperty("line.separator"));
}
}catch(Exception ex){
System.out.println("Error!");
}
}
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtCompany.setText("");
txtContact.setText("");
txtAddress.setText("");
txtPhone.setText("");
txtEmail.setText("");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Customer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Customer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Customer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Customer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Customer().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnExit;
private javax.swing.JButton btnReset;
private javax.swing.JButton btnSave;
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.JTextField txtAddress;
private javax.swing.JTextField txtCompany;
private javax.swing.JTextField txtContact;
private javax.swing.JTextField txtEmail;
private javax.swing.JTextField txtPhone;
// End of variables declaration
}
答案 0 :(得分:0)
在btnSaveActionPerformed
内部方法中,您可以将JTextField
组件的内容写入文件中,
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
...
writer.write(txtCompany.getText() + "," + txtContact.setText() + "," + txtAddress.setText() + "," + txtPhone.getText() + "," + txtEmail.setText() + System.getProperty("line.separator"));
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
resetForm();
}
});
...
}
private void resetForm() {
txtCompany.setText("");
txtContact.setText("");
txtAddress.setText("");
txtPhone.setText("");
txtEmail.setText("");
}