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

📄 cardlayoutdemo.java

📁 精通Java核心技术源代码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
public class CardLayoutDemo extends JFrame 
{
 		private JPanel southPanel,centerPanel,boxPanel,buttonPanel;
     	private CardLayout cardLayout;
     	private JButton checkBox,radioButton;
     	private JCheckBox boldBox,italicBox;
     	private JRadioButton plainButton,boldButton,italicButton,boldItalicButton;
     
     	public CardLayoutDemo()
     	{
     	 	super( "CardLayoutDemo" );
		
	  		// 获取content pane并设置布局管理器
	  		Container container = getContentPane();
	  		container.setLayout( new BorderLayout() );
	  
	  		// 创建各个Panel
     	  	southPanel = new JPanel();
   	  
     	  	cardLayout = new CardLayout();
   	  	centerPanel = new JPanel();
   	  	centerPanel.setLayout( cardLayout );
   	  
   	  	boxPanel = new JPanel();
   	  	buttonPanel = new JPanel();
   	  
   	  	// 创建southPanel中的按钮
   	  	checkBox = new JButton( "CheckBox" );
   	  	radioButton = new JButton( "RadioButton" );
   	  
   	  	// 为按钮注册事件处理器
   	  	ButtonHandler handler = new ButtonHandler();
   	  	checkBox.addActionListener( handler );
   	  	radioButton.addActionListener( handler );
   	  
   	  	// 将按钮加入southPanel中
   	  	southPanel.add( checkBox );
   	  	southPanel.add( radioButton );
   	  
   	  	// 创建选择框并将它们加入boxPanel中
   	  	boldBox = new JCheckBox( "Bold" );
   	  	italicBox = new JCheckBox( "Italic" );
   	  	boxPanel.add( boldBox );
   	  	boxPanel.add( italicBox );
   	  	
   	  	// 将boxPanel 加入 centerPanel中,名字记为"box"
   	  	centerPanel.add( boxPanel,"box" );
   	  
   	  	// 创建radioButtons并将它们加入 buttonPanel中
   	  	plainButton = new JRadioButton( "Plain" );
   	  	boldButton = new JRadioButton( "Bold" );
   	  	italicButton = new JRadioButton( "Italic" );
   	  	boldItalicButton = new JRadioButton( "Bold/Italic" );
   	  
      		// 创建按钮组
      		ButtonGroup radioButtonGroup = new ButtonGroup();
      		radioButtonGroup.add( plainButton );
      		radioButtonGroup.add( boldButton );
      		radioButtonGroup.add( italicButton );
      		radioButtonGroup.add( boldItalicButton );   	  
   	  
   	  	buttonPanel.add( plainButton );
   	  	buttonPanel.add( boldButton );
   	  	buttonPanel.add( italicButton );
   	  	buttonPanel.add( boldItalicButton );
   	  
   	  	// 将 buttonPanel 加入 centerPanel 中,名字记为"radio"
   	  	centerPanel.add( buttonPanel,"radio" );
   	  
   	  	// 将 southPanel 和 centerPanel 加入 container中
   	  	container.add( southPanel,BorderLayout.SOUTH );
   	  	container.add( centerPanel,BorderLayout.CENTER );
   	  
   	  	setSize( 270,100 );
	  		setVisible( true );  
   	}
   	
    	// 用于事件处理的内部类
		private class ButtonHandler implements ActionListener
		{
   	  	public void actionPerformed( ActionEvent event )
       		{
          		if ( event.getSource() == checkBox )
            			cardLayout.show( centerPanel,"box" );
   	     	else cardLayout.show( centerPanel,"radio" );
        	}
     	}
      	  
       	public static void main(String args[]) 
       	{
          	CardLayoutDemo cardLayoutDemo = new CardLayoutDemo();
          	cardLayoutDemo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   	}
}

⌨️ 快捷键说明

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