首页 > 解决方案 > Java Swing希望在重新启动后保留单选按钮选择

问题描述

朋友们

我是一个初学者,正在尝试开发一个系统级的 java swing 软件。

我在 RadioButton 组 bg 中有 2 个 JRadio 按钮,即 A 和 B。

我想在重新启动后或进一步选择之前保留单选按钮选择。

在网上搜索了这么长时间,但得到了 PHP、HTML 等的代码。有人请帮助我。

rdbtA = new JRadioButton("A");
        contentPane.add(rdbtA);
        
        rdbtB = new JRadioButton("B");
        contentPane.add(rdbtB);
        
        ButtonGroup bg = new ButtonGroup();
        bg.add(A);
        bg.add(B);
        

标签: javaswingradio-buttonselection

解决方案


当您启动应用程序时,所有图形组件都会重新创建,因此您必须在关闭程序或选择更改时以某种方式保存选择(最简单的方法是您的选择保存在文件中)并在软件启动(按钮创建后)。

让我用代码解释一下:

rdbtA = new JRadioButton("A");
contentPane.add(rdbtA);

rdbtB = new JRadioButton("B");
contentPane.add(rdbtB);

/*
 1. If exists a save-file, open it (else, ignore 2. and 3.)
 2. read the value of previous A and previous B
 3. Set these values to rdbtA and rdbtB
*/

//Rest of the code

推荐阅读