gpt4 book ai didi

java - 如何设置图标没有间隙?

转载 作者:行者123 更新时间:2023-12-02 03:14:34 25 4
gpt4 key购买 nike

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

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);
}

}

结果我得到这个:this image 。我正在使用网格布局。我的图片尺寸为 20x20 像素。如何添加没有此水平间隙的图像?

最佳答案

谢谢安德鲁·汤普森!看来使用更大尺寸的网格效果很好! enter image description here

关于java - 如何设置图标没有间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40494449/

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