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

📄 cardlayoutdemo.java

📁 学习Java的一些基本课件和源代码 对于初学者很有帮助的
💻 JAVA
字号:
//CardLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CardLayoutDemo extends JFrame implements ActionListener
{
	CardLayout card;
	JPanel p1,p2;
	JButton b1,b2,b3;
	CardLayoutDemo()
	{
		card=new CardLayout();      //创建一个卡片布局管理器
		b1=new JButton("流式布局");
		b2=new JButton("边界布局");
		b3=new JButton("网格布局");
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);				
		p1=new JPanel();
		p1.add(b1);p1.add(b2);p1.add(b3);
		p2=new JPanel();
		p2.setLayout(card);//面板p2的布局管理器设置为卡片布局
		p2.add("流式布局",new TestFlowLayout());//添加标题为"流式布局"的组件
		p2.add("边界布局",new TestBorderLayout());//添加标题为"边界布局"的组件
		p2.add("网格布局",new TestGridLayout());//添加标题为"网格布局"的组件
		this.getContentPane().add(p1,"South");
		this.getContentPane().add(p2,"Center");
		setSize(340,200);
		setTitle("卡片布局演示");
		setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==b1)
				card.show(p2,"流式布局");//显示p2中的"流式布局"页
			else if(e.getSource()==b2)
				card.show(p2,"边界布局");//显示p2中的"边界布局"页
			else if(e.getSource()==b3)
				card.show(p2,"网格布局");//显示p2中的"网格布局"页
				
		}
		public static void main(String args[])
		{
			CardLayoutDemo myDemo=new CardLayoutDemo();
		}				
	}
	class TestFlowLayout extends JPanel
	{
		JButton b1,b2,b3,b4,b5;
		TestFlowLayout()
		{
			setLayout(new FlowLayout());
			b1=new JButton("one");
			b2=new JButton("two");
			b3=new JButton("three");
			b4=new JButton("four");
			b5=new JButton("five");
			add(b1);add(b2);add(b3);add(b4);add(b5);
		}
	}
	class TestBorderLayout extends JPanel
	{
		JButton b1,b2,b3,b4,b5;
		TestBorderLayout()
		{
			setLayout(new BorderLayout());
			b1=new JButton("one");
			b2=new JButton("two");
			b3=new JButton("three");
			b4=new JButton("four");
			b5=new JButton("five");
			add(b1,"North");add(b2,"East");add(b3,"South");
			add(b4,"West");add(b5,"Center");			
		}		
	}
	class TestGridLayout extends JPanel
	{
		JButton b1,b2,b3,b4,b5;
		TestGridLayout()
		{
			setLayout(new GridLayout(3,2,10,10));
			b1=new JButton("one");
			b2=new JButton("two");
			b3=new JButton("three");
			b4=new JButton("four");
			b5=new JButton("five");
			add(b1);add(b2);add(b3);add(b4);add(b5);
		}
	}

⌨️ 快捷键说明

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