我正在使用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);
}
}
结果我得到了这个:。我正在使用gridLayout。我的照片尺寸为20x20像素。如何在没有这个水平间隙的情况下添加图像?
答案 0 :(得分:1)
答案 1 :(得分:1)
看起来框架装饰正在逼迫额外的尺寸。如果将四个图标添加到一行中,则间隙应减小或消失。
框架的默认布局为BorderLayout
。将组件添加到边框布局而未指定约束时,它将最终位于CENTER
中。添加到边框布局中心的组件(panel3
)将被拉伸以填充整个可用宽度和高度。 GridLayout
将为最宽和最高的网格单元分配相同的宽度和高度。因此,通过将panel3
添加到内容窗格的LINE_START
或LINE_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);
}
}