繁体   English   中英

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

[英]How to save a checkbox state in properties file

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

我正在尝试在属性文件中保存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; static optionsframe funct = new optionsframe(); static JCheckBox cb = funct.cbCloseWindow;

这就是我称呼我的Checkbox所在的班级的方式。

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

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和“保存属性”部分中检查逻辑。 希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM