您的位置:首页 > 其它

Box容器中的"刚性区域"概念

2009-10-01 20:57 225 查看
该例是为了学习刚性区域的用法.

(1)Box4.java

import javax.swing.*;
import java.awt.*;

public class Box4 extends JApplet
{
public void init()
{
Box bv=Box.createVerticalBox();
bv.add(new JButton("Top"));
bv.add(Box.createRigidArea(new Dimension(120,90)));
bv.add(new JButton("Bottom"));
Box bh=Box.createHorizontalBox();
bh.add(new JButton("Left"));
bh.add(Box.createRigidArea(new Dimension(160,80)));
bh.add(new JButton("Right"));
bv.add(bh);
getContentPane().add(bv);
}
public static void main(String[] args)
{
Console.run(new Box4(),450,300);
}
}

(2)Console.java

import javax.swing.*;

public class Console
{
public static String title(Object o)
{
String t=o.getClass().toString();
if(t.indexOf("class")!=-1)
{
t=t.substring(6);
}
return t;
}
public static void run(JFrame frame,int width,int height)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,height);
frame.setVisible(true);
}
public static void run(JApplet applet,int width,int height)
{
JFrame frame=new JFrame(title(applet));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width,height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void run(JPanel panel,int width,int height)
{
JFrame frame=new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width,height);
frame.setVisible(true);
}
}

注意:"刚性区域"使用了绝对值,因此有人认为这么做带来的问题比带来的好处更多,
所以,要慎用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: