⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 layoutdemo.java

📁 Java面向对象编程(随书配套源代码) 阐述了面向对象编程的思想
💻 JAVA
字号:
package chapter14;
import java.awt.*;
import javax.swing.*;
public class LayoutDemo extends JFrame
{
	JPanel pnlMain;
	public LayoutDemo()
	{
		pnlMain=new JPanel();
		getContentPane().add(pnlMain);
		//设置面板为空布局
		pnlMain.setLayout(new GridLayout(3,2));
		FlowPanel fp=new FlowPanel();
		BorderPanel bp=new BorderPanel();
		GridPanel gp=new GridPanel();
		NullPanel np=new NullPanel();
		BagPanel bgp=new BagPanel();
		CardPanel cp=new CardPanel();
		pnlMain.add(fp);
		pnlMain.add(bp);
		pnlMain.add(gp);
		pnlMain.add(np);
		pnlMain.add(bgp);
		pnlMain.add(cp);
		setTitle("布局演示");
 		setSize(600, 400);		
		setVisible(true);
	}
	
	public static void main(String args[])
	{
		new LayoutDemo();
	}

}
//流布局面板
class FlowPanel extends JPanel
{
	JLabel lblUser;
	JTextField txtUser;
	JButton btnCheck,btnExit;
	public FlowPanel()
	{
		setLayout(new FlowLayout());
		setBackground(Color.RED);
		lblUser=new JLabel("用户名:");
		txtUser=new JTextField(16);
		btnCheck=new JButton("检测用户名");
		btnExit=new JButton("退出");
		add(lblUser);
		add(txtUser);
		add(btnCheck);
		add(btnExit);
	}
}
//边界布局面板
class BorderPanel extends JPanel
{
	JButton b1,b2,b3,b4;
	public BorderPanel()
	{
		setLayout(new BorderLayout());
		setBackground(Color.YELLOW);
		b1=new JButton("Button 1");
		b2=new JButton("Button 2");
		b3=new JButton("Button 3");
		b4=new JButton("Button 4");
		add(b1,"North");
		add(b2,BorderLayout.SOUTH);
		add("East",b3);
		add("West",b4);
	}
}

//自定义布局面板
class NullPanel extends JPanel
{
	JButton b1,b2,b3,b4;
	public NullPanel()
	{
		setLayout(null);
		this.setBackground(Color.CYAN);
		b1=new JButton("Button 1");
		b1.setBounds(10,20,100,30);
		b2=new JButton("Button 2");
		b2.setBounds(120,20,100,30);
		b3=new JButton("Button 3");
		b3.setBounds(10,70,100,30);
		b4=new JButton("Button 4");
		b4.setBounds(120,70,100,30);
		add(b1);
		add(b2);
		add(b3);
		add(b4);
	}	
}
//网格布局面板
class GridPanel extends JPanel
{
	JButton b1,b2,b3,b4;
	public GridPanel()
	{
		setLayout(new GridLayout(2,2));
		setBackground(Color.BLUE);
		b1=new JButton("Button 1");
		b2=new JButton("Button 2");
		b3=new JButton("Button 3");
		b4=new JButton("Button 4");
		add(b1);
		add(b2);
		add(b3);
		add(b4);
	}
}

//网格袋布局面板
class BagPanel extends JPanel
{
	Canvas c;
    Choice cb;
	TextArea ta;
	JTextField tf;
	JButton b1, b2, b3;
	GridBagConstraints gbc;
	GridBagLayout gbl;
 	public BagPanel()
 	{
 		gbl=new GridBagLayout();
		setLayout(gbl);
		gbc= new GridBagConstraints();
 		ta = new TextArea("这是文本域", 5, 10);
		cb = new Choice();		
		cb.addItem("刘津");
		cb.addItem("刘志成");	
		cb.addItem("王咏梅");
		tf = new JTextField("这是文本框");
		b1 = new JButton("按钮 1");
		b2 = new JButton("按钮 2");
		b3 = new JButton("按钮 3");
		c = new Canvas();  	
		c.setBackground(Color.blue);
		c.setSize(10, 5);
 		// 布局textarea
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.fill = GridBagConstraints.BOTH;
		addComponent(ta, gbl, gbc, 0, 0, 1, 3); 
		// 布局按钮b1
		gbc.fill = GridBagConstraints.HORIZONTAL;
		addComponent(b1, gbl, gbc, 0, 1, 2, 1);
		// 布局choice
		addComponent(cb, gbl, gbc, 2, 1, 2, 1);
		// 布局按钮b2
		gbc.weightx = 1000;
		gbc.weighty = 1;
		gbc.fill = GridBagConstraints.BOTH;
		addComponent(b2, gbl, gbc, 1, 1, 1, 1);
		// 布局按钮b3
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.fill = GridBagConstraints.BOTH;
		addComponent(b3, gbl, gbc, 1, 2, 1, 1);
		// 布局文本框tf
		addComponent(tf, gbl, gbc, 3, 0, 2, 1);
		// 布局画布c
		addComponent(c, gbl, gbc, 3, 2, 1, 1);
	}
	//添加组件方法
 	private void addComponent(Component c, GridBagLayout g,
		GridBagConstraints gc, int row, int column, int width, int height)
	{
		gc.gridx = column;
 		gc.gridy = row;
		gc.gridwidth = width;
		gc.gridheight = height;
		g.setConstraints(c, gc);
		add(c);	
	}
}
//卡片式布局面板
class CardPanel extends JPanel
{
	JButton b1,b2,b3,b4;
	public CardPanel()
	{
		setLayout(new CardLayout());
		setBackground(Color.GREEN);
		b1=new JButton("Button 1");
		b2=new JButton("Button 2");
		b3=new JButton("Button 3");
		b4=new JButton("Button 4");
		add(b1,"North");
		add(b2,"South");
		add("East",b3);
		add("West",b4);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -