Java AWT 组件和面板填充/边框
我不知道为什么我找不到解决方案...我正在尝试在流布局中布局一些 AWT 组件。唯一的问题是使用流布局时组件(面板)之间的“填充”。这是该小程序当前的样子: https://i.sstatic.net/2KZgD.png
我需要一种方法来设置小程序/面板,以便两个面板(黑框)接触(无“填充”)。整个程序是免费的 Swing,全部是 AWT,我计划保持这种状态。我觉得这是一个非常简单的解决方案,但我一直无法找到答案。
这是小程序类中的 init() 代码:
public void init() {
setLayout(new FlowLayout());
c1 = new TestPanel();
c2 = new TestPanel();
c1.setPreferredSize(new Dimension(640, 480));
c2.setPreferredSize(new Dimension(100, 480));
add(c1);
add(c2);
}
这是我正在使用的 TestPanel 类:
public class TestPanel extends Panel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getPreferredSize().width, this.getPreferredSize().height);
}
}
I have no idea why I can't find a solution for this... I am trying to layout some AWT components in a flow layout. The only problem is the 'padding' between the components (Panels) when using a flow layout. This is what the applet currently looks like: https://i.sstatic.net/2KZgD.png
I need a way to set the Applet/Panels so that the two panels (black boxes) are touching (no 'padding'). The entire program is Swing free, all AWT, and I plan on keeping it that way. I feel this is a very simple solution, but I have not been able to find an answer.
This is the init() code from the applet class:
public void init() {
setLayout(new FlowLayout());
c1 = new TestPanel();
c2 = new TestPanel();
c1.setPreferredSize(new Dimension(640, 480));
c2.setPreferredSize(new Dimension(100, 480));
add(c1);
add(c2);
}
This is the TestPanel class I'm using:
public class TestPanel extends Panel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getPreferredSize().width, this.getPreferredSize().height);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FlowLayout
的默认水平(和垂直)间隙设置为5
。因此,您必须显式将水平间隙设置为0
。第一种方法
调用
setHgap(...)
在组件的布局上。由于JPanel
的默认布局是FlowLayout
,因此只需执行以下操作:第二种方法
使用另一个
FlowLayout 构造函数。即
FlowLayout(intalign、inthgap、intvgap)
。只需执行以下操作:The default horizontal (and vertical) gap of
FlowLayout
is set to5
. Therefore, you must explicitly set the horizontal gap to0
.FIRST APPROACH
Invoke
setHgap(...)
on the component's layout. Since the default layout of aJPanel
isFlowLayout
, simply do the following:SECOND APPROACH
Use another
FlowLayout
constructor. That is,FlowLayout(int align, int hgap, int vgap)
. And simply do the following: