gpt4 book ai didi

java - 从 Arraylist 动态创建的单选按钮

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:54 25 4
gpt4 key购买 nike

我之前已经问过这个问题,并且对这个问题进行了更多的研究,但仍然没有成功。我无法根据我的数组列表动态显示单选按钮。任何关于为什么这不起作用的指导都值得赞赏!

public class ColorRadioButtons extends ReadStoreShow{
private ArrayList<String> radioBtnList = new ArrayList<String>();
private JFrame f = new JFrame("Colors");

public ColorRadioButtons() {
JPanel panel2 = new JPanel(new GridLayout());
panel2.setBorder(new EmptyBorder(10,10,10,10));
ButtonGroup radioButtonGroup = new ButtonGroup();
for (int i=0; i<subListOfColors.size(); i++) {
Colors a = subListOfColors.get(i);
String s = a.getColorName();
radioBtnList.add(s);
JRadioButton jrb = new JRadioButton(s);
radioButtonGroup.add(jrb);
panel2.add(jrb);
}
add(panel2, BorderLayout.CENTER);
f.pack();
f.setTitle("Colors Radio Buttons");
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

这是我的父类代码:-

public class ReadStoreShow extends JFrame{

List<Colors> colorNames = new ArrayList<Colors>();
List<Colors> subListOfColors = new ArrayList<Colors>();

private JTextField jtfFileName = new JTextField();
private JTextField jtfNumber = new JTextField();

private JButton jbtClick = new JButton("Display Output");

public ReadStoreShow() {

JPanel panel1 = new JPanel(new GridLayout(4,1));
panel1.setBorder(new EmptyBorder(10,10,10,10));
panel1.add(new JLabel("File Name"));
panel1.add(jtfFileName);
panel1.add(new JLabel("Number"));
panel1.add(jtfNumber);

JPanel panelButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panelButton.add(jbtClick);
add(panel1, BorderLayout.NORTH);
add(panelButton, BorderLayout.SOUTH);

jbtClick.addActionListener(new ButtonListener());
}

public static void main(String[] args) {
ReadStoreShow frame = new ReadStoreShow();
frame.pack();
frame.setTitle("Colors");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {

String fileName = "";
int number = 0;
try {
fileName = jtfFileName.getText();
number = Integer.parseInt(jtfNumber.getText());
if (number >= 10 && number <= 20)
{
File colors = new File(fileName.toString());
Scanner inputFileName = new Scanner(colors);
while (inputFileName.hasNext()) {
String inputString = inputFileName.nextLine();
String[] parts = inputString.split(" ");
String color = parts[0];
String hex = parts[1];
colorNames.add(new Colors(color, hex));
}
subListOfColors = colorNames.subList(0, number);
Collections.sort(subListOfColors, new ColorComp());
Iterator<Colors> iterator = subListOfColors.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
new ColorRadioButtons();
}
inputFileName.close();
}
else {
System.out.println("Number should be between 10 & 20");
}
}
catch (FileNotFoundException fnfe) {
System.out.println("File not found");
System.out.println("Please ensure that file name is <<name>>.txt");
}
}
}
}

class ColorComp implements Comparator<Colors>{
@Override
public int compare(Colors c1, Colors c2){
String string1 = c1.getColorHex().toString();
String string2 = c2.getColorHex().toString();
int compareResult = string1.compareTo(string2);
if(compareResult > 0) {return 1;}
else {return -1;}
}
}

class Colors {
private String colorName;
private String colorHex;
public Colors(String n, String h) {
this.colorName = n;
this.colorHex = h;
}
public String getColorHex() {return colorHex;}
public String getColorName() {return colorName;}
public String toString() {return this.colorName + " " + this.colorHex;}
}

最佳答案

类 ColorRadioButtons 中,将 add(panel2, BorderLayout.CENTER); 替换为 f.add(panel2, BorderLayout.CENTER);

你的代码有点乱。您不需要 2 个框架,也不需要扩展主类。你做了一些奇怪的混合和搭配。

关于java - 从 Arraylist 动态创建的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23462072/

25 4 0