从复选框,组合框和文本字段到txt文件

时间:2014-12-28 07:34:50

标签: java swing

假设我有这个代码,它将“主题”添加到充当组织者的应用程序中。如何编写用户在txt文件中输入的内容,如果可能需要进行某些编辑,将再次读取该内容以编辑原始输入。我试着搜索如何将数据存入文件,但我失败了。我希望你能帮助我。谢谢。

    addSubject = new JPanel();

    contPanel.add(addSubject, "addSubject");

    JLabel lblSubject = new JLabel("SUBJECT");

    JLabel lblTime = new JLabel("TIME");

    JLabel lblDays = new JLabel("DAYS");

    subTxt = new JTextField();
    subTxt.setColumns(10);

    JComboBox thirdTime = new JComboBox();
    thirdTime.addItem("AM");
    thirdTime.addItem("PM");

    JComboBox secondTime = new JComboBox();
    for(int i=0; i<=59; i++){
        if(i<10)
    secondTime.addItem("0"+i);
        else
            secondTime.addItem(i);
    }

    JComboBox firstTime = new JComboBox();
    for(int i=0; i<=12; i++){
    firstTime.addItem(i);
    }

    subConfButton = new JButton("CONFIRM");
    subConfButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int[]x = {};    
            subj.add(new Subject(subTxt.getText(), x, "nadf"));
            GridBagConstraints cons= new GridBagConstraints();
            cons.insets = new Insets(0, 0, 0, 0);
            cons.gridx = 0;
            cons.gridy = subj.size();

            subjTbModel.addRow(new Object[]{subTxt.getText()});
            subjectCombo.addItem(subTxt.getText()+ "");
            subTxt.setText("");
            cards.show(contPanel, "mainPanel");
        }

    });

    JButton cancelAdd = new JButton("CANCEL");
    cancelAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cards.show(contPanel, "mainPanel");
            subTxt.setText("");
        }
    });

    JCheckBox checkTuesday = new JCheckBox("TUESDAY");

    JCheckBox checkWed = new JCheckBox("WEDNESDAY");

    JCheckBox checkThurs = new JCheckBox("THURSDAY");

    JCheckBox checkFriday = new JCheckBox("FRIDAY");

    JCheckBox checkSat = new JCheckBox("SATURDAY");

    JCheckBox checkMonday = new JCheckBox("MONDAY");

    JCheckBox checkBox = new JCheckBox("SUNDAY");

    JComboBox fourthTime = new JComboBox();
    for(int i=0; i<=12; i++){
        fourthTime.addItem(i);
        }

    JComboBox fifthTime = new JComboBox();
    for(int i=0; i<=59; i++){
        if(i<10)
            fifthTime.addItem("0"+i);
                else
        fifthTime.addItem(i);
        }

    JComboBox sixthTime = new JComboBox();
    sixthTime.addItem("AM");
    sixthTime.addItem("PM");

    JLabel lblTo = new JLabel("TO");

1 个答案:

答案 0 :(得分:1)

有很多选择它并不好笑......

你可以

使用Properties存储key = value对,提供storeload方法,以便于读取和写入

你可以

使用允许存储分层数据的XML文件,虽然比Properties更灵活,但会增加复杂性

有关详细信息,请参阅Java API for XML Processing (JAXP)

您还可以使用JAXB(Java Architecture for XML Binding),它可用于在基于XML的结构中存储对象属性。请查看Introduction to JAXB了解更多详情......

你可以

只需滚动自己,将文本写入您想要的任何格式的文件。有关详细信息,请参阅Basic I/O