向JPanel添加按钮网格

问题描述 投票:0回答:1

我正在尝试制作一个20px x 20px按钮的网格,我将其定义为“单元格”,默认为空白,没有特殊装饰(如阴影),并在单击时更改颜色。 (它们仅用于测试目的而显示“1”)。我创建一个Cell类来定义这些按钮,给每个按钮一个ActionListener。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Cell implements ActionListener
{
    private JButton button;
    private EditorPanel editorPanel;

    public Cell(EditorPanel editorPanel){
    button = new JButton("1'");
    button.addActionListener(listener -> colorCell());
    button.setPreferredSize(new Dimension(20,20));
    button.setMargin(new Insets(0,0,0,0));
    button.setOpaque(true);
    button.setContentAreaFilled(false);
    this.editorPanel = editorPanel;
    }

public JButton getButton() {
    return button;
}

public void colorCell()
{
    button.setBackground(Color.BLACK);
}

@Override
public void actionPerformed(ActionEvent e) {

}

}

然后在我的EditorPanel类中使用Cell对象(单元格)数组来创建这些按钮的网格,其尺寸由“col”和“row”定义。

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;


import javax.swing.JFrame;
import javax.swing.JPanel;
public class EditorPanel{

public JFrame jframe;
public JPanel jpanel;
public static EditorPanel editorPanel;
public Render render;
public static final int col = 45, row = 45,  tile_size=20;
public static final int panelWidth=900, panelHeight=900;
public Dimension dim;
public int coloredPixels;

public Cell[][] cells; 

public void getFrame() {

    editorPanel = new EditorPanel();
    dim = Toolkit.getDefaultToolkit().getScreenSize();
    jframe = new JFrame("Pixel Art Creator");
    jframe.setVisible(true);
    jframe.setSize(panelWidth+17, panelHeight+40);
    jframe.setLocation(dim.width/2 - jframe.getWidth()/2, dim.height/2 - jframe.getHeight()/2);
    jframe.add(render = new Render());
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private JPanel addCells()
{
    cells=new Cell[row][col];
    JPanel panel = new JPanel(new GridLayout(row, col));
    for(int i = 0; i< row; i++){
        for(int j = 0; j<col; j++){
            cells[i][j] = new Cell(this);
            panel.add(cells[i][j].getButton());
        }
    }
    return panel;
}

public static void main (String[] args)
{
    editorPanel = new EditorPanel();
    editorPanel.getFrame();
    editorPanel.addCells();
}

}

然后我尝试将我尝试放入的每个创建的Cell对象添加到addCells()方法的cells数组中,并将其添加到我的JPanel中。当我运行此代码时,我没有得到任何按钮,这意味着这些按钮没有添加到JPanel。我该怎么办呢?

java swing jbutton grid-layout
1个回答
1
投票

所以,两个“重大”问题:

  1. editorPanel.addCells();从未将它创造的JPanel添加到任何东西,因此它永远不会被显示出来
  2. 在完成UI建立之前调用JFrame#setVisible可能导致UI元素无法显示在UI上。您可以通过在更改的容器上调用revalidaterepaint来解决此问题,但如果可能,只需首先建立UI,然后使其可见

但是,我会建议方法略有改变。而不是让Cell成为一个包含JButton的类,然后将该按钮暴露给UI的其他方面,我会将Cell作为一个组件,只需将其添加到您想要的容器中,例如......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            for (int y = 0; y < 20; y++) {
                gbc.gridy = y;
                for (int x = 0; x < 20; x++) {
                    gbc.gridx = x;
                    add(new Cell(), gbc);
                }
            }
        }

    }

    public class Cell extends JPanel {

        public Cell() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    colorCell();
                }
            });
            setBorder(new LineBorder(Color.GRAY));
        }

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

        protected void colorCell() {
            if (getBackground() != Color.DARK_GRAY) {
                setBackground(Color.DARK_GRAY);
            } else {
                setBackground(null);
            }
        }

    }

}

现在,我刚刚在这种情况下使用了一个普通的旧JPanel,但你可以很容易地从JButtonJToggledButton延伸......但我可能会想要使用工厂模式,但那就是我。

使用GridBagLayout的目的是允许在不改变Cells本身大小的情况下调整框架和外部容器的大小,这与GridLayout不同,后者将尝试使单元格填充可用空间

© www.soinside.com 2019 - 2024. All rights reserved.