如何用Java中的现有JButton创建JButton数组?

德维肯

我正在做一个彩色铅笔项目,使用100个JButton作为像10x10矩阵定位的像素。我还有10个其他jButton代表颜色,另外2个j代表工具“铅笔”和“桶”。

我目前仅使用铅笔jButton,因此您可以通过单击铅笔JButton然后选择一种颜色的JButton来绘制100个JButton中的任何一个。

该算法运行良好,问题是我需要对所有100个JButton应用相同的着色方法(colorButton),因此我想创建一个数组来存储所有JButton,然后为每个JButton调用我的colorButton方法。

我不知道如何将我所有的100个JButton存储到JButton数组中,因为它们已经创建并命名了。

我正在尝试的是:

public void colorButton(JButton button){
    if (type == "pencil"){
        if(color.equals("gray")){
            button.setBackground( new Color(101,101,101));
        }else if(color.equals("white")){
            button.setBackground( new Color(255,255,255));
        }else if(color.equals("black")){
            button.setBackground( new Color(0,0,0));
        }else if(color.equals("blue")){
            button.setBackground( new Color(0,0,255));
        }else if(color.equals("red")){
            button.setBackground( new Color(255,0,0));
 }

 public void buttonArray(){
    JButton[] button = new JButton[100];

    for (int i = 0; i < 100; i++){
        button[i] = jButton1; //I need to get each of the 100 buttons here
        colorButton(button[i]);
    }
 }

因此,除了JButton1之外,我还需要一种方法来存储所有100个。

任何想法?

谢谢

*编辑以澄清问题和情况

杰森·托洛塔(JasonTolotta)

在不知道其用途的上下文的情况下,并假设colorButton()方法是模拟的,因为它缺少两个花括号。

以下Java代码使用反射使用ColorButtons类中定义的现有JButtons填充ArrayList

我仍然不确定为什么需要在与另一个List的循环中分配Array,但这是在这里。

public class ColorButtons {
    // JButton sample.
    private JButton button1 = new JButton("1");
    private JButton button2 = new JButton("2");
    private JButton button3 = new JButton("3");

    // This is used to store the buttons.
    ArrayList<JButton> jbuttons = new ArrayList<JButton>();

    // Boilerplate, as I have no idea what this does.
    private String type = "pencil";
    private String color = "white";

    /**
     * Populate the JButton List on instantiation.
     * 
     * @see ColorButtons#populateJButtonList()
     */
    public ColorButtons() {
        // Populate "jbuttons" ArrayList with JButtons.
        this.populateJButtonList();
    }

    public void colorButton(JButton button) {
        if (type == "pencil") {
            if (color == "gray") {
                button.setBackground(new Color(101, 101, 101));
            } else if (color == "white") {
                button.setBackground(new Color(255, 255, 255));
            } else if (color == "black") {
                button.setBackground(new Color(0, 0, 0));
            } else if (color == "blue") {
                button.setBackground(new Color(0, 0, 255));
            } else if (color == "red") {
                button.setBackground(new Color(255, 0, 0));
            }
        }
    }

    public void buttonArray() {
        JButton[] button = new JButton[100];

        for (int i = 0; i < 100; i++) {
            // Assign each JButton in the list to array element.
            for (JButton jbutton : jbuttons) {
                button[i] = jbutton; // I need to get each of the 100 buttons
                                        // here
                System.out.println("Button" + button[i].getText());
                colorButton(button[i]);
            }
        }
    }

    /**
     * This is used to add the JButtons to a list using reflection. Used in the
     * constructor.
     * 
     * @see ColorButtons#ColorButtons()
     */
    public void populateJButtonList() {
        // Gets the class attributes, e.g. JButton, String, Integer types, everything.
        // In this case it is this class, but can define any other class in your project.
        Field[] fields = ColorButtons.class.getDeclaredFields();

        // Loop over each field to determine if it is a JButton.
        for (Field field : fields) {

            // If it is a JButton then add it to the list.
            if (field.getType().equals(JButton.class)) {
                try {
                    // De-reference the field from the object (ColorButtons) and cast it to a JButton and add it to the list.
                    jbuttons.add((JButton) field.get(this));
                } catch (IllegalArgumentException | IllegalAccessException
                        | SecurityException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String... args) {
        ColorButtons color = new ColorButtons();

        color.buttonArray();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何用Java中的现有JButton创建JButton数组?

来自分类Dev

在JFrame中单击JButton时如何更改现有图像?(Java)

来自分类Dev

如何用Java制作Round JButton

来自分类Dev

如何模拟特定JButton在JButton数组中的移动

来自分类Dev

如何在Java中创建自定义JButton类?

来自分类Dev

如何在swing java jframe中创建15000个jbutton的数组,并使用button对每一行设置操作?

来自分类Dev

如何创建JButton?

来自分类Dev

如何使JButton在Java中不可点击?

来自分类Dev

如何用ImageIcon完全填充JButton的表面?

来自分类Dev

如何用ImageIcon完全填充JButton的表面?

来自分类Dev

如何在JAVA中不导入JButton的情况下创建按钮

来自分类Dev

数组列表中的Java jbutton,我怎么知道单击了哪个

来自分类Dev

创建一个JButton数组

来自分类Dev

Java JButton 不会遍历容器中的 JLabel(带有图像)

来自分类Dev

如何使用JButton删除textField中字符的所有实例?

来自分类Dev

Java Swing:如何在JPanel中删除JButton的默认间距

来自分类Dev

如何在Java中的JButton上隐藏文本?

来自分类Dev

如何获取Java中JButton的gridlayout单元位置?

来自分类Dev

Java MouseListener创建不带JButton的Button

来自分类Dev

Java Swing ActionListener显示JButton数组

来自分类Dev

创建现有对象Java的数组

来自分类Dev

如何在JButton之间创建间距?

来自分类Dev

如何制作图像的Jbutton数组

来自分类Dev

如何将jbutton设置为数组

来自分类Dev

通过Jbutton在Jtabbedpane中创建动态表单

来自分类Dev

Java-隐藏所有JButton

来自分类Dev

如何在 Ruby 中的现有数组上创建二维数组?

来自分类Dev

动态创建JButton

来自分类Dev

从JButton创建滑块

Related 相关文章

热门标签

归档