关于用Boxlayout进行页面布局时,我添加的panel会默认占满整个页面,我想让2个panel上下紧凑点
代码如下:publicstaticvoidmain(String[]args){JPanelcenterp=newJPanel();centerp.setLayout(n...
代码如下:
public static void main(String[] args) {
JPanel centerp=new JPanel();
centerp.setLayout(new BoxLayout(centerp,BoxLayout.Y_AXIS));
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.add(new JLabel("111"));
p2.add(new JLabel("222"));
centerp.add(p1);
centerp.add(p2);
JFrame f=new JFrame();
f.add(centerp);
f.setVisible(true);
} 展开
public static void main(String[] args) {
JPanel centerp=new JPanel();
centerp.setLayout(new BoxLayout(centerp,BoxLayout.Y_AXIS));
JPanel p1=new JPanel();
JPanel p2=new JPanel();
p1.add(new JLabel("111"));
p2.add(new JLabel("222"));
centerp.add(p1);
centerp.add(p2);
JFrame f=new JFrame();
f.add(centerp);
f.setVisible(true);
} 展开
3个回答
2015-03-03
展开全部
回答完毕,采纳即可呀
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class YuGiOh
{
public static void main ( String[] args )
{
JPanel centerp = new JPanel ();
centerp.setLayout (new BoxLayout (centerp, BoxLayout.Y_AXIS));
JPanel p1 = new JPanel ();
p1.setPreferredSize (new Dimension (110, 110));
p1.setBackground (Color.RED);
JPanel p2 = new JPanel ();
p2.setPreferredSize (new Dimension (110, 110));
p2.setBackground (Color.BLUE);
p1.add (new JLabel ("111"));
p2.add (new JLabel ("222"));
centerp.add (p1);
centerp.add (p2);
JFrame f = new JFrame ();
f.setLayout (new FlowLayout (FlowLayout.CENTER));
f.setSize (600, 400);
f.setResizable (false);
f.setLocationRelativeTo (null);
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
f.add (centerp);
f.setVisible (true);
}
}
展开全部
你给centerp添加个空边框试试效果
centerp.setBorder (BorderFactory.createEmptyBorder (10,10,10,10));
centerp.setBorder (BorderFactory.createEmptyBorder (10,10,10,10));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这不是boxlayout的特性吗?或者你换一种布局也可以..
追问
我想不到别的布局了,我试过gridlayout(),也是默认填满的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询