【问题标题】:How do I make my JRadioButton get a question from the database?如何让我的 JRadioButton 从数据库中获取问题?
【发布时间】:2021-01-27 18:00:24
【问题描述】:

我正在尝试制作一个游戏,从数据库中获取问题,JRadioButton 会显示这些问题。

如何定义单选按钮?

我可以编写代码以从数据库中获取答案,但如何设置我的单选按钮?

【问题讨论】:

    标签: java sql-server string swing jradiobutton


    【解决方案1】:

    您必须知道如何从数据库中获取数据,因为您说您知道如何从数据库中获取答案,因此假设您已检索到一个要应用于单选按钮的问题。我们称之为myQuestion

    JRadioButton 有一个constructor,它接受一个字符串和一个布尔值作为参数,表示应该显示单选按钮的字符串,以及是否应该选择单选按钮。

    JRadioButton myRadioButton = new JRadioButton(myQuestion, false);
    

    这会为您提供一个未选中的单选按钮,其中包含您的问题文本。

    现在,如果您想在某个时候更改您的问题怎么办?您可以随时使用 JRadioButton 的 setText() 方法更改单选按钮的文本。

    myRadioButton.setText(myNewQuestion);
    

    【讨论】:

    • 快乐的 5 位数代表! :)
    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 2011-11-16
    • 2019-11-12
    • 2019-05-17
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多