欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

swing常用的布局

程序员文章站 2024-01-31 23:34:58
...

常用的布局有

1.流布局

swing常用的布局

2.边界布局

swing常用的布局

3.网格布局

swing常用的布局

4.卡片布局

swing常用的布局

swing常用的布局

5.网格包布局

swing常用的布局

测试代码:

public class TestOne {
	private JButton[] button=new JButton[5];
	public static void main(String[] args) {
		TestOne t=new TestOne();
		JFrame frame=new JFrame();
		frame.setBounds(400, 10, 1000, 1000);
		t.testFlowLayOut(frame);
		t.testBorderLayOut(frame);
		t.testGridLayOut(frame);
		t.testCardLayOut(frame);
		t.testGridBagLayOut(frame);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
	}
	/**
	 * 流布局
	 * @param frame
	 */
	public void testFlowLayOut(JFrame frame){
		frame.setLayout(new FlowLayout());
		for(int i=0;i<5;i++){
			button[i]=new JButton("按钮"+i);
			frame.add(button[i]);
		}
	}	
	/**
	 * 边框布局
	 * @param frame
	 */
	public void testBorderLayOut(JFrame frame){
		frame.setLayout(new BorderLayout());
		for(int i=0;i<5;i++){
			button[i]=new JButton("按钮"+i);
		}
		frame.add(button[0],"North");
		frame.add(button[1],"West");
		frame.add(button[2],"East");
		frame.add(button[3],"South");
		frame.add(button[4],"Center");
	}
	/**
	 * 网格布局
	 * @param frame
	 */
	public void testGridLayOut(JFrame frame){
		frame.setLayout(new GridLayout(3, 2));
		for(int i=0;i<5;i++){
			button[i]=new JButton("按钮"+i);
			frame.add(button[i]);
		}
	}
	/**
	 * 卡片布局
	 * @param frame
	 */
	public void testCardLayOut(JFrame frame){
		frame.setLayout(new BorderLayout());
		JPanel pan1=new JPanel();
		JPanel pan2=new JPanel();
		JPanel pan3=new JPanel();
		JPanel pan4=new JPanel();
		JPanel pan5=new JPanel();
		CardLayout card=new CardLayout();
		pan2.setLayout(card);
		frame.add(pan1,"North");
		frame.add(pan2,"Center");
		for(int i=0;i<3;i++){
			button[i]=new JButton("按钮"+i);
			pan1.add(button[i]);
		}
		pan2.add(pan3, "1");
		JLabel lb1=new JLabel("Panel   1");
		pan3.add(lb1);
		pan2.add(pan4, "2");
		JLabel lb2=new JLabel("Panel   2");
		pan4.add(lb2);
		pan2.add(pan5, "3");
		JLabel lb3=new JLabel("Panel   3");
		pan5.add(lb3);
		button[0].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				card.show(pan2, "1");
				
			}
		});
		button[1].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				card.show(pan2, "2");
				
			}
		});
		button[2].addActionListener(new ActionListener() {
	
			@Override
			public void actionPerformed(ActionEvent e) {
				card.show(pan2, "3");
		
			}
		});
	}
	/**
	 * 网格包布局
	 */
	public void testGridBagLayOut(JFrame frame){
		    frame.setLayout(new GridBagLayout());
		    GridBagConstraints c = new GridBagConstraints();
		    for(int i=0;i<5;i++){
				button[i]=new JButton("按钮"+i);
				frame.add(button[i]);
			}
		 
		    c.fill = GridBagConstraints.HORIZONTAL;
		    c.gridx = 0;
		    c.gridy = 0;
		   // c.weightx = 0.2;//水平布局占的权重
		    frame.add(button[0], c);
		 
		    
		    c.fill = GridBagConstraints.HORIZONTAL;
		    c.weightx = 0.8;
		    c.gridx = 1;
		    c.gridy = 0;
		    frame.add(button[1], c);
		 
		   
		    c.fill = GridBagConstraints.HORIZONTAL;
		    c.weightx = 0.2;
		    c.gridx = 2;
		    c.gridy = 0;
		    frame.add(button[2], c);
		 
		  
		    c.fill = GridBagConstraints.HORIZONTAL;
		    c.ipady = 80; // make this component tall
		    c.weightx = 1;
		    c.gridwidth = 3;
		    c.gridx = 0;
		    c.gridy = 1;
		    frame.add(button[3], c);
		 
		  
		    c.fill = GridBagConstraints.HORIZONTAL;
		    c.weighty = 1.0;
		    c.weightx=1;
		    c.anchor = GridBagConstraints.PAGE_END; 
		    c.gridx = 1; 
		    c.gridwidth = 1; 
		    c.gridy = 2; 
		    frame.add(button[4], c);
	}
}