新手请教设置JPanel之间距离问题

zhouyang209117 2011-11-07 05:54:11
大家好。在一个JFrame上添加两个JPanel,想设置两个JPanel的距离,
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 20, 20);
我想通过改变FlowLayout()的第二,三个参数改变,但不是起作用,下面是代码:
package ex;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ex12_1 extends JFrame
{
public Ex12_1()
{
//试图通过这一行改变JPanel的距离,但不起作用。
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 20, 20);
setLayout(flowLayout);

JPanel p1 = new JPanel();
p1.add(new JButton("Button1"));
p1.add(new JButton("Button2"));
p1.add(new JButton("Button3"));
add(p1, FlowLayout.LEFT);
//add(p1);

JPanel p2 = new JPanel();
p1.add(new JButton("Button4"));
p1.add(new JButton("Button5"));
p1.add(new JButton("Button6"));
add(p2, FlowLayout.LEFT);
//add(p2);

System.out.println(flowLayout.getHgap());
System.out.println(flowLayout.getVgap());

}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Ex12_1 frame = new Ex12_1();
frame.setTitle("Exercise12_1");
frame.setSize(1000, 600);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
...全文
563 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
huntor 2011-11-07
  • 打赏
  • 举报
回复
添加 Box.createHorizontalStrut(int);
or Box.createVerticalStrut(int);
benlaten 2011-11-07
  • 打赏
  • 举报
回复
两个这Panel放到一个Panel中 在把这个Panel设置边框就OK

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧