gpt4 book ai didi

java - 我的 JRadioButton 无法与我的 Action 监听器一起使用

转载 作者:行者123 更新时间:2023-12-01 16:55:04 24 4
gpt4 key购买 nike

所以我正在制作一个程序来找出多面体的表面积和体积,所以我需要使用JRadioButton让用户选择他们想要的形状,如果他们想要SurfaceArea或音量之类的东西。

但是,我遇到了一个问题,要求我每次单击新按钮时都运行一些东西。

当我向 JRadioButton 添加 actionListener() 时,actionPerformed() 方法甚至没有运行。我有什么遗漏的吗?

我希望运行我的actionPerformed()方法。

width.addActionListener(ral);
height.addActionListener(ral);
length.addActionListener(ral);
slantHeight.addActionListener(ral);
radius.addActionListener(ral);
displayAnswer.addActionListener(ral);


public void actionPerformed(ActionEvent a) {
System.out.println("Changed Radio Button: " + a.getSource());
}

最佳答案

来自How to Write an Item Listener (强调我的):

Item events are fired by components that implement the ItemSelectable interface. Generally, ItemSelectable components maintain on/off state for one or more items.

由于单选按钮符合此描述,ItemListener会是一个更适合使用的监听器;尝试一下。

希望这有帮助!

关于java - 我的 JRadioButton 无法与我的 Action 监听器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34235257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com