⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 examplelayouts.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.windows;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** An example class used to demonstrate the basics of 
  * creating components such as panels, arranging
  * components using layout objects, and nesting
  * components inside each other.
  */
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 );
      addWindowListener( new WindowCloser() );

      // 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 gridbag 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( 250, 175 );
      setVisible( true );
   }

   /** The test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      new ExampleLayouts( "Example Layouts" );
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -