如何在属性文件中保存复选框状态

时间:2018-10-12 09:11:28

标签: java properties

首先,请不要关闭此线程,我一直在网上搜索这个问题,没有好消息。

我正在尝试在属性文件中保存JCheckBox状态。但是我只是不知道如何。

这是我的代码:

public class Main {

static optionsframe funct = new optionsframe();
static JCheckBox cb = funct.cbCloseWindow;

public void Main(String args[]) throws Exception {
Map<String, Boolean> result = new HashMap<>();
result.put(cb.getName(), cb.isSelected());

FileInputStream in = new FileInputStream("options.properties");
Properties props = new Properties();
props.load(in);
in.close();

FileInputStream out = new FileInputStream("options.properties");
props.setProperty("windows", result);
props.store(out, null);
in.close();
}}

static optionsframe funct = new optionsframe(); static JCheckBox cb = funct.cbCloseWindow;

我怎么称呼我的复选框所在的班级。

和下面的内容是我从某个论坛上获得的随机信息(我什至不知道它是否有效)。

1 个答案:

答案 0 :(得分:0)

public class ScannerMain implements Runnable {

    private JFrame frame;


    @Override
    public void run() {
        // ==============LOAD PROPERTIES=======================
        FileInputStream in1 = null;
        boolean chkA = false;
        boolean chkB = false;

        try {
            in1 = new FileInputStream("options.properties");
            Properties props = new Properties();
            props.load(in1);
            in1.close();
            chkA = Boolean.valueOf(props.getProperty("ckA", "false"));
            chkB = Boolean.valueOf(props.getProperty("ckB", "false"));

        } catch (IOException e) {
            e.printStackTrace();
        }

        // =====================================
        frame = new JFrame("Check Box Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        JPanel checkBoxPanel = new JPanel();
        JCheckBox exchangingCard1 = new JCheckBox("A");
        exchangingCard1.setSelected(chkA);
        checkBoxPanel.add(exchangingCard1);
        JCheckBox exchangingCard2 = new JCheckBox("B");
        checkBoxPanel.add(exchangingCard2);
        exchangingCard2.setSelected(chkB);
        mainPanel.add(checkBoxPanel);
        JButton save = new JButton("Save");
        checkBoxPanel.add(save);


        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);


        // ==============SAVE PROPERTIES=======================
        save.addActionListener(actionEvent -> {
            FileInputStream in = null;
            try {
                in = new FileInputStream("options.properties");
                Properties props = new Properties();
                props.load(in);
                in.close();
                props.setProperty("ckA", String.valueOf(exchangingCard1.isSelected()));
                props.setProperty("ckB", String.valueOf(exchangingCard2.isSelected()));
                FileWriter out = new FileWriter("options.properties");
                props.store(out, null);
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        // =====================================


    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ScannerMain());
    }
}

您可以检查LOAD PROPERTIES和“保存属性”部分中的逻辑。 希望对您有帮助。