import java.io.*;public class TextFileWriter {
PrintWriter out;

public TextFileWriter(String fileName,boolean append)throws IOException{
            out = new PrintWriter(
             new BufferedWriter(
             new FileWriter(fileName,append)));
} public final void output(String...text){
              
               for (String s: text)
                   out.print(s + "|");
out.println();
}

public final void closeFile(){
out.close();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;public class ProductTextFileWriterFrame extends Frame{
ProductTextFileWriterFrame(){
setTitle("产品文本文件输入程序");
centerWindow(this);
setSize(300,220);
setResizable(false);
setDefaultCloseOeration(JFrame.EXIT_ON_CLOSE);
JPanel panel = new ProductTextFileWriterPanel();
this.add(panel);
}

private void centerWindow(Window w){
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
setLocation((d.width-w.getWidth()) / 2,(d.height-w.getHeight() / 2)); 
}

class ProductTextFileWriterPanel extends JPanel implements ActionListener{

private JTextField  IDTextField,
                    titleTextField,
                    priceTextField,
                    infoTextField;
private JLabel      IDLabel,
                    titleLabel,
                    priceLabel,
                    infoLabel,
                    countLabel;
private JButton     saveButton,
                    exitButton;

private TextFileWriter fileWriter;
private String fileName = "/home/cactus/桌面/临时文件/test/product.txt";
private int count;

public ProductTextFileWriterPanel(){

JPanel productFilePanel = new JPanel();
productFilePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

IDLabel = new JLabel("产品编号");
productFilePanel.add(IDLabel);

IDTextField = new JTextField(20);
productFilePanel.add(IDTextField);

titleLabel = new JLabel("产品名");
productFilePanel.add(titleLabel);

titleTextField = new JTextField(20);
productFilePanel.add(titleTextField);

priceLabel = new JLabel("产品名");
productFilePanel.add(priceLabel);

priceTextField = new JTextField(20);
productFilePanel.add(priceTextField);

infoLabel = new JLabel("数据存储至");
productFilePanel.add(infoLabel);
infoLabel.setVisible(false);

infoTextField = new JTextField(20);
infoTextField.setEditable(false);
infoTextField.setFocusable(false);
productFilePanel.add(infoTextField);
infoTextField.setVisible(false);

countLabel = new JLabel();
productFilePanel.add(countLabel);

//button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

saveButton = new JButton("保存");
    saveButton.addActionListener(this);
buttonPanel.add(saveButton);

exitButton = new JButton("退出");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);

    this.setLayout(new BorderLayout());
    this.add(productFilePanel,BorderLayout.CENTER);
    this.add(buttonPanel,BorderLayout.NORTH);
    try{
     fileWriter = new TextFileWriter(fileName,true);
    
    }
    catch (IOException e){
     System.out.println(e);
    }     count = 0;
}

public void actionPerformed(ActionEvent e){

if (e.getSource() == exitButton){
fileWriter.closeFile();
System.exit(0);
}
else if (e.getSource() == saveButton){
String ID = IDTextField.getText();
String title= titleTextField.getText();
String price = priceTextField.getText();
fileWriter.output(ID,title,price);

IDTextField.setText("");
titleTextField.setText("");
priceTextField.setText("");

infoLabel.setVisible(true);
infoTextField.setVisible(true);
infoTextField.setText(fileName);
countLabel.setText("已存记录:                    " + ++count );
}
}
} }setDefaultCloseOpration();方法提示错误,怎么改。

解决方案 »

  1.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;public class ProductTextFileWriterFrame extends JFrame{
        ProductTextFileWriterFrame(){
            setTitle("产品文本文件输入程序");
            centerWindow(this);
            setSize(300,220);
            setResizable(false);
            //setDefaultCloseOeration(JFrame.EXIT_ON_CLOSE);
            //首先要继承JFrame,而不是Frame;其次是方法名要写正确,你自己看看!
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new ProductTextFileWriterPanel();
            this.add(panel);
        }
        
        private void centerWindow(Window w){
            Toolkit tk=Toolkit.getDefaultToolkit();
            Dimension d=tk.getScreenSize();
            setLocation((d.width-w.getWidth()) / 2,(d.height-w.getHeight() / 2)); 
        }
        
        class ProductTextFileWriterPanel extends JPanel implements ActionListener{
            
            private JTextField  IDTextField,
                                titleTextField,
                                priceTextField,
                                infoTextField;
            private JLabel      IDLabel,
                                titleLabel,
                                priceLabel,
                                infoLabel,
                                countLabel;
            private JButton     saveButton,
                                exitButton;
            
            private TextFileWriter fileWriter;
            private String fileName = "/home/cactus/桌面/临时文件/test/product.txt";
            private int count;
            
            public ProductTextFileWriterPanel(){
                
                JPanel productFilePanel = new JPanel();
                productFilePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
                
                IDLabel = new JLabel("产品编号");
                productFilePanel.add(IDLabel);
                
                IDTextField = new JTextField(20);
                productFilePanel.add(IDTextField);
            
                titleLabel = new JLabel("产品名");
                productFilePanel.add(titleLabel);
                
                titleTextField = new JTextField(20);
                productFilePanel.add(titleTextField);
                
                priceLabel = new JLabel("产品名");
                productFilePanel.add(priceLabel);
                
                priceTextField = new JTextField(20);
                productFilePanel.add(priceTextField);
                
                infoLabel = new JLabel("数据存储至");
                productFilePanel.add(infoLabel);
                infoLabel.setVisible(false);
                
                infoTextField = new JTextField(20);
                infoTextField.setEditable(false);
                infoTextField.setFocusable(false);
                productFilePanel.add(infoTextField);
                infoTextField.setVisible(false);
                
                countLabel = new JLabel();
                productFilePanel.add(countLabel);
        
                //button panel
                JPanel buttonPanel = new JPanel();
                buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
                
                saveButton = new JButton("保存");
                saveButton.addActionListener(this);
                buttonPanel.add(saveButton);
                
                exitButton = new JButton("退出");
                exitButton.addActionListener(this);
                buttonPanel.add(exitButton);
                
                this.setLayout(new BorderLayout());
                this.add(productFilePanel,BorderLayout.CENTER);
                this.add(buttonPanel,BorderLayout.NORTH);
                try{
                    fileWriter = new TextFileWriter(fileName,true);
                    
                }
                catch (IOException e){
                    System.out.println(e);
                }            count = 0;
            }
            
            public void actionPerformed(ActionEvent e){
                
                if (e.getSource() == exitButton){
                    fileWriter.closeFile();
                    System.exit(0);
                }
                else if (e.getSource() == saveButton){
                    String ID = IDTextField.getText();
                    String title= titleTextField.getText();
                    String price = priceTextField.getText();
                    fileWriter.output(ID,title,price);
                    
                    IDTextField.setText("");
                    titleTextField.setText("");
                    priceTextField.setText("");
                    
                    infoLabel.setVisible(true);
                    infoTextField.setVisible(true);
                    infoTextField.setText(fileName);
                    countLabel.setText("已存记录:                    " + ++count );
                }
            }
        } }程序中注释了!