java - 是否可以使用 for 循环来创建单选按钮?

标签 java swing for-loop arraylist hashmap

我正在设计一个程序,它使用输入文件来存储颜色及其十六进制值(例如,Black 000000)。目前我有两个数组列表,一个用于颜色,一个用于十六进制值(我知道我可能应该使用 map ,但我坚持将输入传输到 map 中)。无论如何,是否可以使用我的 colorCollection 数组的大小来使用 for 循环?我附加了一些代码,看看这是否有助于我想要完成的任务。

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

public class ReadStoreShow extends JFrame{
private static int number;
private static ArrayList<String> colorCollection = new ArrayList<String>();
private static ArrayList<String> hexCollection = new ArrayList<String>();
private JRadioButton[] jrbColor = new JRadioButton[20];

public ReadStoreShow() {
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(4,5));
    for (int i = 0; i < colorCollection.size(); i++) {
        jrbColor[i] = new JRadioButton(colorCollection.get(i));
            // Is it possible to create buttons based on the size of colorCollection?
        jrbColor[i].setText(colorCollection.get(i));
        ButtonGroup group = new ButtonGroup();
        group.add(jrbColor[i]);
        p1.add(jrbColor[]);
        } 

        add(p1, BorderLayout.CENTER);
        setContentPane(p1);
        for (int j = 0; j < colorCollection.size(); j++){
        jrbColor[j].addActionListener(new ActionListener() {
            @Override
                public void actionPerformed(final ActionEvent e) {
                for (int k = 0; k < colorCollection.size(); k++){
                    final String hexColor = hexCollection.get(k);
                    getContentPane().setBackground(Color.decode(hexColor));
                    repaint();
                }
                } 
            });
        }
}

public static void main(final String[] args) throws IOException {
    final ReadStoreShow frame = new ReadStoreShow();
    frame.pack();
    frame.setLayout(new GridLayout(20, 1));
    frame.setSize(400, 300);
    frame.setTitle("Color Change");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

try {
     final java.io.File colors = new java.io.File("input.txt");
     final Scanner input = new Scanner(colors);
     while (input.hasNext()) {
         colorCollection.add(input.next());
         hexCollection.add(input.next());
     } // I'm assuming I should have used one Map instead of two arrays...
     input.close();
 }
 catch (final FileNotFoundException a) {
     JOptionPane.showMessageDialog(null, "File not found.");
     System.exit(0);
 }
while (number < 10 || number > 20) {
     number = Integer.parseInt(JOptionPane.showInputDialog(null, 
         "How many colors do you want? Must be between 10 and 20."));
 } // while
 System.out.println("The colors entered were:");
 for (final Iterator<String> itr = colorCollection.iterator(); itr.hasNext();)
     System.out.println(itr.next());
 System.out.println("The hexidecimal codes entered were:");
 for (final Iterator<String> itr = hexCollection.iterator(); itr.hasNext();)
     System.out.println(itr.next());
 }
}

这是我当前的输入.txt:

Black 0x000000 
Red 0xFF0000
Green 0x00FF00
Blue 0x0000FF
Yellow 0xFFFF00
White 0xFFFFFF 
Gray 0x707070
Purple 0x990099
Orange 0xFF6600
LightBlue 0x6666FF 

最佳答案

是的,您可以做到这一点,但是您的代码中有很多问题。我在下面仅描述了一些内容,这里的评论中有更多信息。

你有:

for (int i = 0; i < colorCollection.size(); i++) {
    jrbColor[i] = new JRadioButton(colorCollection.get(i));
    ...
    ButtonGroup group = new ButtonGroup();
    group.add(jrbColor[i]);
} 

在这里您将创建一个新的 ButtonGroup对于每个单选按钮。这可能不是你想要的。一个ButtonGroup是一组单选按钮,其中的选择是互斥的,因此您的 ButtonGroup应包含给定概念选择的所有选项。就您而言,听起来很简单:

ButtonGroup group = new ButtonGroup();
for (int i = 0; i < colorCollection.size(); i++) {
    jrbColor[i] = new JRadioButton(colorCollection.get(i));
    ...
    group.add(jrbColor[i]);
} 

另一个问题是您使用的是固定大小 JRadioButton[]大批。考虑使用动态的东西,比如 ArrayList<JRadioButton> 反而。这样您就可以在末尾添加任意数量的新按钮。

让我印象深刻的第三件事是,你不想setContentPane(p1) ; Swing 将为您提供一个内容 Pane 。您需要做的就是设置适当的布局并将组件添加到框架中。

官方有大量的 Swing 教程 here 。我建议浏览并阅读相关的内容;特别是 radio buttons 上的那些, button groups ,和laying out components将对您非常有帮助。

不要直接解决代码片段中的所有问题;您可能需要阅读这些教程,再试一次,如果仍然遇到问题,请回来。如果您从一个小测试程序开始,只在面板上创建几个单选按钮,这也可能会有所帮助;这样您就可以感受到它,而不会受到实际程序应该做的其他事情的束缚。

关于java - 是否可以使用 for 循环来创建单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22084104/

相关文章:

java - android studio 中的 Tabitem 绑定(bind)

Java Swing 编程面板标题

python - 嵌套列表循环

java - 如何通过按空格键切换 JPanel

c++ - 右对齐三角形的间距

java - 是否可以在没有两个循环的情况下遍历二维数组?

java - Android - 日历数据未使用 putExtra() 发送

java - 同一个类上注解Entity和Component是不是错了

java - 在 Eclipse 中的 jar 中包含一个 jar 吗? (不使 JAR 可运行)

java - 从 JMenuBar 获取 JMenuItems