现在的位置: 首页 > 综合 > 正文

Java各种布局实例

2013年04月14日 ⁄ 综合 ⁄ 共 2620字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.