java 摆脱JPanels之间的差距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7161430/
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
Get rid of the gap between JPanels
提问by bili
Here is my problem
这是我的问题
How can I get rid of the gap from the inner rectangle box (it is actually the border of the middle JPanel)?
我怎样才能摆脱内部矩形框的间隙(它实际上是中间JPanel的边框)?
The outter rectangle is a extends JCompoment. Inside it contains three JPanel
s. Each of them use GridLayout. I even try to setHgap to a negative value in the big rectangle, but it doesn't change anything.
外部矩形是一个extends JCompoment。它里面包含三个JPanel
s。他们每个人都使用 GridLayout。我什至尝试在大矩形中将 Hgap 设置为负值,但它不会改变任何东西。
Edit: Sorry the question is not clear. I do want the border, but I don't want the gap between the inner border and outer border. If there is no gap in between, the whole rectangle will represent a class diagram class.
编辑:对不起,问题不清楚。我确实想要边框,但我不想要内边框和外边框之间的间隙。如果两者之间没有间隙,则整个矩形将代表一个类图类。
回答by mKorbel
You may mean this way:
你的意思可能是这样:
import java.awt.*;
import javax.swing.*;
public class GridBadFrame {
private JFrame frame;
private JPanel pnlCenter;
private JPanel pnl1;
private JPanel pnl2;
private JPanel pnl3;
public GridBadFrame() {
pnl1 = new JPanel();
pnl1.setBackground(Color.red);
pnl2 = new JPanel();
pnl2.setBackground(Color.blue);
pnl3 = new JPanel();
pnl3.setBackground(Color.red);
JLabel lblWest = new JLabel();
lblWest.setPreferredSize(new Dimension(50, 150));
JLabel lblEast = new JLabel();
lblEast.setPreferredSize(new Dimension(50, 150));
JLabel lblNorth = new JLabel();
lblNorth.setPreferredSize(new Dimension(600, 50));
JLabel lblSouth = new JLabel();
lblSouth.setPreferredSize(new Dimension(600, 50));
pnlCenter = new JPanel();
pnlCenter.setBackground(Color.black);
pnlCenter.setLayout(new java.awt.GridLayout(3, 0, 10, 10));
pnlCenter.setPreferredSize(new Dimension(600, 400));
pnlCenter.add(pnl1);
pnlCenter.add(pnl2);
pnlCenter.add(pnl3);
frame = new JFrame();
frame.add(pnlCenter, BorderLayout.CENTER);
frame.add(lblNorth, BorderLayout.NORTH);
frame.add(lblSouth, BorderLayout.SOUTH);
frame.add(lblWest, BorderLayout.WEST);
frame.add(lblEast, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GridBadFrame gridBadFrame = new GridBadFrame();
}
});
}
}
回答by Thomas
it is actually the border of the middle JPanel
其实就是中间JPanel的边框
If it is the border, remove that (setBorder(null)
).
如果是边框,则将其移除 ( setBorder(null)
)。
回答by Ashkan Aryan
I think if you try setBorderPainted(false) it may get rid of the unwanted border.
我认为如果您尝试 setBorderPainted(false) 它可能会摆脱不需要的边框。
回答by gotomanners
The issue is probably to do with the preferred height and width set by the GridLayout on the JPanel.
问题可能与 JPanel 上的 GridLayout 设置的首选高度和宽度有关。
Excerpt from the documentation:
文档摘录:
The preferred width of a grid layout is the largest preferred width of any of the components in the container times the number of columns, plus the horizontal padding times the number of columns plus one, plus the left and right insets of the target container.
The preferred height of a grid layout is the largest preferred height of any of the components in the container times the number of rows, plus the vertical padding times the number of rows plus one, plus the top and bottom insets of the target container.
网格布局的首选宽度是容器中任何组件的最大首选宽度乘以列数,加上水平填充乘以列数加一,再加上目标容器的左右插图。
网格布局的首选高度是容器中任何组件的最大首选高度乘以行数,加上垂直填充乘以行数加一,再加上目标容器的顶部和底部插入。