【发布时间】:2015-12-09 01:07:53
【问题描述】:
我编写了这个令人烦恼的源代码来演示another question 中提到的游戏屏幕布局。它将按钮(或标签,在启动时可选择)放入GridBagLayout。
如果您在出现提示时(在 GUI 出现之前)选择不使用按钮,则整个 GUI 会非常美观且紧凑,没有任何间隙。但是如果你选择使用按钮,它会(如果你的设置和我的一样)看起来像这样..
注意红色水平线。那是面板的BG颜色显示出来。当 GUI 使用标签时,看不到这些行。稍微拉伸一下 GUI,看看它甚至没有在每一行(有九行)之后放置一条红线 - 尽管每一行都使用按钮(相同的组件)。
使用按钮时如何去除多余的垂直空间?
我想这一定是我在配置按钮或行权重时忘记做的事情,但我不知道是什么!
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;
public class SoccerField {
private JPanel ui = null;
int[] x = {0, 35, 70, 107, 142, 177, 212, 247, 282, 315};
int[] y = {0, 45, 85, 140, 180, 225, 265, 280, 320, 345};
boolean buttons;
SoccerField() {
initUI();
}
public void initUI() {
int result = JOptionPane.showConfirmDialog(ui, "Use buttons?");
buttons = result == JOptionPane.OK_OPTION;
if (ui != null) {
return;
}
ui = new JPanel(new GridBagLayout());
ui.setBackground(Color.RED);
try {
URL url = new URL("http://i.stack.imgur.com/9E5ky.jpg");
BufferedImage img = ImageIO.read(url);
BufferedImage field = img.getSubimage(100, 350, 315, 345);
BufferedImage[] bi = subSampleImageColumns(field);
BufferedImage[][] fieldParts = new BufferedImage[bi.length][];
for (int ii=0; ii<bi.length; ii++) {
fieldParts[ii] = subSampleImageRows(bi[ii]);
}
for (int ii=0; ii<fieldParts[0].length; ii++) {
for (int jj=0; jj<fieldParts.length; jj++) {
addImageToPanel(ui, fieldParts[ii][jj], ii, jj);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void addImageToPanel(JPanel panel, BufferedImage img, int row, int col) {
Insets insets = new Insets(0,0,0,0);
GridBagConstraints gbc = new GridBagConstraints(
row, col,
1, 1,
.5, .5,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
insets, 0, 0);
ImageIcon ii = new ImageIcon(img);
JButton b = new JButton(ii);
b.setBorder(null);
b.setBorderPainted(false);
b.setContentAreaFilled(false);
Component c = buttons ? b : new JLabel(ii);
panel.add(c, gbc);
}
private BufferedImage[] subSampleImageColumns(BufferedImage img) {
System.out.println("Image Size: " + img.getWidth() + "," + img.getHeight());
BufferedImage[] imageRows = new BufferedImage[x.length - 1];
for (int ii = 0; ii < x.length - 1; ii++) {
BufferedImage bi = img.getSubimage(
x[ii], 0, x[ii + 1] - x[ii], img.getHeight());
imageRows[ii] = bi;
}
return imageRows;
}
private BufferedImage[] subSampleImageRows(BufferedImage img) {
BufferedImage[] imageRows = new BufferedImage[y.length - 1];
for (int ii = 0; ii < y.length - 1; ii++) {
BufferedImage bi = img.getSubimage(
0, y[ii], img.getWidth(), y[ii + 1] - y[ii]);
imageRows[ii] = bi;
}
return imageRows;
}
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) {
}
SoccerField o = new SoccerField();
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);
}
}
【问题讨论】:
-
愚蠢的问题:在设置按钮的边框时,您是否尝试过指定
EmptyBorder(所有 4 个值的插入为 0)而不是null?它有什么改变吗? -
@Laf 不,这不是一个愚蠢的问题,是的,我确实有。一样的效果。我没有提供这样做的变体的唯一原因是懒于添加功能(而不是仅仅更改一行代码并重新编译),然后在每次运行时选择一个或另一个。 ;)
-
@Laf 顺便说一句,该代码是完全独立的,因此人们应该很容易运行。它在运行时直接从 imgur 拉取图像。
-
是的,我刚刚尝试了您的代码。无论您是使用按钮还是标签运行,在调用
pack ()后,框架的大小似乎都不同。还在努力看看能不能快点找到。
标签: java swing jbutton layout-manager gridbaglayout