GUI 编程【二】 (GridLayout,BorderLayout,将面板作为容器使用)

GUI 编程【二】

                    ——GridLayout , BorderLayout,将面板作为容器使用:

GridLayout:

package java19;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ShowGridLayout extends JFrame {

	
	    public ShowGridLayout()  
	    {  
	        Container container=getContentPane();  
	        //定义容器  
	        container.setLayout(new GridLayout(4,3,10,20));  
	        //等价于:  
	        //GridLayout layout=new GridLayout(FlowLayout.LEFT,10,20);  
	        //container.setLayout(layout);  
	        //布局方式  
	        for(int i=1;i<=10;i++)  
	        {  
	            container.add(new JButton("Component"+i));  
	        }  
	    }  
	    public static void main(String[] args) {  
	    	ShowGridLayout frame=new ShowGridLayout();  
	        frame.setTitle("show FlowLayout");  
	        frame.setSize(200,200);  
	        frame.setVisible(true);  
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
	    }  


}

效果图


BorderLayout:

package java19;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ShowBorderLayout extends JFrame {

	
	    public ShowBorderLayout()  
	    {  
	        Container container=getContentPane();  
	        //定义容器  ,BorderLayout(x,y),x,y分别为水平和垂直间距
	        container.setLayout(new BorderLayout(5,10));  
	        //等价于:  
	       ; //BorderLayout layout=new BorderLayout(5,10);  
	        //container.setLayout(layout);  
	        //布局方式  
	        container.add(new JButton("East"),BorderLayout.EAST);
	        container.add(new JButton("South"),BorderLayout.SOUTH);
	        container.add(new JButton("West"),BorderLayout.WEST);
	        container.add(new JButton("North"),BorderLayout.NORTH);
	        container.add(new JButton("Center"),BorderLayout.CENTER);
	    }  
	    public static void main(String[] args) {  
	    	ShowBorderLayout frame=new ShowBorderLayout();  
	        frame.setTitle("show FlowLayout");  
	        frame.setSize(200,200);  
	        frame.setVisible(true);  
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
	    }  

}

效果

将面板作为容器使用:

package java19;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TestPanels extends JFrame {
       public TestPanels()
       {
    	   //创建容器
    	   Container container=getContentPane();
    	   container.setLayout(new BorderLayout());
    	   //创建面板p1
    	   JPanel p1=new JPanel();
    	   p1.setLayout(new GridLayout(4,3));
    	   //对p1面板添加数据
    	   for(int i=1;i<9;i++)
    	   {
    		   p1.add(new JButton(" "+i));
    	   }
    	   p1.add(new JButton(" "+0));
    	   p1.add(new JButton("start"));
    	   p1.add(new JButton("stop"));
    	   //创建面板p2
    	   JPanel p2=new JPanel();
    	   p2.setLayout(new BorderLayout());
    	   p2.add(new JTextField("time to be display here"),BorderLayout.NORTH);
    	   p2.add(p1,BorderLayout.CENTER);
    	   //添加到容器
    	   container.add(p2,BorderLayout.EAST);
    	   container.add(new Button("Food to be place here"),BorderLayout.CENTER);
    	   
       }
	
	    
	    public static void main(String[] args) {  
	    	TestPanels frame=new TestPanels();  
	        frame.setTitle("the front view of a microwave oven");  
	        frame.setSize(200,200);  
	        frame.setVisible(true);  
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
	    }  

}


效果:

 

 

 

posted @ 2017-05-09 09:34  老鼠先生qiangji  阅读(351)  评论(0编辑  收藏  举报