Java Swing - JPanel 和 GridLayout 边距/填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4331699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing - JPanel and GridLayout Margins/Padding
提问by dwat
I'm working on building a chess game in Java, and I'm currently having a bit of trouble getting the GUI exactly the way I want it with Swing. I'm using a GridLayout
to organize a grid of 8x8 ChessButton
s (which override the JButton
so that I can store extra information inside of them such as coordinates). Originally, the ChessButton
s wouldn't appear unless I moused over them, but I solved that problem by placing each ChessButton
inside a separate JPanel
and setting each button's setPreferredSize()
to a set height and width.
我正在用 Java 构建一个国际象棋游戏,但我目前在使用 Swing 完全按照我想要的方式获取 GUI 时遇到了一些麻烦。我正在使用 aGridLayout
来组织一个 8x8ChessButton
的网格(它覆盖了JButton
以便我可以在其中存储额外的信息,例如坐标)。最初,ChessButton
除非我将鼠标悬停在它们上,否则 s 不会出现,但是我通过将每个按钮ChessButton
放在单独的内部JPanel
并将每个按钮设置setPreferredSize()
为设置的高度和宽度来解决该问题。
Now, my problem is that there seems to be a small margin or padding above (and/or below?) each button. I've made sure to set setHgap(0)
and setVgap(0)
for the GridLayout
, so I'm pretty sure the mysterious margin is coming from either the buttons or the JPanel
s. But, I can't seem to get rid of them, and they seem to be causing each ChessButton
to shift a little bit up/down whenever I mouse of them.
现在,我的问题是每个按钮上方(和/或下方?)似乎有一个小的边距或填充。我一定要做出一套setHgap(0)
和setVgap(0)
为GridLayout
,所以我敢肯定的神秘利润率从任一按钮或未来JPanel
秒。但是,我似乎无法摆脱它们,而且ChessButton
每当我用鼠标移动它们时,它们似乎都会使每个向上/向下移动一点。
I realize this description of the problem might be a little hard to visualize, so I've taken a screenshot (using JButton
s rather than ChessButton
s so the gaps are slightly easier to recognize): http://img3.imageshack.us/img3/6656/jbuttonmargins.png
我意识到这个问题的描述可能有点难以想象,所以我截取了一个屏幕截图(使用JButton
s 而不是ChessButton
s 所以差距更容易识别):http: //img3.imageshack.us/img3/ 6656/jbuttonmargins.png
Here is the code I used to initialize each ChessButton
:
这是我用来初始化每个的代码ChessButton
:
chessBoard = new JPanel(new GridLayout(8, 8, 0, 0));
chessBoard.setBorder(BorderFactory.createEmptyBorder());
for (int i = 0; i <= 65; i++) {
//Create a new ChessButton
ChessButton button = new ChessButton("hi");
button.setBorder(BorderFactory.createEmptyBorder());
button.setPreferredSize(new Dimension(75, 75));
button.setMargin(new Insets(0, 0, 0, 0));
//Create a new JPanel that the ChessButton will go into
JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(75, 75));
buttonPanel.setBorder(BorderFactory.createEmptyBorder());
buttonPanel.add(button);
//Add the buttonPanel to the grid
chessBoard.add(buttonPanel);
}
So, how can I get rid of these vertical spaces between buttons? I'm relatively new to Swing, so I'm sorry if the answer is extremely obvious, but I'd appreciate any help anyone might have to offer! Thanks in advance!
那么,我怎样才能摆脱按钮之间的这些垂直空间呢?我对 Swing 比较陌生,所以如果答案非常明显,我很抱歉,但我很感激任何人可能需要提供的任何帮助!提前致谢!
采纳答案by camickr
Originally, the ChessButtons wouldn't appear unless I moused over them, but I solved that problem by placing each ChessButton inside a separate JPanel and setting each button's setPreferredSize() to a set height and width
最初,除非我将鼠标悬停在 ChessButtons 上,否则它们不会出现,但我通过将每个 ChessButton 放在单独的 JPanel 中并将每个按钮的 setPreferredSize() 设置为设置的高度和宽度来解决该问题
That is not the proper solution. There is no reason to use a JPanel to hold the buttons. In fact, this is probably the cause of the problem. The buttons should show up when you add them to a GridLayout. If they don't show up its probably because you added the buttons to the GUI after making the GUI visible. Components should be added to the GUI BEFORE it is made visible.
这不是正确的解决方案。没有理由使用 JPanel 来固定按钮。事实上,这可能是问题的原因。当您将按钮添加到 GridLayout 时,它们应该会显示出来。如果它们没有出现,可能是因为您在使 GUI 可见后将按钮添加到了 GUI。组件应在 GUI 可见之前添加到 GUI。
Now, my problem is that there seems to be a small margin or padding above (and/or below?) each button
现在,我的问题是每个按钮上方(和/或下方?)似乎有一个小的边距或填充
I don't understand why there also isn't a horizontal gap. When you create a JPanel, by default it uses a FlowLayout which also contains a horizontal/vertical gap of 5 pixels. So I understand why you might have the vertical gap of 10 pixels. I don't understand why there is no horizontal gap.
我不明白为什么也没有水平差距。创建 JPanel 时,默认情况下它使用 FlowLayout,其中还包含 5 个像素的水平/垂直间隙。所以我明白为什么你可能会有 10 像素的垂直间隙。我不明白为什么没有横向间隙。
If you need more help post your SSCCEdemonstrating the problem. And the SSCCE should use regular JButtons. Get the basics working with standard components before you start playing with custom components. That way you know if the problem is with your custom code or not.
如果您需要更多帮助,请发布您的SSCCE 来演示问题。并且 SSCCE 应该使用常规的 JButton。在开始使用自定义组件之前,先了解使用标准组件的基础知识。这样您就知道问题是否出在您的自定义代码上。
回答by Patrick Gombert
Try adding chessBoard.setPreferredSize(600, 600) to create a JPanel for the board that only has room to fit the buttons (8 buttons each way * 75 size each way on the buttons).
尝试添加 chessBoard.setPreferredSize(600, 600) 为只有空间容纳按钮的棋盘创建一个 JPanel(每方向 8 个按钮 * 按钮上的每方向 75 大小)。
回答by trashgod
Don't add an empty border; do use setBorderPainted(false)
.
不要添加空边框;使用setBorderPainted(false)
.
Addendum: As @camickr notes, the panel's layout may include default gaps. The example below uses no-gap GridLayout
accordingly.
附录:正如@camickr 所指出的,面板的布局可能包含默认间隙。下面的示例相应地使用了无间隙GridLayout
。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** @see http://stackoverflow.com/questions/4331699 */
public class ButtonBorder extends JPanel {
private static final int N = 8;
private static final int SIZE = 75;
public ButtonBorder() {
super(new GridLayout(N, N));
this.setPreferredSize(new Dimension(N * SIZE, N * SIZE));
for (int i = 0; i < N * N; i++) {
this.add(new ChessButton(i));
}
}
private static class ChessButton extends JButton {
public ChessButton(int i) {
super(i / N + "," + i % N);
this.setOpaque(true);
this.setBorderPainted(false);
if ((i / N + i % N) % 2 == 1) {
this.setBackground(Color.gray);
}
}
}
private void display() {
JFrame f = new JFrame("ButtonBorder");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ButtonBorder().display();
}
});
}
}