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

标签 java arraylist

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

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/

相关文章:

JAVA: Resizing Array lists ,创建的旧数组和临时数组会怎样?

java - ArrayList 排序元素的初始索引

java - java中从ArrayList中获取值

java - 无法在 Spring 服务中使用 ReposiotryResource 端点

java - 如何将新对象添加到已创建的列表中?

java - 从另一个项目调用 spring @service

java - 如何引用特定对象?

java - Java 中查找整数 ArrayList 中最小值的递归函数

java - 可扩展后端中的服务器发送事件

java - 如何使用不同数据类型的其他列表中的值填充java中的列表?