Java Swing希望在重新启动后保留单选按钮的选择。

huangapple go评论56阅读模式
英文:

Java Swing want to keep Radio button selection after Restart

问题

朋友们,

我是一个初学者,正试图开发一个系统级的 Java Swing 软件。

我在一个单选按钮组(bg)中有两个单选按钮,即 A 和 B。

我希望在重新启动后保持单选按钮的选择,直到进一步的选择为止。

在网络上搜索了很长时间,但只找到了 PHP、HTML 等的代码。

请有人帮帮我。

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

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

ButtonGroup bg = new ButtonGroup();
bg.add(rdbtA);
bg.add(rdbtB);
英文:

Friends

I am a beginner and trying to develop a system level java swing software.

I have 2 JRadio buttons viz A & B in a RadioButton group bg.

I want to keep the radio button selection after restart or until further selection.

Searched for this long in net but getting code for PHP,HTML etc.
Somebody please help me.

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

答案1

得分: 0

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

让我用代码解释一下:

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

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

/*
  1. 如果存在保存文件,则打开它(否则,忽略步骤 2 和 3)
  2. 读取之前 A 和 B 的值
  3. 将这些值设置给 rdbtA 和 rdbtB
*/

// 其余代码
英文:

When you start your application all the graphic components ar re-created, so you have to save the selection in some way when you close your program or when the selection changes (the easiest is to save your choice in a file) and restore it when the software is started (after the buttons creation).

Let me explain this in code:

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

huangapple
  • 本文由 发表于 2020年7月26日 14:57:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63097065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定