【问题标题】:JRadioButton cannot be converted to ButtonModelJRadioButton 无法转换为 ButtonModel
【发布时间】:2018-10-09 21:13:44
【问题描述】:

我有一些 JRadioButtons (suggested_radio, deleted_radio, done_radio, ...)。 我将它们全部放在一个 ButtonGroup (subTask_situation) 中。 我将建议的无线电设置为选择suggested_radio.setSelected(true)。 我希望在用户单击“添加”按钮后再次将此单选设置为选中状态。 所以,我在actionPerformed中写了如下代码:

//some code
subTask_situation.setSelected(suggested_radio, true);

但它给了我错误:JRadioButton 无法转换为 ButtonModel

知道点击“添加”按钮后如何重新选择suggested_radio吗?

【问题讨论】:

  • 或者你可以使用JRadioButton的模型:subTask_situation.setSelected(suggested_radio.getModel(), true);

标签: java user-interface actionlistener


【解决方案1】:

您使用的方法:ButtonGroup.setSelected(...) 实际上接受了 ButtonModel 的参数,而不是 JRadioButton。而不是这样做,只需使用上面编写的相同代码将单选按钮设置为 true:suggested_radio.setSelected(true)

阅读 API 以获取有关单选按钮的更多信息:https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java

对于按钮组:https://docs.oracle.com/javase/8/docs/api/javax/swing/ButtonGroup.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 2023-02-02
    • 2016-06-30
    • 2016-01-14
    • 2013-07-25
    • 2016-07-05
    相关资源
    最近更新 更多