Java无法在.text中编写

时间:2011-01-03 21:26:25

标签: java printing combobox text-files

你好,我迫切需要你的帮助,我有一个带有jcombobox和3个文本字段的jframe我想要在textfields中写的任何东西,我在组合框中做出的选择是用.txt编写的,我试过很多东西但是没有,该文件被创建为Orders.txt但仍然是空白的:S这是我的代码我感谢任何帮助谢谢:)

public class addSalesMan extends JFrame {
    private JComboBox namesJComboBox;
    private JTextField text1;//gia to poso
    private JTextField text2;//thn perigrafh
    private JTextField text3;//kai to numero ths paragelias kai ola auta tha egrafontai sto Orders.txt
    private JButton okJbutton;
    private String names[] = {"Basilis Komnhnos", "Iwanna Papadhmhtriou"};
   public String amount,name,description,number;

    public addSalesMan() {
        super("Προσθήκη παραγγελιών");
        setLayout(new FlowLayout());//dialegoume to flowlayout
//        TextFieldHandler handler = new TextFieldHandler(); writer.write(string);



            //ftiaxonoume to combobox gia tn epilogi tou onomatos
        namesJComboBox = new JComboBox(names);//orizmos JCOMBO BOX
        namesJComboBox.setMaximumRowCount(2);//na emfanizei 2 grammes
                     add(namesJComboBox);
             namesJComboBox.addItemListener(new ItemListener() {
            //xeirozome to simvan edw dhladh tn kataxwrisei ston fakelo         
           public void itemStateChanged(ItemEvent event) {
//prosdiorizoyme an eina epilegmeno to plaisio elegxou
                if (event.getStateChange() == ItemEvent.SELECTED) {                 

                            name = (names[namesJComboBox.getSelectedIndex()]);

//                            writer.newLine();             
                 setVisible(true);
            }
                 }
        });   //telos touComboBOx


        //dimioutgw pediou keimenou me 10 sthles gia thn kathe epilogh kai veveonomaste oti tha mporoume na ta epe3ergasoume kanontas ta editable

        text1 = new JTextField("Amount",10);
add(text1);
        text2 = new JTextField("Description",10);
add(text2);
        text3 = new JTextField("Order Number",10);
add(text3);

        TextFieldHandler handler = new TextFieldHandler();
        text1.addActionListener(handler);
        text2.addActionListener(handler);
        text3.addActionListener(handler);

        //private eswterikh clash gia ton xeirismo twn events twn text
//button kataxwrisis
 okJbutton=new JButton("Καταχώρηση");
 add(okJbutton);
 ButtonHandler bhandler=new ButtonHandler();
 okJbutton.addActionListener(bhandler);

    Order order=new Order(name,amount,description,number);
      Order.addOrders(name,amount,description,number);

    }
    private class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent bevent ){

             JOptionPane.showMessageDialog(addSalesMan.this,String.format("Η Καταχωρηση ήταν επιτυχής",bevent.getActionCommand()));


        }
    }
       private class TextFieldHandler implements ActionListener {
            //epe3ergasia twn simvantwn me kathe enter t xrhsth

            public void actionPerformed(ActionEvent evt) {
                String amount,description,number;
                amount=text1.getText();
                description=text2.getText();
                number=text3.getText();
                text1.selectAll();
                text2.selectAll();
                text3.selectAll();

              }
      if(evt.getSource()==text1 && evt.getSource()==text2 && evt.getSource()==text3){
        JOptionPane.showMessageDialog(addSalesMan.this,String.format("Η Καταχωρηση ήταν επιτυχής",evt.getActionCommand()));

           }
      }

//actionperformed telos
            //ean o xrhsths patisei enter sthn kathe epilogh antistixi kataxwrisi sto

            }


          public static void main(String args[]) {
       java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        new addSalesMan().setVisible(true);
            }
 });
      }
    }

作家在另一个班级。以下是相关代码:

public static void addOrders(String name,String amount,String description,String o_number){
    FileOutputStream fout;
    try {
        FileWriter fstream = new FileWriter("Orders.txt");
        if(name!=null){
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(name);
            out.write(amount);
            out.write(description);
            out.write(o_number);
            out.write("\t\n");
            out.close();
        }
    } catch (IOException e) {
        System.err.println ("Unable to write to file");
        System.exit(-1);
    }
}

1 个答案:

答案 0 :(得分:1)

看起来主要的问题是你在构造函数中调用了Order.addOrders()。相反,您应该在用户选择保存它的选择时调用它。我假设您希望在用户按下按钮时发生这种情况。因此,代码应添加到按钮的ActionListener

当用户关闭框架时,您可能需要尝试刷新和关闭编写器。

将以下内容添加到框架的构造函数中:

addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
        writer.flush();
        writer.close();
    }
});

当用户关闭框架时,上面的代码将刷新并关闭编写器。

您的代码不清楚,所以我不确定writer变量的声明位置,我只是假设它是一个类级变量。

此外,如果要在文件中添加行而不是每次都覆盖它,则需要以“追加”模式打开文件。这可以通过以下方式实现:

new FileWriter(yourFilePath, true); // set append to true