正文

Layout用法2005-09-22 17:20:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hurongliang/5109.html

分享到:

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.Box;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import java.awt.event.*;

public class LayoutDemo
{
 public static void main(String[] args)
 {
  JFrame frame = new JFrame ("Layout Manager Demo");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  
  JTabbedPane tp = new JTabbedPane();
  tp.addTab ("Flow",new FlowPanel());
  tp.addTab ("Border",new BorderPanel());
  tp.addTab ("Grid",new GridPanel());
  tp.addTab ("Box",new BoxPanel());
  
  frame.setContentPane(tp);
  frame.pack();
  frame.show();
 }
}

//定义流式布局管理器
class FlowPanel extends JPanel
{
 public FlowPanel ()
 {
  /*
  构造方法:
  1: FlowLayout ()
  2: FlowLayout (int align)
  3: FlowLayout (int align, int hgap, int vgap)
  参数说明:
  align 表示一行的对齐方式.有左对齐FlowLayout.LEFT,居中对齐FlowLayout.CENTER,右对齐FlowLayout.RIGHT
  hgap:组件的水平间隙,vgap:组件的垂直间隙
  */
  FlowLayout layout = new FlowLayout (FlowLayout.LEFT, 10,50);
  layout.layoutContainer (this);//参数指明应用的对象容器.同于:setLayout (layout);
  layout.setAlignment (FlowLayout.CENTER);
  layout.setHgap (50);
  layout.setVgap (20);
  
  setBackground (Color.green);
  
  JButton b1 = new JButton ("BUTTON 1");
  JButton b2 = new JButton ("BUTTON 2");
  JButton b3 = new JButton ("BUTTON 3");
  JButton b4 = new JButton ("BUTTON 4");
  JButton b5 = new JButton ("BUTTON 5");
  
  add (b1);
  add (b2);
  add (b3);
  add (b4);
  add (b5);
 }
}

 

 

 

 


//定义边界布局管理器

class BorderPanel extends JPanel
{
 private ImageIcon icon = new ImageIcon("Image//layoutDemodriveWay.gif");
 JButton west = new JButton ("西");
 JButton north = new JButton ("北");
 JButton south = new JButton ("南");
 JButton east = new JButton ("东");
 JLabel center = new JLabel ("", icon,SwingConstants.CENTER);
 
 public BorderPanel()
 {
  setLayout (new BorderLayout());
  
  setBackground (Color.black);
  west.addActionListener (new Button1Listener());
  north.addActionListener (new Button2Listener());
  south.addActionListener (new Button3Listener());
  east.addActionListener (new Button4Listener());
  
  
  /*将组件添加到边界布局的容器中需要指明位置如果不指明则
  默认为BorderLayout.CENTER,可以覆盖*/
  
  add (west,BorderLayout.WEST);
  add (north,BorderLayout.NORTH);
  add (south,BorderLayout.SOUTH);
  add (east,BorderLayout.EAST);
  add (center,BorderLayout.CENTER);
  
 }

 private class Button1Listener implements ActionListener
 {
  public void actionPerformed (ActionEvent event)
  {
   center.setIcon (new ImageIcon("Image//layoutDemoarrowLeft.gif"));
   repaint();
  }
 }

 private class Button2Listener implements ActionListener
 {
  public void actionPerformed (ActionEvent event)
  {
   center.setIcon(new ImageIcon("Image//layoutDemoarrowUp.gif"));
   repaint();
  }
 }

 private class Button3Listener implements ActionListener
 {
  public void actionPerformed (ActionEvent event)
  {
   center.setIcon(new ImageIcon("Image//layoutDemoarrowDown.gif"));
   repaint();
  }
 }

 private class Button4Listener implements ActionListener
 {
  public void actionPerformed (ActionEvent event)
  {
   center.setIcon(new ImageIcon("Image//layoutDemoarrowRight.gif"));
   repaint();
  }
 }
}

 

//定义盒式布局管理器
class BoxPanel extends JPanel
{
 public BoxPanel()
 {
  /*
  就一个构造函数
  BoxLayout (Container target, int axis)
  参数说明:
  target指明要设置次布局方式的容器
  axis设置布局方式,有如下选择:
  BoxLayout.X_AXIS从左到右
  BoxLayout.Y_AXIS从上到下
  BoxLayout.LINE_AXIS
  BoxLayout.PAGE_AXIS
  
  
  */
  setLayout (new BoxLayout (this, BoxLayout.X_AXIS));
  
  setBackground (Color.green);
  
  JButton b1 = new JButton ("BUTTON 1");
  JButton b2 = new JButton ("BUTTON 2");
  JButton b3 = new JButton ("BUTTON 3");
  JButton b4 = new JButton ("BUTTON 4");
  JButton b5 = new JButton ("BUTTON 5");
  /*
  盒式布局管理器的组件间没有间距,可以添加不可见组件到容器中去以便占用空间
  Box类包含静态方法可创建不可见组件,他是在包java.swing中
  Box类定义两种组件:固定大小的和可以伸缩的
  Box的一些静态方法:
  createGlue()//伸缩的,可水平伸缩也可垂直伸缩
  createRigidArea(Dimension d)//固定水平垂直大小
  createHorizontalGlue()//水平伸缩
  createHorizontalStrut(int width)//固定水平大小
  createVerticalGlue()//垂直伸缩
  createVerticalStrut(int height)//固定垂直大小
  
  */
  add (Box.createHorizontalGlue());
  add (b1);
  add (Box.createRigidArea (new Dimension (10,0)));
  add (b2);
  add (b3);
  add (Box.createHorizontalStrut (50));
  add (b4);
  add (Box.createHorizontalGlue());
  add (b5);
 }

//定义网格布局管理器
class GridPanel extends JPanel
{
 public GridPanel()
 {
  /*
  构造函数:
  GridLayout()//单行多列
  GridLayout(int rows, int cols)
  GridLayout(int rows, int cols, int hgap, int vgap)//行列加水平垂直间隔
  
  
  
  
  */
  
  GridLayout layout = new GridLayout (2,3);
  layout.setRows (3);//当构造函数中已设置了行时此方法无效
  layout.setColumns (4);//当构造函数中已设置了列时此方法无效
  layout.setHgap (5);
  layout.setVgap (5);
  setLayout (layout);
  setBackground (Color.white);
  
  JButton b1 = new JButton ("BUTTON 1");
  JButton b2 = new JButton ("BUTTON 2");
  JButton b3 = new JButton ("BUTTON 3");
  JButton b4 = new JButton ("BUTTON 4");
  JButton b5 = new JButton ("BUTTON 5");
  JButton b6 = new JButton ("BUTTON 6");
  
  
  add (b1);
  add (b2);
  add (b3);
  add (b4);
  add (b5);
  add (b6);
 }
}

阅读(4934) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册