Swing的版面管理器

1.先说说Swing中的不一般的组件
JFrame,JDialog,JWindow,JApplet这四个组件是重量级组件,因为要在操作系统中显示窗口画面,必须使用操作系统的窗口资源,所以JFrame 是继承自AWT里面的Frame的,有本地C代码.
JFrame,JDialog,JWindow,JApplet这四个组件是最上层组件,其余的Swing组件都必须依附在这四个组件之上才能显示出来.这四个组件实现了RootPaneContainer接口,RootPaneContainer接口定义了各种容器取得与设置的方法,这里的各种容器指的是JRootPane(虚拟的容器),GlassPane,ContentPane,我们要在最上层组件上加入任何组件只能在GlassPane和ContentPane上面增加,也就是在Layered Pane上面或者在Layered Pane的ContentPane上面增加.
2.RootPaneContainer接口的定义
查看Java的doc,可以知道的(有问题第一反映应该是看JDK的文档,要学会学习)
通过查看文档我们可以发现:
(1).共有五个类实现了RootPaneContainer接口,他们是:JFrame,JApplet,JWindow,JDialog,JInternalFrame.值得注意的是其中的JInternalFrame是轻量级组件,
不能单独显示的
(2).RootPanelContainer有如下的几种方法
Container         getContentPane();
Component      getGlassPane();
JLayeredPane   getLayeredPane();
JRootPane       getRootPane();
void setContentPane( Container  contentPane);
void setGlassPane( Component  glassPane);
void setLayeredPane( JLayeredPane  layeredPane);
 
3.JRootPane的介绍
请大家自己查看JDK的文档,不要偷懒.
我就大概谈一下:
JRootPane是由Glass Pane与LayeredPane组成的,是虚拟的容器,不能在上面加任何组件.
有好同志会问不能加任何组件那用来干嘛的??
因为它是用来让最上层组件能加入别的组件的。几个最上层组件里面都有JRootPane组件。
4.Layered Pane是什么东东??
很多写过Swing程序的新手都没有接触到Layered Pane,和他们接触最多的就是Content Pane了,那么Layered Pane是什么东东呢??Layered Pane就像家中放鞋子的鞋架,有很多层,而ContetPane则是其中的一层,一般我们只要对这一层进行操作就行了!!
 
 5.Layout Manager的版面管理器
(1).BorderLayout

 使用可以去看JDK文档,

package MyJava.Base;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FrameBorderLayout extends JFrame

 private JPanel jPanel;
 private BorderLayout bl;
 private JButton button1=new JButton("Center");
 private JButton button2=new JButton("East");
 private JButton button3=new JButton("West");
 private JButton button4=new JButton("South");
 private JButton button5=new JButton("North");
 
 FrameBorderLayout()
 { 
  super("This is an example of FrameBorderLayout");
  setSize(400,300);
  
  bl=new BorderLayout();
  
  jPanel=new JPanel();
  jPanel.setLayout(bl);
  jPanel.add(button1,BorderLayout.CENTER);
  jPanel.add(button2,BorderLayout.EAST);
  jPanel.add(button3,BorderLayout.WEST);
  jPanel.add(button4,BorderLayout.SOUTH);
  jPanel.add(button5,BorderLayout.NORTH);
 
  this.setContentPane(jPanel);
 
 }
 public static void main(String[] args)
 { 
  FrameBorderLayout fr=new FrameBorderLayout();
  fr.show();
  fr.addWindowListener(new WindowAdapter()
     {
      public void windowClosing(WindowEvent e)
      { 
       System.exit(0); 
       
      } 
     }
        );
 
 }

 

}
程序运行的结果:

 

(2).FlowLayout

package MyJava.Base;

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;

public class FrameFlowLayout extends JFrame
{
    private JButton JButton1=new JButton("Flow1");
    private JButton JButton2=new JButton("Flow2");
    private JButton JButton3=new JButton("Flow3");
    private JButton JButton4=new JButton("Flow4");
    private JButton JButton5=new JButton("Flow5");
   
    public FrameFlowLayout()
    {
        super("This is an example of FrameFlowLayout");
        setSize(400,300);
       
        FlowLayout f1=new FlowLayout(FlowLayout.TRAILING,10,10);
        JPanel jpanel1=new JPanel();
        jpanel1.setLayout(f1);
        jpanel1.add(JButton1);
        jpanel1.add(JButton2);
        jpanel1.add(JButton3);
        jpanel1.add(JButton4);
        jpanel1.add(JButton5);
        this.setContentPane(jpanel1);
        }
    public static void main(String args[]){
        FrameFlowLayout ff1=new FrameFlowLayout();
        ff1.show();
        ff1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        }
    }

 

运行结果:

 

(3).GridLayout

 

package MyJava.Base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class FrameGridLayout extends JFrame
{
    private JPanel jpanel1;
    private GridLayout g1;
    private JButton JButton1=new JButton("Flow1");
    private JButton JButton2=new JButton("Flow2");
    private JButton JButton3=new JButton("Flow3");
    private JButton JButton4=new JButton("Flow4");
    private JButton JButton5=new JButton("Flow5");
    private JButton JButton6=new JButton("Flow6");
    public FrameGridLayout()
    {
        super("FrameGridLayout");
        setSize(300,120);
        g1=new GridLayout(2,3,10,10);
        jpanel1=new JPanel();
        jpanel1.setLayout(g1);
        jpanel1.add(JButton1);
        jpanel1.add(JButton2);
        jpanel1.add(JButton3);
        jpanel1.add(JButton4);
        jpanel1.add(JButton5);
        jpanel1.add(JButton6);
        this.setContentPane(jpanel1);
        }
    public static void main(String args[])
    {
        FrameGridLayout fg1=new FrameGridLayout();
        fg1.show();
        fg1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        }
    }

 

(4).GridBagLayout

package MyJava.Base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FrameGridBagLayout extends JFrame
{
    private JPanel jpanel1=new JPanel();
    private GridLayout g1;
    private JButton JButton1=new JButton("First");
    private JButton JButton2=new JButton("Second");
    private JButton JButton3=new JButton("Third");
    private JButton JButton4=new JButton("Fourth");
    private JButton JButton5=new JButton("Fifth");
    private JButton JButton6=new JButton("Sixth");
    private JButton JButton7=new JButton("Seventh");
 
    public FrameGridBagLayout()
    {
        super("FrameGridBagLayout");
        setSize(300,150);
        GridBagLayout gb1=new GridBagLayout();
        jpanel1.setLayout(gb1);
        GridBagConstraints gbc=new GridBagConstraints();
        gbc.fill=GridBagConstraints.BOTH;
        gbc.gridwidth=1;
        gbc.gridheight=1;
        gbc.gridx=0;
        gbc.gridy=0;
        jpanel1.add(JButton1,gbc);
        gbc.gridx=1;
        jpanel1.add(JButton2,gbc);
        gbc.gridx=2;
        jpanel1.add(JButton3,gbc);
        gbc.gridx=0;
        gbc.gridy=1;
        gbc.gridwidth=3;
        jpanel1.add(JButton4,gbc);
        gbc.gridy=2;
        gbc.gridwidth=1;
        jpanel1.add(JButton5,gbc);
        gbc.gridx=1;
        gbc.gridwidth=2;
        gbc.gridheight=2;
        jpanel1.add(JButton6,gbc);
        gbc.gridx=0;
        gbc.gridy=3;
        gbc.gridwidth=1;
        gbc.gridheight=1;
        jpanel1.add(JButton7,gbc);
        this.setContentPane(jpanel1);
        }
    public static void main(String args[]){
        FrameGridBagLayout fg1=new FrameGridBagLayout();
        fg1.show();
        fg1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        }
    }

 

(5).CardLayout

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FrameCardLayout extends JFrame{
    private JPanel jpanel1;
    private CardLayout c1;
    private JButton jButton1=new JButton("Layer1");
    private JButton jButton2=new JButton("Layer2");
    private JButton jButton3=new JButton("Layer3");
    public FrameCardLayout(){
        super("frameCardlayout");
        setSize(300,120);
        jpanel1=new JPanel();
        c1=new CardLayout();
        jpanel1.setLayout(c1);
        jpanel1.add(jButton1,"First Layer");
        jpanel1.add(jButton2,"Second Layer");
        jpanel1.add(jButton3,"Third Layer");
        c1.show(jpanel1,"Second Layer");
        this.setContentPane(jpanel1);
       
        }
    public static void main(String arg[]){
        FrameCardLayout fg1=new FrameCardLayout();
        fg1.show();
         fg1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        }
    }

 

(6).BoxLayout

 

这个程序以后补上

 

6.如果觉得版面管理器学不了可以不使用,直接指定组件的绝对位置和大小

package MyJava.Base;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


class NonLayoutPane extends JPanel

 public NonLayoutPane()
 { 
  
  this.setLayout(null);
  
  JButton b1=new JButton("one");
  b1.setBounds(15,10,80,30);
  add(b1);
  
  JButton b2=new JButton("two");
  b1.setBounds(80,50,90,40);
  add(b2);
  
  
  
 
 
 
 }

 


}

class NonLayoutFrame extends JFrame

 public NonLayoutFrame()
 { 
  this.setTitle("NonLayoutTest");
  setSize(200,130);
  this.getContentPane().add(new NonLayoutPane());
  this.addWindowListener(new WindowAdapter()
        { 
         public void windowClosing(WindowEvent evt)
          { 
          
           System.exit(0);
          }
        
        }   
        );
 
 
 }

 

}
public class NonLayoutTest

 public static void main(String[] args)
 { 
 
  NonLayoutFrame frame=new NonLayoutFrame();
  frame.setVisible(true);
 
 
 }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值