Box容器中的"胶水"概念

该例是为了学习胶水的用法.胶水是尽可能地将组件分离开。

(1)Box3.java

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

public class Box3 extends JApplet
{
    public void init()
    {
        Box bv=Box.createVerticalBox();
        bv.add(new JLabel("Hello"));
        bv.add(Box.createVerticalGlue());
        bv.add(new JLabel("Applet"));
        bv.add(Box.createVerticalGlue());
        bv.add(new JLabel("World"));
        Box bh=Box.createHorizontalBox();
        bh.add(new JLabel("Hello"));
        bh.add(Box.createHorizontalGlue());
        bh.add(new JLabel("Applet"));
        bh.add(Box.createHorizontalGlue());
        bh.add(new JLabel("World"));
        bv.add(Box.createVerticalGlue());
        bv.add(bh);
        bv.add(Box.createVerticalGlue());
        getContentPane().add(bv);
    }
    public static void main(String[] args)
    {
        Console.run(new Box3(),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);
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值