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

📄 cardlayoutdemo.java

📁 GUI代码,用与实现相关GUI的功能,如有需要即可自由下载!
💻 JAVA
字号:
/*
 * $Header$
 * $Date$ 
 * $Revision$
 * Copyright (C) 2003-2004 CIC, Tsinghua Univ. 
 */
package jcase.gui;

import java.awt.Button;
import java.awt.*;
import java.awt.event.*;



/**
 * CardLayoutDemo 
 */
public class CardLayoutDemo extends BaseDemo implements ActionListener,MouseListener{
	
	Panel cards, buttons;
	CardLayout cardLO;
	Button card1, card2,card3;

	/**
	 * @see jcase.gui.BaseDemo#init()
	 */
	protected void init() {
		card1 = new Button("The Matrix I");
		card2 = new Button("The Matrix II");
		card3 = new Button("The Matrix III");
		add(card1);
		add(card2);
		add(card3);
		
		cardLO = new CardLayout();
		cards = new Panel();
		cards.setLayout(cardLO); // set panel layout to card layout 

		Font font = new Font("Dialog",Font.PLAIN,24);		
		// add components to cards 
		cards.add(createCard("The Matrix I -- First Chapter",Color.BLUE,font), 
				"The Matrix I");
		cards.add(createCard("The Matrix II -- RELOADED",Color.GREEN,font), 
				"The Matrix II"); 
		cards.add(createCard("The Matrix III -- REVOLUTION",Color.RED,font), 
				"The Matrix III");
		// add cards to main panel 
		add(cards); 

		// register to receive action events 
		card1.addActionListener(this);
		card2.addActionListener(this); 
		card3.addActionListener(this);
		// register mouse events 
		addMouseListener(this);

	}
	
	private Label createCard(String text,Color bgColor,Font font){
		Label label = new Label(text);
		label.setBackground(bgColor);
		label.setFont(font);
		return label;
	}
	
	/**
	 * @see jcase.gui.BaseDemo#getTitle()
	 */
	public String getTitle() {		
		return "演示CardLayoutDemo";
	}
	/**
	 * 根据按下的按钮显示对应的card
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof Button) {
			String label = ((Button)e.getSource()).getLabel();
			cardLO.show(cards, label);
		} 
	}
	
	/**
	 * 鼠标按下后切换到下一个card
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	public void mousePressed(MouseEvent e) {
		cardLO.next(cards);		
	}
	/**
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	public void mouseClicked(MouseEvent e) {
		// TODO something else
		
	}
	/**
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	public void mouseReleased(MouseEvent e) {
		// TODO something else
		
	}
	/**
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	public void mouseEntered(MouseEvent e) {
		
	}
	/**
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	public void mouseExited(MouseEvent e) {
		// TODO something else
		
	}
	
	/**
	 * @see java.awt.Component#getPreferredSize()
	 */
	public Dimension getPreferredSize() {
		// TODO something else
		return new Dimension(360,100);
	}
}

⌨️ 快捷键说明

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