gpt4 book ai didi

java - JRadiobutton 选择未显示

转载 作者:行者123 更新时间:2023-12-01 13:40:25 26 4
gpt4 key购买 nike

Java:我在 JPanel 中添加了 3 个 JRadioButtons,后者又被添加到 GraphicsProgram 中。当我运行程序时,我可以选择单选按钮(当我单击按钮时,它们会起作用)。但是,JPanel 不显示新选择。换句话说,即使选择有效,它们也不会显示。

即使我将单选按钮添加到 ButtonGroup 中,它也不起作用。我尝试了 JPanel 工具栏的 repaint() ,但它仍然不起作用。

指向小程序运行时屏幕截图的链接。单选按钮就这样卡住了。当我点击“Pellets”或“Gate”时,它仍然显示“Walls”被选中。然而,即使按钮没有显示正确的选择,它们仍然被选中。

enter image description here

toolbar = new JPanel();

wallButton = new JRadioButton("Walls");
wallButton.setActionCommand("walls");
wallButton.setSelected(true);

pelletButton = new JRadioButton("Pellets");
pelletButton.setActionCommand("pellets");

gateButton = new JRadioButton("Gate");
gateButton.setActionCommand("gate");

toolbar.add(wallButton);
toolbar.add(pelletButton);
toolbar.add(gateButton);

wallButton.addActionListener(this);
pelletButton.addActionListener(this);
gateButton.addActionListener(this);

add(toolbar, SOUTH);

最佳答案

我认为您缺少ButtonGroup

add()之前添加以下代码:

ButtonGroup radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(wallButton);
radioButtonGroup.add(pelletButton);
radioButtonGroup.add(gateButton);

关于java - JRadiobutton 选择未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20848601/

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