如何在 java 中保存和加载复选框状态和文本字段值?

How to save and load checkBox states and text Field values in java?

我一直想知道如何在 Java 中保存和加载 jcheckbox 和 jtextfields 值。是否可以保存和加载 jcheckbox 和 jtextfields 值? 我附上了一个小程序的图片,我想在其中保存复选框的值并加载它们以供稍后查看。我是否使用 JFileChooser 来执行操作?

   public class Try extends JFrame {
   private JPanel contentPane;
   private JCheckBox chckbx_1;
private JCheckBox chckbx_2;
private JTextField textField;
private JTextField textField_1;

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

/**
 * Create the frame.
 */
public Try() {
    setTitle("Price tag");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    chckbx_1 = new JCheckBox("Price");
    chckbx_1.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange() == ItemEvent.SELECTED){
                textField.setEnabled(true);
                textField.setText("");
            }
            else if(e.getStateChange() == ItemEvent.DESELECTED){
                textField.setEnabled(false);
                textField.setText("");
        }
        }
    });

    chckbx_1.setBounds(72, 7, 81, 41);
    contentPane.add(chckbx_1);

    chckbx_2 = new JCheckBox("New Price");
    chckbx_2.addItemListener(new ItemListener() {
         @Override
        public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange() == ItemEvent.SELECTED){
                textField.setEnabled(true);
                textField.setText("");
            }
            else if(e.getStateChange() == ItemEvent.DESELECTED){
                textField_1.setEnabled(false);
                textField_1.setText("");
        }
        }
    });
    chckbx_2.setBounds(72, 51, 89, 50);
    contentPane.add(chckbx_2);

    textField = new JTextField();
    textField.setBounds(196, 17, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setBounds(196, 66, 86, 20);
    contentPane.add(textField_1);
    textField_1.setColumns(10);

    JButton btn1 = new JButton("Save");
    btn1.setBounds(193, 173, 89, 23);
    contentPane.add(btn1);

    JButton btn2 = new JButton("Load");
    btn2.setBounds(314, 173, 89, 23);
    contentPane.add(btn2);
}}

您可以通过将 CheckBox 选定的索引和文本字段保存到文件(例如 .txt)并格式化为

来做到这一点

StateCheckBox1|2|StateTextField1|示例

然后,当你阅读它时,你将整行并使用 .split() 方法,将其保存到一个字符串数组中。

当然是用数据库更好,但是如果你只想保存几个状态,就没必要用了。

否则如果你不关闭你的应用程序,并且想要加载你的组件的以前状态,你可以制作 Object 例如。 StateHolder 哪个持有你的状态。

如果您需要有关如何制作此 .txt 的更多说明,请询问。我会尽力向你解释。

您应该使用的变量是:

 private FileWriter fw;
 private PrintWriter pw;
 private BufferedReader br;

要保存复选框状态和文本字段值,您可以使用 FileWriterPrintWriter 保存在文件中:

btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String desktopPath = System.getProperty("user.home") + "/Desktop/";
                try {
                    fw = new FileWriter(desktopPath + "yourfile.txt");
                } catch (IOException ex) {
                    System.out.println("Error : " + ex);
                }
                pw = new PrintWriter(fw);
                pw.println(chckbx_1.isSelected() + "-" + textField.getText() + "-" + chckbx_2.isSelected()
                        + " -" + textField_1.getText());
                if (pw != null) {
                    pw.close();
                }
            }
        });

为了从文件中读取,您可以使用 BufferedReader 并使用 StringTokenizer 从文件中拆分读取行:

btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String desktopPath = System.getProperty("user.home") + "/Desktop/";
                try {
                    String sCurrentLine;
                    br = new BufferedReader(new FileReader(desktopPath + "yourfile.txt"));
                    while ((sCurrentLine = br.readLine()) != null) {
                        StringTokenizer tokenizer = new StringTokenizer(sCurrentLine, "-");
                        if (tokenizer.hasMoreElements()) {
                             Boolean boolean1 = Boolean.valueOf(tokenizer.nextToken());
                             chckbx_1.setSelected(boolean1);
                        }
                        if (tokenizer.hasMoreElements()) {
                            textField.setText(tokenizer.nextToken());
                        }
                        if (tokenizer.hasMoreElements()) {
                             Boolean boolean1 = Boolean.valueOf(tokenizer.nextToken());
                             chckbx_2.setSelected(boolean1);
                        }
                        if (tokenizer.hasMoreElements()) {
                            textField_1.setText(tokenizer.nextToken());
                        }

                    }
                } catch (FileNotFoundException ex) {
                    System.out.println("Error : " + ex);
                } catch (IOException ex) {
                    System.out.println("Error : " + ex);
                }
            }
        });

如果你想从多文件中读取,你可以使用 JFileChooser 比如:

    btn2.addActionListener(new ActionListener() {
                @Override
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser fc = new JFileChooser();
                        int ret = fc.showOpenDialog(null);
                        if (ret == JFileChooser.APPROVE_OPTION) {
                            File file = fc.getSelectedFile();
                             filename = file.getAbsolutePath();
                        }
....

然后,放入读取代码:

br = new BufferedReader(new FileReader(filename));