简体   繁体   English

我如何让我的 JRadioButton 得到一个问题,我让我的代码从数据库中获取

[英]How do i make my JRadioButton get a question that i made my code get from the database

im trying to make a game where i get questions from the database and the JRadioButton shows these questions.我正在尝试制作一个游戏,让我从数据库中获取问题,而 JRadioButton 会显示这些问题。 My problem is, how do i define the JRadioButton?我的问题是,我如何定义 JRadioButton?

i can code to get the answers from the database but how do i set up my JRadioButton.我可以编写代码以从数据库中获取答案,但是如何设置我的 JRadioButton。 Im learning to code any tips would be appreciated我正在学习编码任何提示将不胜感激

You must know how to fetch data from the database since you say you know how to get the answers from the database, so let's assume you've retrieved a question that you want to apply to your radio button.您必须知道如何从数据库中获取数据,因为您说您知道如何从数据库中获取答案,因此假设您已检索到要应用于单选按钮的问题。 We'll call it myQuestion .我们称之为myQuestion

JRadioButton has a constructor which takes a String and a boolean as arguments, representing the String that the radio button should be displayed, and whether the radio button should be selected. JRadioButton 有一个构造函数,它接受一个 String 和一个 boolean 作为 arguments,代表应该显示单选按钮的字符串,以及是否应该选择单选按钮。

JRadioButton myRadioButton = new JRadioButton(myQuestion, false);

This gives you an unselected radio button with your question as text.这为您提供了一个未选中的单选按钮,其中您的问题作为文本。

Now what if you want to change your question at some point?现在如果你想在某个时候改变你的问题怎么办? You can use JRadioButton's setText() method to change the radio button's text at any time.您可以随时使用 JRadioButton 的 setText() 方法更改单选按钮的文本。

myRadioButton.setText(myNewQuestion);

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

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