gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:42:08 24 4
gpt4 key购买 nike

我正在制作一款依赖于在网格中放置图 block 的 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。

最佳答案

问题是 GridLayout 将为所有组件提供等量的空间,将可用空间除以行/列数,因此随着可用空间的增加,每个单独的单元格也会增加,但组件保持其首选大小。

你可以...

使用像 WrapLayout 这样的东西随着水平空间的减少,它会自动按行向下组件

WrapLayout

你可以...

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

GridBagLayout

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

Insets

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

Fill

看看Laying Out Components Within a Container , How 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 的引用,但我只用了这些

关于java - 如何删除 GridLayout (Java) 中 ImageIcons 之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31259373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com