由于您没有发布代码,我们被迫猜测(这不好)。
我的猜测:您可能正在设置某些组件或 GUI 的大小或首选大小,这会导致 GUI 边缘出现间隙。
解决方案:不要这样做。让组件通过其首选大小和布局管理器自行调整大小pack()
在 GUI 上调用。
如需更多帮助,请创建并发布您的sscce。对于这样的问题,代码确实是强制性的,实际上我有点惊讶你没有发布你的代码。
Edit 1
例如,请注意生成的 GUI 中的差异:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
public class GapExample {
private static final int M = 5;
private static final int N = 6;
private static final int PREF_W = 700;
private static final int PREF_H = 500;
@SuppressWarnings("serial")
private static void createAndShowGui() {
// *** attempt 1: set preferredSize of the mainPanel JPanel ***
JPanel mainPanel = new JPanel(new GridLayout(M, N));
mainPanel.setBorder(BorderFactory.createLineBorder(Color.red));
mainPanel.setPreferredSize(new Dimension(PREF_W, PREF_H));
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
String text = String.format("Foo [%d, %d]", i, j);
JLabel label = new JLabel(text, SwingConstants.CENTER);
label.setBorder(BorderFactory.createLineBorder(Color.blue));
mainPanel.add(label);
}
}
JOptionPane.showMessageDialog(null, mainPanel,
"Attempt 1, setPreferredSize of mainPane",
JOptionPane.PLAIN_MESSAGE);
// *** attempt 2: override the getPreferredSize of the JLabel cells in the
// grid ***
mainPanel = new JPanel(new GridLayout(M, N));
mainPanel.setBorder(BorderFactory.createLineBorder(Color.red));
final Dimension labelPrefSize = new Dimension(PREF_W / N, PREF_H / M);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
String text = String.format("Foo [%d, %d]", i, j);
JLabel label = new JLabel(text, SwingConstants.CENTER) {
@Override
public Dimension getPreferredSize() {
return labelPrefSize;
}
};
label.setBorder(BorderFactory.createLineBorder(Color.blue));
mainPanel.add(label);
}
}
JOptionPane
.showMessageDialog(null, mainPanel,
"Attempt 2, override getPreferredSize of the JLabel cells in the grid",
JOptionPane.PLAIN_MESSAGE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
在第二个对话框中,我让 JLabels 返回首选大小,并让容纳它们的容器设置显示 GUI 的最佳大小,并且在第二次尝试时,网格会更加适合。
这显示了第一次(糟糕的)尝试内部和外部组件之间的间隙:
![enter image description here](https://i.stack.imgur.com/AsPJs.jpg)
第二次(好的)尝试没有显示出这样的差距:
![enter image description here](https://i.stack.imgur.com/z7TzK.jpg)