JAVA BorderLayout布局

importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Dimension;importjavax... import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PanelRunner2 {
public static void main(String[] args)
{
JFrame frame = new JFrame("Color Panels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 1000);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(1200, 1000));
JPanel subpanel1 = new JPanel();
subpanel1.setBackground(Color.RED);
subpanel1.setPreferredSize(new Dimension(200, 200));
panel. add(subpanel1, BorderLayout.NORTH);
JPanel subpanel2 = new JPanel();
subpanel2.setBackground(Color.WHITE);
subpanel2.setPreferredSize(new Dimension(200, 200));
panel.add(subpanel2,BorderLayout.WEST);
JPanel subpanel3 = new JPanel();
subpanel3.setBackground(Color.BLUE);
subpanel3.setPreferredSize(new Dimension(200, 200));
panel.add(subpanel3,BorderLayout.AFTER_LAST_LINE);
JPanel subpanel4 = new JPanel();
subpanel4.setBackground(Color.GREEN);
subpanel4.setPreferredSize(new Dimension(200, 200));
panel.add(subpanel4,BorderLayout.AFTER_LAST_LINE);
JPanel subpanel5 = new JPanel();
subpanel5.setBackground(Color.YELLOW);
subpanel5.setPreferredSize(new Dimension(200, 200));
panel.add(subpanel5,BorderLayout.EAST);
JPanel subpanel6 = new JPanel();
subpanel6.setBackground(Color.ORANGE);
subpanel6.setPreferredSize(new Dimension(200,200));
panel.add(subpanel6, BorderLayout.SOUTH);
frame.add(panel);
frame.setVisible(true);
}
}
要求布局如下,
You will need to create another subpanel to hold the blue and green panels. Try running the program and making the framenarrower. What happens the panels in thecenter? Why? Fix the problem by using a GridLayout manager forthe center panel.
GridLayout不知道怎么用,求大神修改。
展开
 我来答
yugi111
2014-05-08 · TA获得超过8.1万个赞
知道大有可为答主
回答量:5.1万
采纳率:70%
帮助的人:1.3亿
展开全部
package image;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PanelRunner extends JFrame
{
private static final long serialVersionUID = 1L;

private static void initPanels ( JFrame pr )
{
for ( int i = 0; i < 5; i++ )
{
JPanel panel = new JPanel ();
switch (i)
{
case 0:
panel.setBackground (Color.RED);
panel.setPreferredSize (new Dimension (200, 100));
pr.add (panel, BorderLayout.NORTH);
break;
case 1:
panel.setBackground (Color.YELLOW);
panel.setPreferredSize (new Dimension (200, 300));
pr.add (panel, BorderLayout.EAST);
break;
case 2:
panel.setBackground (Color.ORANGE);
panel.setPreferredSize (new Dimension (200, 100));
pr.add (panel, BorderLayout.SOUTH);
break;
case 3:
panel.setBackground (Color.WHITE);
panel.setPreferredSize (new Dimension (200, 300));
pr.add (panel, BorderLayout.WEST);
break;
case 4:
pr.add (panel, BorderLayout.CENTER);
panel.setPreferredSize (new Dimension (200, 100));
panel.setLayout (new GridLayout (1, 2));
for ( int j = 0; j < 2; j++ )
{
JPanel subPanel = new JPanel ();
subPanel.setPreferredSize (new Dimension (200, 100));
Color color = j == 0 ? Color.BLUE : Color.GREEN;
subPanel.setBackground (color);
panel.add (subPanel);
}
break;
default:
break;
}
}
}

private static void initFrame ( JFrame pr )
{
pr.setLayout (new BorderLayout ());
pr.setSize (600, 300);
pr.setLocationRelativeTo (null);
pr.setResizable (false);
pr.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}

public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
PanelRunner pr = new PanelRunner ();
initFrame (pr);
initPanels (pr);
pr.setVisible (true);
}
});
}
}

miniapp1fbdq6yjd2onp
2014-05-08 · 超过17用户采纳过TA的回答
知道答主
回答量:36
采纳率:100%
帮助的人:14.3万
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PanelRunner2 {
public static void main(String[] args)
{
JFrame frame = new JFrame("Color Panels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 1000);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setPreferredSize(new Dimension(1200, 1000));
JPanel subpanel1 = new JPanel();
subpanel1.setBackground(Color.RED);
subpanel1.setPreferredSize(new Dimension(200, 200));
panel.add(subpanel1, BorderLayout.NORTH);
JPanel panelCenter = new JPanel();
panelCenter.setLayout(new GridLayout(1,4));
JPanel subpanel2 = new JPanel();
subpanel2.setBackground(Color.WHITE);
subpanel2.setPreferredSize(new Dimension(200, 200));
panelCenter.add(subpanel2, BorderLayout.WEST);

JPanel subpanel3 = new JPanel();
subpanel3.setBackground(Color.BLUE);

subpanel3.setPreferredSize(new Dimension(200, 200));

panelCenter.add(subpanel3);
JPanel subpanel4 = new JPanel();
subpanel4.setBackground(Color.GREEN);
subpanel4.setPreferredSize(new Dimension(200, 200));
panelCenter.add(subpanel4);
JPanel subpanel5 = new JPanel();
subpanel5.setBackground(Color.YELLOW);
subpanel5.setPreferredSize(new Dimension(200, 200));
panelCenter.add(subpanel5);
panel.add(panelCenter,BorderLayout.CENTER);

JPanel subpanel6 = new JPanel();
subpanel6.setBackground(Color.ORANGE);
subpanel6.setPreferredSize(new Dimension(200, 200));
panel.add(subpanel6, BorderLayout.SOUTH);
frame.add(panel);
frame.setVisible(true);
}
}
追问
中间两个只能显示一个,要求使用GridLayout修改。
追答
额!
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式