Box容器

来源:互联网 发布:浙江大学网络教育平台 编辑:程序博客网 时间:2024/05/20 20:18

Box容器是天生以BoxLayout为布局管理器的容器,下例展示了它的用法。

(1)Box1.java

//<applet code=Box1 width=450 height=200></applet>

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

public class Box1 extends JApplet
{
   public void init()
   {
      Box bv=Box.createVerticalBox();
      for(int i=0;i<5;i++)
      {
         bv.add(new JButton("bv "+i));
      }
      Box bh=Box.createHorizontalBox();
      for(int i=0;i<5;i++)
      {
         bh.add(new JButton("bh "+i));
      }
      Container cp=getContentPane();
      cp.add(BorderLayout.EAST,bv);
      cp.add(BorderLayout.SOUTH,bh);
   }
   public static void main(String[] args)
   {
      Console.run(new Box1(),450,200);
   }
}

(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);
   }
}

原创粉丝点击