从Arraylist动态创建的单选按钮

时间:2014-05-04 22:00:44

标签: java arraylist

我之前已经问过这个问题并且已经解决了这个问题,但仍然没有运气。我无法根据我的arraylist动态显示radiobuttons。任何指导为什么这不起作用表示赞赏!

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;}
}

1 个答案:

答案 0 :(得分:1)

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

你的代码有点混乱。你不需要2帧,你不需要扩展主类。你做了一些奇怪的混合搭配。