java学习篇(二)---卡片布局详解(Swing和awt)

其实java的自带的Swing和awt布局也挺好看的...

package cn.hsp.cardlayout;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

/**
 * 用doc文档写 开发文档, 这里需要去简单演示一下学生管理系统的几个重要界面,截图作为开发的界面. 卡片布局简单案例: 
 * 测试卡片布局是怎么使用的
 * @author nsz 2014下午12:08:26
 */
public class TestCard extends JFrame implements MouseListener {

	private static final long serialVersionUID = 1L;

	// 定义需要的组件
	JSplitPane jsp = null;
	JPanel jPanel_left, jPanel_right, jPanel_right_1, jPanel_right_2,
			jPanel_right_3;
	JLabel jb1, jb2, jb3;
	CardLayout cl = new CardLayout();

	public static void main(String[] args) {
		new TestCard();

	}

	public TestCard() {
		// 创建组件
		jPanel_left = new JPanel(new GridLayout(5, 1));
		jPanel_left.setBorder(BorderFactory.createEtchedBorder());

		jb1 = new JLabel("学生选课系统", JLabel.CENTER);
		jb1.addMouseListener(this);
		jb2 = new JLabel("老师管理", JLabel.CENTER);
		jb2.addMouseListener(this);
		jb3 = new JLabel("学生管理", JLabel.CENTER);
		jb3.addMouseListener(this);
		jPanel_left.add(jb1);
		jPanel_left.add(jb2);
		jPanel_left.add(jb3);

		// 右边的panel
		jPanel_right = new JPanel(cl);

		jPanel_right_1 = new JPanel();
		jPanel_right_1.setBackground(Color.RED);
		jPanel_right_1.add(new JLabel(new ImageIcon("face09.png")));
		jPanel_right_2 = new JPanel();
		jPanel_right_2.setBackground(Color.BLUE);
		jPanel_right_3 = new JPanel();
		jPanel_right_3.setBackground(Color.GREEN);

		jPanel_right.add("1", jPanel_right_1);
		jPanel_right.add("2", jPanel_right_2);
		jPanel_right.add("3", jPanel_right_3);

		// 设置默认显示的卡片
		cl.show(jPanel_right, "1");

		jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel_left,
				jPanel_right);
		jsp.setDividerLocation(140);
		jsp.setDividerSize(0);

		this.add(jsp);

		this.setSize(800, 600);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 1) {
			// 判断用户点击了哪个JLable
			if (e.getSource() == jb1) {
				cl.show(jPanel_right, "1");
			} else if (e.getSource() == jb2) {
				cl.show(jPanel_right, "2");
			} else if (e.getSource() == jb3) {
				cl.show(jPanel_right, "3");
			}
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	// 鼠标点击
	@Override
	public void mouseEntered(MouseEvent e) {
		// if(e.getSource() == jb1){
		// jb1.setForeground(Color.RED);
		// jb1.setCursor(new Cursor(Cursor.HAND_CURSOR));
		// }
		// else if(e.getSource() == jb2){
		// jb1.setForeground(Color.RED);
		// jb1.setCursor(new Cursor(Cursor.HAND_CURSOR));
		// }
		// else if(e.getSource() == jb3){
		// jb1.setForeground(Color.RED);
		// jb1.setCursor(new Cursor(Cursor.HAND_CURSOR));
		// }
		// 跟上面的代码作用一样
		((JLabel) e.getSource()).setForeground(Color.RED);
		((JLabel) e.getSource()).setCursor(new Cursor(Cursor.HAND_CURSOR));
	}

	// 鼠标退出
	@Override
	public void mouseExited(MouseEvent e) {
		if (e.getSource() == jb1) {
			jb1.setForeground(Color.BLACK);
			jb1.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		} else if (e.getSource() == jb2) {
			jb2.setForeground(Color.BLACK);
			jb2.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		} else if (e.getSource() == jb3) {
			jb3.setForeground(Color.BLACK);
			jb3.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		}
	}
}

显示的效果

java学习篇(二)---卡片布局详解(Swing和awt)_第1张图片java学习篇(二)---卡片布局详解(Swing和awt)_第2张图片java学习篇(二)---卡片布局详解(Swing和awt)_第3张图片

你可能感兴趣的:(Java,SE)