【问题标题】:GridLayout Removing Padding between JPanelsGridLayout 移除 JPanel 之间的填充
【发布时间】:2023-03-25 14:17:01
【问题描述】:

我正在从事一个计算机科学项目,但我找不到任何解决我的问题的方法。我正在创建一个JPanels 的二维数组,其中包含通过GridLayout 的图像。我想删除所有面板之间的填充/边距,以便它无缝地流入单个图像。但是, setHGap 和 setVGAp 方法似乎对我没有帮助。我会很感激任何回应。谢谢。

import java.awt.GridLayout;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MapArray {

    static JPanel[][] tiles = new JPanel[11][11];

    public MapArray() {
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Map.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setSize(325, 300);
                GridLayout layout = new GridLayout(10, 10);
                frame.setLayout(layout);

                for (int i = 0; i < 10; i++) {
                    for (int j = 0; j < 10; j++) {
                        tiles[i][j] = new JPanel();
                        tiles[i][j].add(new JLabel(
                            createImageIcon("tile-1.png")));
                        frame.add(tiles[i][j]);
                    }
                }

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.validate();
                frame.repaint();
            }
        });
    }
}

【问题讨论】:

  • 记住你的每个面板也有填充
  • 使用pack(),而不是setSize()
  • 我将如何更改填充?
  • 另外 pack() 没有改变任何东西。

标签: java arrays swing jpanel margin


【解决方案1】:

几点说明:

  • 给每个面板一个GridLayout

  • 使用数组长度作为循环限制。

  • 尽可能重构常量。

代码:

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MapArray {

    private static final int SIZE = 4;
    private static final JPanel[][] tiles = new JPanel[SIZE][SIZE];

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(SIZE, SIZE));
                for (int i = 0; i < tiles.length; i++) {
                    for (int j = 0; j < tiles[0].length; j++) {
                        tiles[i][j] = new JPanel(new GridLayout());
                        tiles[i][j].add(new JLabel(
                            new ImageIcon("image.gif")));
                        frame.add(tiles[i][j]);
                    }
                }
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    相关资源
    最近更新 更多