如何设置图标没有间隙?

How to set icons without gap?

我正在使用 java 秋千。我想添加图片网格。

public class LayoutTest {
    JFrame frame = new JFrame("GridLayout demo");
    JPanel panel = new JPanel();
    JButton btn1 = new JButton("First");
    JButton btn2 = new JButton("Second");
    JButton btn3 = new JButton("Third");
    JButton btn4 = new JButton("Fourth");
    JPanel panel2 = new JPanel();
    JButton btn12 = new JButton("First2");
    JButton btn22 = new JButton("Second2");
    JButton btn32 = new JButton("Third2");
    JButton btn42 = new JButton("Fourth2");
    JPanel panel3 = new JPanel();
    JButton btn13 = new JButton("First2");
    JButton btn23 = new JButton("Second2");
    JButton btn33 = new JButton("Third2");
    JButton btn43 = new JButton("Fourth2");

    JLabel label13 = new JLabel(new ImageIcon("pictures/building.jpg"), JLabel.CENTER);
    JLabel label23 = new JLabel(new ImageIcon("pictures/building2.png"), JLabel.CENTER);
    JLabel label33 = new JLabel(new ImageIcon("pictures/building.jpg"), JLabel.CENTER);
    JLabel label43 = new JLabel(new ImageIcon("pictures/building2.png"), JLabel.CENTER);
    public LayoutTest() {
        panel3.setLayout(new GridLayout(2,2,0,0));
        panel3.add(label13);
        panel3.add(label23);
        panel3.add(label33);
        panel3.add(label43);

        frame.add(panel3);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
//        frame.setSize(40,40);
        frame.setVisible(true);
    }

}

结果我得到了这个:。我正在使用网格布局。我的图片大小为 20x20 像素。如何在没有这种水平间隙的情况下添加图像?

谢谢安德鲁·汤普森!似乎使用更大尺寸的网格效果很好!

看来框架装饰迫使额外的尺寸。如果将四个图标添加到一行中,则间隙应该减小或消失。

框架的默认布局是 BorderLayout。在没有指定约束的情况下将组件添加到边框布局时,它将在 CENTER 中结束。添加到边框布局中心的组件 (panel3) 将被拉伸以填充可用的整个宽度和高度。 GridLayout 将根据需要为最宽和最高的网格单元分配相等的宽度和高度。因此,也可以通过将 panel3 添加到内容窗格的 LINE_STARTLINE_END 来避免这种行为 - 这些位置将拉伸高度但尊重宽度。

这是一个示例,它演示了边框布局的每个约束内的效果。

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class GridLayoutSpace {

    private JComponent ui = null;

    GridLayoutSpace() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout());
        ui.setBorder(new EmptyBorder(4,4,4,4));

        addFourIcons(Color.ORANGE, BorderLayout.PAGE_START);
        addFourIcons(Color.ORANGE, BorderLayout.PAGE_END);

        addFourIcons(Color.BLUE, BorderLayout.LINE_START);
        addFourIcons(Color.BLUE, BorderLayout.LINE_END);

        addFourIcons(Color.YELLOW, BorderLayout.CENTER);
    }

    private void addFourIcons(Color bg, String constraint) {
        JPanel p = new JPanel(new GridLayout(0, 2, 0, 0));
        p.setBackground(bg);
        ui.add(p, constraint);
        p.add(new JLabel(new ImageIcon(getImage(Color.RED))));
        p.add(new JLabel(new ImageIcon(getImage(Color.GREEN))));
        p.add(new JLabel(new ImageIcon(getImage(Color.GREEN))));
        p.add(new JLabel(new ImageIcon(getImage(Color.RED))));
    }
    int sz = 24;
    private BufferedImage getImage(Color color) {
        BufferedImage bi = new BufferedImage(sz, sz, BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.setColor(color);
        g.fillRect(0,0, sz, sz);
        g.dispose();
        return bi;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                GridLayoutSpace o = new GridLayoutSpace();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}