新手请教设置JPanel之间距离问题
大家好。在一个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);
}
}
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);
}
}
...全文
相关推荐
Swing控件(JPanel,Container等)设置背景图片
NULL 博文链接:https://javapub.iteye.com/blog/764681
jpanel设置背景图片的二个小例子
主要介绍了jpanel设置背景图片的二个小例子,实现了动态加载图片做背景的方法,需要的朋友可以参考下
swing多个jpanel布局切换实例
swing多个布局切换实例,就是一个小demo
JFrame中添加和设置JPanel的方法实例解析
主要介绍了JFrame中添加和设置JPanel的方法实例解析,具有一定借鉴价值
可插入背景图片的JPanel
Java GUI 编程中,无法像C#一样向一个Panel中插入背景图片,这里在网上搜索了一个可以插入背景的Jpanel。 跟大家共享一下,也许你已经下载过,但主要还是方便那些还没有找到的人。


Java SE
