向JPanel添加按钮网格

时间:2018-03-01 03:48:46

标签: java swing jbutton grid-layout

我尝试制作一个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();
}

}

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

1 个答案:

答案 0 :(得分:1)

所以,两个&#34;重要&#34;的问题:

  1. editorPanel.addCells();永远不会将它创建的JPanel添加到任何内容中,因此永远不会显示
  2. 在您完成UI建立之前调用JFrame#setVisible可能导致UI元素无法显示在UI上。您可以通过调用已更改的容器上的revalidaterepaint来解决此问题,但如果可能,只需首先建立用户界面,然后将其设为可见
  3. 但是,我会建议方法略有改变。而不是使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的目的是允许调整框架和外部容器的大小而不改变Cell自身的大小,这与GridLayout不同,$files = glob("../database/CWBRadar/CWB05Radar\[Wufenshan\]/*.jpg"); var_dump($files); 将尝试制作单元格填充可用空间