gpt4 book ai didi

java - 如何显示JRadioButton的数组文本

转载 作者:行者123 更新时间:2023-12-02 03:31:08 24 4
gpt4 key购买 nike

我用谷歌搜索了好几次,但找不到答案。我有一个初始化的 JRadioButton 数组。每当单击单选按钮并且用户单击“确定”按钮时,我想显示 JRadioButton 的初始化值。目前我的 JOptionPane 仅显示内存地址。但包含数组的元素意味着它无法编译。错误是“没有找到适合 toString(jradiobutton) 的方法。请有人指出我正确的方向吗?我是新手。

代码片段在这里:

import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.Arrays;

public class NewGameGUI implements ActionListener
{
private JRadioButton[] rb = new JRadioButton[20];
JFrame f3;
JButton b;
JLabel label1;
public NewGameGUI()
{
label1 = new JLabel("Please choose a team");
f3 = new JFrame("Ballon d'or");
f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rb[0] = new JRadioButton("Arsenal");
rb[1] = new JRadioButton("Aston Villa");
rb[2] = new JRadioButton("Bournemouth");
rb[3] = new JRadioButton("Chelsea");
rb[4] = new JRadioButton("Crystal Palace");
rb[5] = new JRadioButton("Everton");
rb[6] = new JRadioButton("Leicester City");
rb[7] = new JRadioButton("Liverpool");
rb[8] = new JRadioButton("Manchester United");
rb[9] = new JRadioButton("Manchester City");
rb[10] = new JRadioButton("Newcastle United");
rb[11] = new JRadioButton("Norwich City");
rb[12] = new JRadioButton("Southampton");
rb[13] = new JRadioButton("Stoke City");
rb[14] = new JRadioButton("Sunderland");
rb[15] = new JRadioButton("Swansea City");
rb[16] = new JRadioButton("Tottenham Hotspur");
rb[17] = new JRadioButton("Watford");
rb[18] = new JRadioButton("West Brom");
rb[19] = new JRadioButton("West Ham");


label1.setBounds(50,25,200,30);
f3.add(label1);

rb[0].setBounds(50,100,170,30);
rb[1].setBounds(50,150,170,30);
rb[2].setBounds(50,200,170,30);

ButtonGroup bg=new ButtonGroup();
bg.add(rb[0]);
bg.add(rb[1]);
bg.add(rb[2]);
bg.add(rb[3]);
bg.add(rb[4]);
bg.add(rb[5]);
bg.add(rb[6]);
bg.add(rb[7]);
bg.add(rb[8]);
bg.add(rb[9]);
bg.add(rb[10]);
bg.add(rb[11]);
bg.add(rb[12]);
bg.add(rb[13]);
bg.add(rb[14]);
bg.add(rb[15]);
bg.add(rb[16]);
bg.add(rb[17]);
bg.add(rb[18]);
bg.add(rb[19]);

b = new JButton("OK");
b.setBounds(200,250,180,30);
b.addActionListener(this);
f3.add(rb[0]);
f3.add(rb[1]);
f3.add(rb[2]);
f3.add(rb[3]);
f3.add(rb[4]);
f3.add(rb[5]);
f3.add(rb[6]);
f3.add(rb[7]);
f3.add(rb[8]);
f3.add(rb[9]);
f3.add(rb[10]);
f3.add(rb[11]);
f3.add(rb[12]);
f3.add(rb[13]);
f3.add(rb[14]);
f3.add(rb[15]);
f3.add(rb[16]);
f3.add(rb[17]);
f3.add(rb[18]);
f3.add(rb[19]);
f3.add(b);


f3.setSize(500,500);
f3.setLayout(null);
f3.setVisible(true);

// NewGameGUI test = new NewGameGUI();
// test.setFrame(f3);

}

public void setFrame(JFrame f3){
this.f3 = f3;
}
public JFrame getFrame(){

return f3;
}
public void actionPerformed(ActionEvent e)
{
if(rb[0].isSelected())
{
// f3.dispose();
f3.dispose();

JOptionPane.showMessageDialog(f3,"You chose : " + Arrays.toString(rb[0]));
}
if(rb[1].isSelected())
{
JOptionPane.showMessageDialog(f3,"This works too!");
}
if(rb[2].isSelected())
{
System.exit(0);
}

}


}

最佳答案

改变这个

JOptionPane.showMessageDialog(f3,"您选择了:"+ Arrays.toString(rb[0]));

JOptionPane.showMessageDialog(f3,"您选择了:"+ rb[0].getText());

这应该检索JRadioButton的文本而不是“内存地址”。

关于java - 如何显示JRadioButton的数组文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38070920/

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