创建jpanel后改变颜色

时间:2014-03-16 14:12:56

标签: java swing user-interface jpanel

我正在尝试制作一个GUI迷宫游戏,当计算机试图解决迷宫时,它会改变迷宫中点的颜色。迷宫由带有JPanel(GridLayout)的JFrame组成。在网格中是我需要改变颜色的JPanels。我不确定在创建它们之后如何访问它们。

我的代码:

public Maze(int length) {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel(new GridLayout(length, length, 5,5));
    panel.setPreferredSize(new Dimension(500, 500));

    for (int i = 0; i < length; i++) {
        for (int j = 0; j < length; j++) {               
            JPanel p2 = new JPanel();
            p2.setBackground(Color.red);           

            panel.add(p2);
        }
    }

    frame.setDefaultCloseOperation(3);
    frame.setTitle("Maze Game");
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
}

有没有办法在不同的方法中更改p2的颜色?或者有更好的方法吗?

1 个答案:

答案 0 :(得分:6)

如果您有JFrame的裁判,那么您可以这样做。

    int count = 0;
    for (Component comp : frame.getContentPane().getComponents()) {
        System.out.println(comp.getBackground());
        if (count == 6) {
            comp.setBackground(Color.GREEN);
        }
        count++;
    }

此处6表示第2行和第3列,与JPanel中添加JFrame的顺序相同。

enter image description here


完整示例代码[已编辑]

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class Maze {
    private JFrame frame = null;

    public Maze(int length) {

        frame = new JFrame();

        JPanel panel = new JPanel(new GridLayout(length, length, 5, 5)) {
            private static final long serialVersionUID = 1L;

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(500, 500);
            }
        };

        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                JPanel p2 = new JPanel();
                p2.setBackground(Color.red);

                panel.add(p2);
            }
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Maze Game");
        frame.setContentPane(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void setPanelColor(int index) {
        frame.getContentPane().getComponents()[index].setBackground(Color.GREEN);
    }

    public static void main(String[] a) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }
                Maze maze = new Maze(4);
                maze.setPanelColor(6);
            }
        });
    }
}

编辑:

  1. EventQueue.invokeLater()

    java中所有与GUI相关的东西应该总是通过一个线程。线程是我们的传奇AWT-EventQueue。因此,所有与GUI相关的操作都必须通过AWT Event线程。如果不是这样,您可能最终陷入死锁

    了解更多Should we use EventQueue.invokeLater for any GUI update in a Java desktop application?

  2. UIManager.setLookAndFeel()

    UIManager管理当前的外观,即可用的外观和感觉。

    了解更多How to Set the Look and Feel

  3. JComponent#getPreferredSize()

    了解更多Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?