着色GridLayout的特定部分

时间:2018-06-10 15:21:43

标签: java swing

private JPanel buildCenterPanel(ActionHandler handler)
{
    JLabel[] tiles = {new JLabel("1"),new JLabel("2"), new JLabel("3"), new JLabel("4")}
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new GridLayout(2, 2));
    JPanel grid0 = new JPanel();
    JPanel grid1 = new JPanel();
    JPanel grid2 = new JPanel();
    JPanel grid3 = new JPanel();

    JPanel[] panels = {grid0, grid1, grid2, grid3}; 
    for (int i = 0; i < tiles.length; i++)
    {
        panels[i].add(tiles[i]);
        if(((JLabel)panels[i].getComponent(0)).getText().equals("2"))
        {
            panels[i].setBackground(Color.red);
        }
        if(((JLabel)panels[i].getComponent(0)).getText().equals("4"))
        {
            panels[i].setBackground(Color.blue);
        }
        centerPanel.add(panels[i]);
    }
    return centerPanel;

我想制作一个2乘2的网格,右上角为红色,右下角为蓝色,但是我得到一个具有正确数字但颜色不正确的java文件。 ..

1 个答案:

答案 0 :(得分:0)

发布的方法中缺少的主要内容是将标签添加到面板

private JPanel buildCenterPanel()
{
    JLabel[] tiles = {new JLabel("1"),new JLabel("2"), new JLabel("3"), new JLabel("4")};
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new GridLayout(2, 2));
    JPanel grid0 = new JPanel();
    grid0.add(tiles[0]); // add the label to the JPanel
    JPanel grid1 = new JPanel();
    grid1.add(tiles[1]);
    JPanel grid2 = new JPanel();
    grid2.add(tiles[2]);
    JPanel grid3 = new JPanel();
    grid3.add(tiles[3]);

    JPanel[] panels = {grid0, grid1, grid2, grid3};
    for (int i = 0; i < tiles.length; i++)
    {
        panels[i].add(tiles[i]);
        if(((JLabel)panels[i].getComponent(0)).getText().equals("2"))
        {
            panels[i].setBackground(Color.red);
        }
        if(((JLabel)panels[i].getComponent(0)).getText().equals("4"))
        {
            panels[i].setBackground(Color.blue);
        }
        centerPanel.add(panels[i]);
    }
    return centerPanel;
}

使用循环会稍微好一点:

private JPanel buildCenterPanel()
{
    int rows = 2, cols = 2;
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new GridLayout(rows, cols));

    for (int i = 0; i < (rows*cols) ; i++)
    {
        JPanel panel = new JPanel(); //construct a panel
        panel.add(new Label(String.valueOf(i+1))); //add label to it
        //set panel's background 
        if(i == 1) {
            panel.setBackground(Color.red);
        }else if(i == 3) {
            panel.setBackground(Color.blue);
        }
        centerPanel.add(panel);
    }
    return centerPanel;
}

mcve可能很简单:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestPanel extends JPanel{

    private final static int ROWS = 2, COLS = 2;

    public TestPanel() {

        setLayout(new GridLayout(ROWS, COLS));

        for (int i = 0; i < (ROWS*COLS) ; i++)
        {
            JPanel panel = new JPanel();
            panel.add(new Label(String.valueOf(i+1)));

            if(i == 1) {
                panel.setBackground(Color.red);
            }else if(i == 3) {
                panel.setBackground(Color.blue);
            }
            add(panel);
        }
    }

    public static void main(String[] args){

        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(new TestPanel());
        window.pack();
        window.setVisible(true);
    }
}