JRadiobutton选择没有显示

问题描述 投票:0回答:3

Java:我有一个3 JRadioButtons添加到JPanel,它被添加到GraphicsProgram。当我运行程序时,我可以选择radiobuttons(当我点击按钮时,它们会起作用)。然而,JPanel没有显示新的选择。换句话说,即使选择WORK,它们也不会显示。

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

applet运行时屏幕截图的链接。单选按钮被卡住了。当我点击“Pellets”或“Gate”时,它仍然显示选择了“Walls”。但是,即使按钮没有显示正确的选择,它们仍然被选中。

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);
java swing jpanel jradiobutton
3个回答
1
投票

我想你错过了ButtonGroup

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

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

0
投票

我认为你的JRadioButton面板上有toolbars的布局问题。尝试将LayoutManager(可能是new FlowLayout())设置为toolBar面板。


0
投票

在组件更改后,您必须调用repaint()revalidate()。尝试在RadioButtons的动作侦听器上添加这些方法。

© www.soinside.com 2019 - 2024. All rights reserved.