gpt4 book ai didi

java - 为什么即使使用 setSelected(true) 也没有选择 JRadioButton

转载 作者:行者123 更新时间:2023-12-02 00:17:08 25 4
gpt4 key购买 nike

我只想默认选择上述按钮之一但 setSelected(true) 不起作用。当我运行下面的程序时,没有选择任何 JRadoiButton

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class RadioDemo implements ActionListener {

String buttonName;
JPanel radioPanel=new JPanel();
ButtonGroup group = new ButtonGroup();
Enumeration enl;
int result;
ActionEvent e;
JRadioButton birdButton[];
int i;
Vector<JComponent> list;
Vector<String> listName;


public RadioDemo(Vector<JComponent> list,Vector<String> listName,Enumeration en,Enumeration enl)
{
birdButton=new JRadioButton[list.size()];
this.enl=enl;
this.list=list;
this.listName=listName;

for(i=0;i<list.size()-1;i++)
{
buttonName=(String)enl.nextElement();
birdButton[i] = new JRadioButton(buttonName);
birdButton[i].setSelected(false);
birdButton[i].setActionCommand(buttonName);
group.add(birdButton[i]);
birdButton[i].addActionListener(this);
radioPanel.add(birdButton[i]);
}

buttonName=(String)enl.nextElement();
birdButton[i] = new JRadioButton(buttonName);
birdButton[i].setSelected(true);
birdButton[i].setActionCommand(buttonName);
group.add(birdButton[i]);
birdButton[i].addActionListener(this);

radioPanel.add(birdButton[i]);
radioPanel.setLayout(new BoxLayout(radioPanel,BoxLayout.Y_AXIS));
//birdButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
result = JOptionPane.showConfirmDialog(null, radioPanel,
"Please choose", JOptionPane.OK_CANCEL_OPTION);
show();
}



/** Listens to the radio buttons. */
public void actionPerformed(ActionEvent e)
{
this.e=e;
}

public void show()
{
if (result == JOptionPane.OK_OPTION)
{ i=0;
while(!birdButton[i].isSelected())
{
i++;
System.out.println(i);
}
//list.removeElementAt(i);
//listName.removeElementAt(i);
System.out.println(i);
System.out.println(e.getActionCommand());
}
}

我也尝试birdButton[0].setSelected(true);跳出循环

最佳答案

您还没有发布如何调用构造函数,所以也许那里有什么东西。我稍微修改了你的代码,添加了一个 main 方法,它似乎工作正常。看看它:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class RadioDemo implements ActionListener {

String buttonName;
JPanel radioPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
int result;
JRadioButton birdButton[];
Vector<String> listName;
private JRadioButton selectedButton;

public RadioDemo(Vector<String> listName) {
birdButton = new JRadioButton[listName.size()];
this.listName = listName;
int i = 0;
for (String buttonName : listName) {
birdButton[i] = new JRadioButton(buttonName);
if (i == 0) {
birdButton[i].setSelected(true);
selectedButton = birdButton[i];
}
birdButton[i].setActionCommand(buttonName);
group.add(birdButton[i]);
birdButton[i].addActionListener(this);
radioPanel.add(birdButton[i]);
i++;
}

radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
// birdButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
result = JOptionPane.showConfirmDialog(null, radioPanel, "Please choose", JOptionPane.OK_CANCEL_OPTION);
show();
}

/** Listens to the radio buttons. */
@Override
public void actionPerformed(ActionEvent e) {
JRadioButton rb = (JRadioButton) e.getSource();
System.err.println(rb.getText() + " is selected");
selectedButton = rb;
}

public void show() {
if (result == JOptionPane.OK_OPTION) {
System.err.println(selectedButton.getText() + " is selected and approved");
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Vector<String> buttonNames = new Vector<String>();
buttonNames.add("Show");
buttonNames.add("Something");
buttonNames.add("Else");
buttonNames.add("Beep");
new RadioDemo(buttonNames);
}
});
}
}

关于java - 为什么即使使用 setSelected(true) 也没有选择 JRadioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709169/

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