Layout

LayoutDemo.java
001 package cn.rolia.learning.swing;
002 import java.awt.FlowLayout;
003 import java.awt.GridLayout;
004 import java.awt.BorderLayout;
005 import java.awt.Color;
006 import java.awt.Dimension;
007 import javax.swing.ImageIcon;
008 import javax.swing.Box;
009 import javax.swing.SwingConstants;
010 import javax.swing.BoxLayout;
011 import javax.swing.JFrame;
012 import javax.swing.JLabel;
013 import javax.swing.JPanel;
014 import javax.swing.JTabbedPane;
015 import javax.swing.JButton;
016 import java.awt.event.*;
017 
018 public class LayoutDemo
019 {
020  public static void main(String[] args)
021  {
022   JFrame frame = new JFrame ("Layout Manager Demo");
023   frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
024   
025   JTabbedPane tp = new JTabbedPane();
026   tp.addTab ("Flow",new FlowPanel());
027   tp.addTab ("Border",new BorderPanel());
028   tp.addTab ("Grid",new GridPanel());
029   tp.addTab ("Box",new BoxPanel());
030   
031   frame.setContentPane(tp);
032   frame.pack();
033   frame.setVisible(true);
034  }
035 }
036 
037 //定义流式布局管理器
038 class FlowPanel extends JPanel
039 {
040  /**
041    
042    */
043   private static final long serialVersionUID = 1L;
044 
045 public FlowPanel ()
046  {
047   /*
048   构造方法:
049   1: FlowLayout ()
050   2: FlowLayout (int align)
051   3: FlowLayout (int align, int hgap, int vgap)
052   参数说明:
053   align 表示一行的对齐方式.有左对齐FlowLayout.LEFT,居中对齐FlowLayout.CENTER,右对齐FlowLayout.RIGHT
054   hgap:组件的水平间隙,vgap:组件的垂直间隙
055   */
056   FlowLayout layout = new FlowLayout (FlowLayout.LEFT, 10,50);
057   layout.layoutContainer (this);//参数指明应用的对象容器.同于:setLayout (layout);
058   layout.setAlignment (FlowLayout.CENTER);
059   layout.setHgap (50);
060   layout.setVgap (20);
061   
062   setBackground (Color.green);
063   
064   JButton b1 = new JButton ("BUTTON 1");
065   JButton b2 = new JButton ("BUTTON 2");
066   JButton b3 = new JButton ("BUTTON 3");
067   JButton b4 = new JButton ("BUTTON 4");
068   JButton b5 = new JButton ("BUTTON 5");
069   
070   add (b1);
071   add (b2);
072   add (b3);
073   add (b4);
074   add (b5);
075  }
076 }
077 
078  
079 
080  
081 
082  
083 
084  
085 
086 
087 //定义边界布局管理器
088 
089 class BorderPanel extends JPanel
090 {
091  /**
092    
093    */
094   private static final long serialVersionUID = 1L;
095 private ImageIcon icon = new ImageIcon("Image//layoutDemodriveWay.gif");
096  JButton west = new JButton ("西");
097  JButton north = new JButton ("北");
098  JButton south = new JButton ("南");
099  JButton east = new JButton ("东");
100  JLabel center = new JLabel ("", icon,SwingConstants.CENTER);
101  
102  public BorderPanel()
103  {
104   setLayout (new BorderLayout());
105   
106   setBackground (Color.black);
107   west.addActionListener (new Button1Listener());
108   north.addActionListener (new Button2Listener());
109   south.addActionListener (new Button3Listener());
110   east.addActionListener (new Button4Listener());
111   
112   
113   /*将组件添加到边界布局的容器中需要指明位置如果不指明则
114   默认为BorderLayout.CENTER,可以覆盖*/
115   
116   add (west,BorderLayout.WEST);
117   add (north,BorderLayout.NORTH);
118   add (south,BorderLayout.SOUTH);
119   add (east,BorderLayout.EAST);
120   add (center,BorderLayout.CENTER);
121   
122  }
123 
124  private class Button1Listener implements ActionListener
125  {
126   public void actionPerformed (ActionEvent event)
127   {
128    center.setIcon (new ImageIcon("Image//layoutDemoarrowLeft.gif"));
129    repaint();
130   }
131  }
132 
133  private class Button2Listener implements ActionListener
134  {
135   public void actionPerformed (ActionEvent event)
136   {
137    center.setIcon(new ImageIcon("Image//layoutDemoarrowUp.gif"));
138    repaint();
139   }
140  }
141 
142  private class Button3Listener implements ActionListener
143  {
144   public void actionPerformed (ActionEvent event)
145   {
146    center.setIcon(new ImageIcon("Image//layoutDemoarrowDown.gif"));
147    repaint();
148   }
149  }
150 
151  private class Button4Listener implements ActionListener
152  {
153   public void actionPerformed (ActionEvent event)
154   {
155    center.setIcon(new ImageIcon("Image//layoutDemoarrowRight.gif"));
156    repaint();
157   }
158  }
159 }
160 
161  
162 
163 //定义盒式布局管理器
164 class BoxPanel extends JPanel
165 {
166  /**
167    
168    */
169   private static final long serialVersionUID = 1L;
170 
171 public BoxPanel()
172  {
173   /*
174   就一个构造函数
175   BoxLayout (Container target, int axis)
176   参数说明:
177   target指明要设置次布局方式的容器
178   axis设置布局方式,有如下选择:
179   BoxLayout.X_AXIS从左到右
180   BoxLayout.Y_AXIS从上到下
181   BoxLayout.LINE_AXIS
182   BoxLayout.PAGE_AXIS
183   
184   
185   */
186   setLayout (new BoxLayout (this, BoxLayout.X_AXIS));
187   
188   setBackground (Color.green);
189   
190   JButton b1 = new JButton ("BUTTON 1");
191   JButton b2 = new JButton ("BUTTON 2");
192   JButton b3 = new JButton ("BUTTON 3");
193   JButton b4 = new JButton ("BUTTON 4");
194   JButton b5 = new JButton ("BUTTON 5");
195   /*
196   盒式布局管理器的组件间没有间距,可以添加不可见组件到容器中去以便占用空间
197   Box类包含静态方法可创建不可见组件,他是在包java.swing中
198   Box类定义两种组件:固定大小的和可以伸缩的
199   Box的一些静态方法:
200   createGlue()//伸缩的,可水平伸缩也可垂直伸缩
201   createRigidArea(Dimension d)//固定水平垂直大小
202   createHorizontalGlue()//水平伸缩
203   createHorizontalStrut(int width)//固定水平大小
204   createVerticalGlue()//垂直伸缩
205   createVerticalStrut(int height)//固定垂直大小
206   
207   */
208   add (Box.createHorizontalGlue());
209   add (b1);
210   add (Box.createRigidArea (new Dimension (10,0)));
211   add (b2);
212   add (b3);
213   add (Box.createHorizontalStrut (50));
214   add (b4);
215   add (Box.createHorizontalGlue());
216   add (b5);
217  }
218 
219 
220 //定义网格布局管理器
221 class GridPanel extends JPanel
222 {
223  /**
224    
225    */
226   private static final long serialVersionUID = 1L;
227 
228 public GridPanel()
229  {
230   /*
231   构造函数:
232   GridLayout()//单行多列
233   GridLayout(int rows, int cols)
234   GridLayout(int rows, int cols, int hgap, int vgap)//行列加水平垂直间隔
235   
236   
237   
238   
239   */
240   
241   GridLayout layout = new GridLayout (2,3);
242   layout.setRows (3);//当构造函数中已设置了行时此方法无效
243   layout.setColumns (4);//当构造函数中已设置了列时此方法无效
244   layout.setHgap (5);
245   layout.setVgap (5);
246   setLayout (layout);
247   setBackground (Color.white);
248   
249   JButton b1 = new JButton ("BUTTON 1");
250   JButton b2 = new JButton ("BUTTON 2");
251   JButton b3 = new JButton ("BUTTON 3");
252   JButton b4 = new JButton ("BUTTON 4");
253   JButton b5 = new JButton ("BUTTON 5");
254   JButton b6 = new JButton ("BUTTON 6");
255   
256   
257   add (b1);
258   add (b2);
259   add (b3);
260   add (b4);
261   add (b5);
262   add (b6);
263  }
264 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值