如何在GridLayout(Java)中删除ImageIcons之间的空间

时间:2015-07-07 03:45:20

标签: java swing jlabel grid-layout imageicon

我正在制作一个依赖于网格中的图块的java游戏。我在GridLayout中使用了ImageIcon和JLabel。我在创建GridLayout时将垂直和水平都设置为零,并且所有使用的图像都没有额外的空间。

解决此问题的最佳方法是什么?

//Sets up the game canvas
private void setUpCanvasPanel(){
    //Adjusts the panel settings
    canvasPanel.setLayout(new GridLayout(Main.diameter, Main.diameter, 0 ,0));

    //Adds panel to masterpanel
    resetc();
    c.gridx = 0;
    c.gridy = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;

    masterPanel.add(canvasPanel, c);
}

//Renders the game canvas
public void renderCanvas(){
    ImageIcon[] iconArray = new ImageIcon[Main.diameter * Main.diameter];
    JLabel[] labelArray = new JLabel[Main.diameter * Main.diameter];
    int count = 0;

    for(int i = 0; i < Main.diameter; i ++){
        for(int j = 0; j < Main.diameter; j ++){
            iconArray[count] = new ImageIcon("textures/" + Main.renderer.renderedWorld[i][j] + ".jpeg"); 
            labelArray[count] = new JLabel(iconArray[count]);
            canvasPanel.add(labelArray[count]);

            count ++;
        }
    }

    count = 0;

    canvasPanel.setVisible(true);
}

enter image description here

编辑:注意,在这种情况下,Main.diameter设置为100。

2 个答案:

答案 0 :(得分:7)

问题是GridLayout将为所有组件提供相等的空间,将可用空间除以行数/列数,以便可用空间增加,每个单独的单元格也会增加,但组件保持在他们喜欢的大小。

你可以......

使用类似WrapLayout的内容,当水平空间缩小时,它会自动按行排列

WrapLayout

你可以......

使用GridBagLayout,这将允许您维护行/列,但可以更好地控制各个组件占用的可用空间

GridBagLayout

...使用GridBagConstraints#insets在组件之间添加间距

Insets

...添加GridBagConstraints#weightxGridBagConstraints#weightxGridBagConstraints#fill以填充可用空间

Fill

查看Laying Out Components Within a ContainerHow to Use GridLayoutHow to Use GridBagLayout了解详情

......还有一些测试代码......

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TexturePane extends JPanel {

        public TexturePane(Color background) {
            setBackground(background);
        }

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

    }

    public class TestPane extends JPanel {

        public TestPane() {
            int total = 10 * 10;
            Color[] rowColors = new Color[]{
                Color.RED,
                Color.GREEN,
                Color.BLUE,
                Color.CYAN,
                Color.MAGENTA,
                Color.ORANGE,
                Color.PINK,
                Color.YELLOW,
                Color.DARK_GRAY,
                Color.GRAY,
            };
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(1, 1, 1, 1);
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            for (int row = 0; row < 10; row++) {
                gbc.gridy = row;
                for (int col = 0; col < 10; col++) {
                    gbc.gridx = col;
                    double progress = (row * 10) + col;
                    Color color = blend(Color.BLACK, rowColors[row], col / 10d);
                    add(new TexturePane(color), gbc);
                }
            }
        }

    }
    public static Color blend(Color color1, Color color2, double ratio) {
        float r = (float) ratio;
        float ir = (float) 1.0 - r;

        float rgb1[] = new float[3];
        float rgb2[] = new float[3];

        color1.getColorComponents(rgb1);
        color2.getColorComponents(rgb2);

        float red = rgb1[0] * r + rgb2[0] * ir;
        float green = rgb1[1] * r + rgb2[1] * ir;
        float blue = rgb1[2] * r + rgb2[2] * ir;

        if (red < 0) red = 0;
        else if (red > 255) red = 255;
        if (green < 0) green = 0;
        else if (green > 255) green = 255;
        if (blue < 0) blue = 0;
        else if (blue > 255) blue = 255;

        Color color = null;
        try {

            color = new Color(red, green, blue);

        } catch (IllegalArgumentException exp) {

            NumberFormat nf = NumberFormat.getNumberInstance();
            System.out.println(nf.format(red) + "; " + nf.format(green) + "; " + nf.format(blue));

        }
        return color;
    }

}

您必须自己加入WrapLayout并从上述代码中移除对GridBagLayout的引用,但这就是我使用过的所有内容

答案 1 :(得分:1)

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
masterPanel.add(canvasPanel, c);

我会说问题是你的约束。您告诉小组增长以填充所有可用空间。因此,随着网格的增长,您将获得所有单元格之间的空间。

相反,您希望图标以其首选大小显示,这是默认值。所以摆脱那些限制。

或者另一种方法是使图标动态调整大小以填充每个单元格中的可用空间。您可以将Darryl的Stretch Icon用于此目的。