码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-05-09 11:08:38      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:extend   extends   owb   i++   tla   etc   button   java   textfield   

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

}


效果:

技术分享

 

 

 

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

标签:extend   extends   owb   i++   tla   etc   button   java   textfield   

原文地址:http://www.cnblogs.com/qiangji319208/p/6829071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!