【问题标题】:(Java) Using GridLayout (possibly GridBagLayout) on a 2D array of JPanels(Java) 在 JPanel 的 2D 数组上使用 GridLayout(可能是 GridBagLayout)
【发布时间】:2013-04-21 18:28:58
【问题描述】:

我正在尝试制作一个程序,允许用户通过鼠标交互来更改 JPanel 的颜色。我的 JPanel 阵列全部启动并运行(5x5),但无论大小,它们都可以扩展以适应整个主面板。关于如何给它们一个标准尺寸以便它们不会填满整个窗口并很好地放在中间的任何想法?

有人告诉我,我可能必须为此使用 GridBagLayout 而不是 GridLayout,但我确实没有使用该方法的经验,而且只是第一次听到这个名字,所以任何关于此的帮助也是类.

public void gridInit() {
        cell = new JPanel [x][x];
        setLayout(new GridLayout(5,5));
        for (int i = 0; i < x; i ++) {
            for (int j = 0; j < x; j ++) {
                cell [i][j] = new JPanel();
                cell [i][j].setBackground(new Color(i*40, j*30, 10));
                cell [i][j].setBorder(loweredetched);
                add(cell [i][j]);           
        }

编辑:我刚刚意识到这是我的第一篇文章,不是关于修复错误,这可能是向前迈出的一步 :)

【问题讨论】:

    标签: java swing jpanel grid-layout gridbaglayout


    【解决方案1】:

    将您的单元格放入一个 GridLayout 中,使用创建的 JPanel 来保存它们,然后使用适合您的 GUI 的任何布局将 that JPanel 添加到主 JPanel。请理解,您可以根据需要嵌套 JPanel,每个 JPanel 都使用自己的布局管理器,从而允许使用简单的布局管理器来创建复杂的布局。

    例如,

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ColorGrid extends JPanel {
       private static final int PREF_W = 750;
       private static final int PREF_H = 550;
       private static final int GRID_ROWS = 5;
       private static final int GRID_COLS = 5;
    
       public ColorGrid() {
          JPanel gridPanel = new JPanel(new GridLayout(GRID_ROWS, GRID_COLS));
          for (int row = 0; row < GRID_ROWS; row++) {
             for (int col = 0; col < GRID_COLS; col++) {
                gridPanel.add(new ColorGridCell(row, col));
             }
          }
    
          setLayout(new GridBagLayout()); // to center component added
          add(gridPanel);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private static void createAndShowGui() {
          ColorGrid mainPanel = new ColorGrid();
    
          JFrame frame = new JFrame("ColorGrid");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    @SuppressWarnings("serial")
    class ColorGridCell extends JPanel {
       private static final int PREF_W = 100;
       private static final int PREF_H = 75;
       private final static Color[] COLORS = { Color.red, Color.orange,
             Color.yellow, Color.green, Color.blue, Color.cyan, Color.darkGray,
             Color.magenta, Color.pink };
       private int colorIndex = (int) (Math.random() * COLORS.length);
       private int row;
       private int col;
    
       public ColorGridCell(int row, int col) {
          this.row = row;
          this.col = col;
          setBackground(COLORS[colorIndex]);
    
          addMouseListener(new MouseAdapter() {
             @Override
             public void mousePressed(MouseEvent e) {
                colorIndex++;
                colorIndex %= COLORS.length;
                setBackground(COLORS[colorIndex]);
             }
          });
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       public int getRow() {
          return row;
       }
    
       public int getCol() {
          return col;
       }
    }
    

    【讨论】:

    • 啊,聪明。我会试一试的。不错的用户名顺便说一句
    • 你是一个……了不起的人。我觉得我需要为此补偿你。你喜欢我可以捐赠给任何特定的慈善机构吗?或者我有办法在网上给你买一品脱吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多