将我的ComboBox值写入文件

时间:2016-12-14 17:45:57

标签: java combobox

我遇到了这个问题,我将comboBox中的文本保存到文本变量中,但现在我无法将其保存到文件中,例如'num1'和'num2'后点击一个按钮。我知道我错过了一些简单的事情 - 或者一切都是错的,请帮忙!谢谢!

package windowbuilded.views;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class WiewWindow {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WiewWindow window = new WiewWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public WiewWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                int num1, num2, ans2, combo;
                num1=Integer.parseInt(textField.getText());
                num2=Integer.parseInt(textField_1.getText());
                ans2 = num1 + num2;
                textField_2.setText(Integer.toString(ans2));
                try{
                    File dir = new File("C:\\test");
                     File[] directoryListing = dir.listFiles();
                     if (directoryListing != null) {
                            for (File child : directoryListing) {
                    FileWriter writer = new FileWriter(child, true);
                    PrintWriter out = new PrintWriter(writer);
                    out.println(num1);
                    out.println(num2);
                    out.close();
                            }
                     }
                } catch (IOException e) {
                   // do something
                }
            }
        });
        btnNewButton.setBounds(124, 206, 89, 23);
        frame.getContentPane().add(btnNewButton);

        textField = new JTextField();
        textField.setBounds(10, 34, 86, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(124, 34, 86, 20);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setBounds(124, 101, 86, 20);
        frame.getContentPane().add(textField_2);
        textField_2.setColumns(10);

        JComboBox<String> comboBox = new JComboBox<String>();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Yes", "No", "Blah!"}));
        String text = (String)comboBox.getSelectedItem();
        System.out.println(text);
        comboBox.setBounds(265, 101, 121, 20);
        frame.getContentPane().add(comboBox);
    }
}

0 个答案:

没有答案