java [22] JFrame 三种布局和Jpanel

图形用户界面。

package graph;
/*
 * 
 * 边界布局案例 
 * 
 * 
 */
import java.awt.*;
import javax.swing.*;

public class demo2 extends JFrame{
	JButton button1,button2,button3,button4,button5 = null;
	
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		demo2 demo = new demo2();
	}
	public demo2() {
		//创建组件
		button1 = new JButton("东部");
		button2 = new JButton("南部");
		button3 = new JButton("西部");
		button4 = new JButton("北部");
		button5 = new JButton("中部");
		//添加组件
		this.add(button5,BorderLayout.CENTER);
		this.add(button1,BorderLayout.EAST);
		this.add(button2,BorderLayout.SOUTH);
		this.add(button3,BorderLayout.WEST);
		this.add(button4,BorderLayout.NORTH);
		
		//设置窗体
		this.setTitle("边界布局案例");
		this.setSize(300,300);
		this.setLocation(500,300);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
		
	}

}

 

 

package graph;

/*流式布局演示*/
import java.awt.*;
import javax.swing.*;

public class demo3 extends JFrame{
	JButton button1,button2,button3,button4,button5,button6 = null;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		demo3 demo =new demo3();

	}
	
	
	public demo3() {
		//创建组件
		button1 = new JButton("1");
		button2 = new JButton("2");
		button3 = new JButton("3");
		button4 = new JButton("4");
		button5 = new JButton("5");
		button6 = new JButton("6");
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		this.add(button1);
		this.add(button2);
		this.add(button3);
		this.add(button4);
		this.add(button5);
		this.add(button6);
		//设置窗体
		this.setTitle("边界布局案例");
		this.setSize(300,300);
		this.setLocation(500,300);
		//禁止用户改变窗体大小 
		this.setResizable(false);
		
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
		
		
		
	}
	
	

}

 

扫描二维码关注公众号,回复: 2529111 查看本文章
package graph;

/*网格布局*/

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


public class demo4 extends JFrame{
	
	
	
	int siza = 9;
	JButton[] button =new JButton[siza];
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		demo4 demo = new demo4();
	
	
	
	}
	
	
	
	public demo4(){
		for (int i=0;i<siza;i++) {
			try {
				
				//非简单的数据类型,new 、。
				button[i] = new JButton(String.valueOf(i));
			} catch (Exception e) {
				// TODO: handle exception
				e.getStackTrace();
			}
			
		}
		//设置网格布局
		this.setLayout(new GridLayout(3, 3,10,10));
		for (int i=0;i<siza;i++) {
			try {
				this.add(button[i]);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		//设置窗体
		this.setTitle("网格布局案例");
		this.setSize(300,300);
		this.setLocation(500,300);
		//禁止用户改变窗体大小 
		this.setResizable(false);
		
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
	}
}

 

package graph;
/*
 * 
 * 
 * jpanel
 * */

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


public class demo5 extends JFrame{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		demo5 demo = new demo5();
		
		
		
		
		

	}
	public demo5() {
		//创建jpanel 容器类 ,可以放置按钮  
		JPanel jp1 = new JPanel();
		JPanel jp2 = new JPanel();
		//创建button
		JButton button1 = new JButton("banana");
		JButton button2 = new JButton("apple");
		JButton button3 = new JButton("orange");
		JButton button4 = new JButton("juice");
		JButton button5 = new JButton("bread");
		JButton button6 = new JButton("rice");
		//向容器添加构件
		jp1.add(button1);
		jp1.add(button2);
		jp2.add(button4);
		jp2.add(button5);
		jp2.add(button6);
		this.add(jp1,BorderLayout.NORTH);
		this.add(jp2,BorderLayout.SOUTH);
		this .add(button3, BorderLayout.CENTER);
		
		this.setTitle("JPanel");
		this.setSize(300, 300);
		this.setLocation(300, 300);
		//关闭时自动关闭java虚拟机
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_38125626/article/details/81282687