Pri*_*mus 4 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);
}
Run Code Online (Sandbox Code Playgroud)

编辑:注意,在这种情况下,Main.diameter设置为100.
问题是GridLayout为所有组件提供相等的空间,将可用空间除以行数/列数,以便可用空间增加,每个单独的单元格也会增加,但组件仍保持其首选大小.
WrapLayout当水平空间减小时,使用类似的东西将自动按行分组

使用a GridBagLayout可以保留行/列,但可以更好地控制各个组件占用的可用空间

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

...添加GridBagConstraints#weightx,GridBagConstraints#weightx并GridBagConstraints#fill填补可用空间

看一下在容器中布局组件,如何使用GridLayout以及如何使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
你必须包括WrapLayout你自己并GridBagLayout从上面的代码中删除引用,但这就是我所使用的
| 归档时间: |
|
| 查看次数: |
2168 次 |
| 最近记录: |