【发布时间】:2014-03-27 16:35:11
【问题描述】:
我是 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
}
【问题讨论】:
-
太好了,你的问题是什么?你必须更具体...
标签: java swing jframe bufferedreader bufferedwriter