Java各种布局实例

package examples.windows;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** An example class used to demonstrate the basics of
* using many of the available layout managers by
* positioning JButton objects
*/
public class ExampleLayouts extends JFrame {
   private JPanel flow
      = new JPanel( new FlowLayout( FlowLayout.CENTER ) );
   private Box box = new Box( BoxLayout.Y_AXIS );
   private JPanel boxPanel = new JPanel();
   private JPanel grid
      = new JPanel( new GridLayout( 3, 2 ) );
   private JPanel gridBag
      = new JPanel( new GridBagLayout() );
   private JPanel border
      = new JPanel( new BorderLayout() );

   /** Class constructor method
     * @param titleText Window's title bar text
     */
   public ExampleLayouts( String titleText ) {
      super( titleText );
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      // Add the buttons to the flow layout
      flow.add( new JButton( "One" ) );
      flow.add( new JButton( "Two" ) );
      flow.add( new JButton( "Three" ) );
      flow.add( new JButton( "Four" ) );
      // Add the buttons to the box
      box.add( new JButton( "One" ) );
      box.add( new JButton( "Two" ) );
      box.add( new JButton( "Three" ) );
      box.add( new JButton( "Four" ) );
      // Add the buttons to the grid layout
      grid.add( new JButton( "One" ) );
      grid.add( new JButton( "Two" ) );
      grid.add( new JButton( "Three" ) );
      grid.add( new JButton( "Four" ) );
      grid.add( new JButton( "Five" ) );
      grid.add( new JButton( "Six" ) );
      // Add the buttons to the grid-bag layout
      GridBagConstraints c = new GridBagConstraints();
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;
      c.weighty = 1.0;
      c.gridwidth = GridBagConstraints.REMAINDER;
      gridBag.add( new JButton( "One" ), c );
      c.gridy = 1;
      c.gridx = 1;
      gridBag.add( new JButton( "Two" ), c );
      c.gridy = 2;
      gridBag.add( new JButton( "Three" ), c );
      c.gridy = 1;
      c.gridx = 0;
      c.gridheight = 2;
      c.gridwidth = 1;
      gridBag.add( new JButton( "Four" ), c );
      // Add the buttons to the border layout
      border.add( new JButton( "One" ),
                  BorderLayout.NORTH );
      border.add( new JButton( "Two" ),
                  BorderLayout.WEST );
      border.add( new JButton( "Three" ),
                  BorderLayout.CENTER );
      border.add( new JButton( "Four" ),
                  BorderLayout.EAST );
      border.add( new JButton( "Five" ),
                  BorderLayout.SOUTH );
      // create a tabbed pane and put the panels into it
      JTabbedPane tp = new JTabbedPane();
      tp.addTab( "Flow", flow );
      boxPanel.add( box );
      tp.addTab( "Box", boxPanel );
      tp.addTab( "Grid", grid );
      tp.addTab( "GridBag", gridBag );
      tp.addTab( "Border", border );
      setContentPane( tp );
      setSize( 300, 175 );
      setVisible( true );
   }
   /** The test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      new ExampleLayouts( "Example Layouts" );
   }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值