Java 学习之 Swing 编程

主要记录java Swing编程,JFrame控制的使用以及内容面板(JPanel)的常用布局的使用。


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;


public class MyJFrame extends JFrame {

	/**
	 * 
	 */
	private JPanel contentPane;
	private static final long serialVersionUID = -4948295198859299320L;
	
	public static void main(String []args){
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");// 修改窗体的外观为Nimbus
		} catch (Throwable e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					MyJFrame frame = new MyJFrame();
					frame.setVisible(true);
					
//					frame.AbsoluteLayoutDemo();
//					frame.FlowLayoutDemo();
//					frame.BorderLayoutDemo();
					frame.GirdLayoutDemo();
					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		});
	}
	
	public MyJFrame(){
		setTitle("最简单的窗体");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,100,250,100);
	}
	
	// 绝对布局管理
	public void AbsoluteLayoutDemo(){
		setTitle("绝对布局");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置应用退出模式,退出时关闭
		
		setBounds(100, 100, 250, 100); //  设置窗体的大小和位置
		contentPane = new JPanel();  // 创建内容面板
		contentPane.setBorder(new EmptyBorder(5,5,5,5)); // 设置内容面板边框
		setContentPane(contentPane); // 使用内容面板
		contentPane.setLayout(null);// 设置布局管理器为null
		
		JButton btn1 = new JButton("按钮-1");
		btn1.setBounds(6, 9, 90, 30);
		contentPane.add(btn1);
		btn1.setBackground(Color.GREEN);
		
		
		JButton btn2 = new JButton("按钮-2");
		btn2.setBounds(96, 39, 90, 30);
		btn2.setBackground(Color.RED);
		contentPane.add(btn2);
	}
	// 流式布局管理,根据内容面板的变化,排列也发生变化
	public void FlowLayoutDemo(){
		setTitle("流式布局");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 150);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FlowLayout()); //设置为流式布局
		
		for (int i = 1; i < 11; i++) {
			JButton btn = new JButton("按钮"+i);
			contentPane.add(btn);			
		}		
	}
	
	// 边界布局管理,指定各自的方位
	public void BorderLayoutDemo(){
		setTitle("边界布局");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,100,450,300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout());// 设置内容面板的布局管理器为边界布局
		setContentPane(contentPane);// 应用内容面板
		
		JButton westBtn = new JButton("西部");
		contentPane.add(westBtn, BorderLayout.WEST);
		
		JButton eastBtn = new JButton("东部");
		contentPane.add(eastBtn, BorderLayout.EAST);
		
		JButton northBtn = new JButton("北部");
		contentPane.add(northBtn, BorderLayout.NORTH);
		
		JButton southBtn = new JButton("南部");
		contentPane.add(southBtn, BorderLayout.SOUTH);
		
		JButton centerBtn = new JButton("中部");
		contentPane.add(centerBtn, BorderLayout.CENTER);
	}
	
	// 网格布局管理器,按照从左到右,从上到下 的 “行*列”布局一次排列
	public void GirdLayoutDemo(){
		setTitle("网格布局");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,100,450,300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
//		contentPane.setLayout(new GridLayout());// 设置网格布局,默认是单行多列
		contentPane.setLayout(new GridLayout(3, 3, 10, 10));// 设置网格布局 3行3列
		
		for (int i = 0; i < 8; i++) {
			JButton btn = new JButton("btn"+(i+1));
			contentPane.add(btn);			
		}
	}
}


你可能感兴趣的:(java,JFrame,swing,layout)