我正在尝试从 B 类编写一个 CSV 文件。但是,jTextField(在 A 类中)只是没有输出到创建的 CSV 文件。我需要这方面的帮助/步骤/提示。
编辑:这现在符合。
A级
public class ClassA extends javax.swing.JFrame {
public ClassA() {
initComponents();
}
private void initComponents() {
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
jTextField1 = new JTextField();
JMenu aFile = new JMenu("File");
JMenuItem saveFile = new JMenuItem ("Save...");
menuBar.add(aFile);
aFile.add(saveFile);
saveFile.addActionListener(new java.awt.event.ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ClassB ss = new ClassB();
try {
ss.saveAsTable();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClassA().setVisible(true);
}
});
}
protected static javax.swing.JTextField jTextField1;
}
B类
public class ClassB extends ClassA{
/**
*
*/
public ClassB() {}
public void saveAsTable() throws IOException {
JFileChooser sat = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"CSV files (*csv)", "csv");
sat.setFileFilter(filter);
int ret = sat.showSaveDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
File f = sat.getSelectedFile();
FileWriter out = new FileWriter(f);
out.write(jTextField1.getText() + ",");//This doesn't work...
out.close();
}
}