[JExtensionPanel]可伸缩的面板

先看代码:
package lfl.swing.comp;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
* 可以伸缩的面板
*
* @author 李富粮
* @since 20120806
* @version 0.0.1
* */
public class JExtensionPanel extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
private boolean expand;// 是否展开
private String title;// 标题
private JLabel label;// 标题面板
private Component panel;// 主面板
private ImageIcon up_icon, down_icon;// 图标

// ----各种构造函数---------------------------------------
/**
* 可以伸缩的面板,默认展开
*
* @author 李富粮
* @since 20120806
* @version 0.0.1
*
* */
public JExtensionPanel() {
this("", null, true);
}

/**
* 可以伸缩的面板,默认展开
*
* @author 李富粮
* @since 20120806
* @version 0.0.1
* @param c
* 内容面板
* */
public JExtensionPanel(Component c) {
this("", c, true);
}

/**
* 可以伸缩的面板,默认展开
*
* @author 李富粮
* @since 20120806
* @version 0.0.1
* @param title
* 标题
* @param c
* 内容面板
* */
public JExtensionPanel(String title, Component c) {
this(title, c, true);
}

/**
* 可以伸缩的面板
*
* @author 李富粮
* @since 20120806
* @version 0.0.1
* @param title
* 标题
* @param c
* 内容面板
* @param expand
* 默认展开与否
* */
public JExtensionPanel(String title, Component c, boolean expand) {
this.title = title;
this.expand = expand;
this.panel = c;
init();
}

// ----各种方法---------------------------------------
/**
* 返回面板伸缩的状态
* */
public boolean isExpand() {
return expand;
}

/**
* 设置面板的伸缩
*
* @param true 展开
* @param false 收缩
* */
public void setExpand(boolean expand) {
this.expand = expand;
if (this.expand) {
this.label.setIcon(down_icon);
this.label.setBorder(BorderFactory.createLoweredBevelBorder());
if (null != this.panel) {
panel.setVisible(true);
}
} else {
this.label.setIcon(up_icon);
this.label.setBorder(BorderFactory.createRaisedBevelBorder());
if (null != this.panel) {
panel.setVisible(false);
}
}
this.updateUI();
this.updateUI();
}

/**
* 返回面板的内容面板
* */
public Component getPanel() {
return panel;
}

/**
* 设置内容面板
*
* @param panel
* 内容面板
* */
public void setPanel(Component panel) {
if (null != panel) {
this.remove(this.panel);
this.panel = panel;
this.add(this.panel, BorderLayout.CENTER);
if (this.expand) {
panel.setVisible(true);
} else {
panel.setVisible(false);
}
} else {
this.remove(this.panel);
this.panel = null;
}
this.updateUI();
}

/**
* 返回面板标题
* */
public String getTitle() {
return title;
}

/**
* 设置面板标题
*
* @param panel
* 面板标题
* */
public void setTitle(String title) {
this.title = title;
this.label.setText(title);
this.updateUI();
}

/**
* 创建标题指示图标
* */
private void createImages() {
int w = this.getPreferredSize().width, h = this.getPreferredSize().height;
BufferedImage open = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB), close = new BufferedImage(
w, h, BufferedImage.TYPE_INT_RGB);
// 展开的图标
Graphics2D g2 = open.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(this.getBackground());
g2.fillRect(0, 0, w, h);
int[] x = { 0, w / 2, w };
int[] y = { h / 4, 3 * h / 4, h / 4 };
Polygon p = new Polygon(x, y, 3);
g2.setPaint(Color.BLACK);
g2.fill(p);
g2.setPaint(Color.BLACK);
g2.draw(p);
g2.dispose();
down_icon = new ImageIcon(open);
// 收缩的的图标
g2 = close.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(getBackground());
g2.fillRect(0, 0, w, h);
x = new int[] { w / 4, 3 * w / 4, w / 4 };
y = new int[] { 0, h / 2, h };
p = new Polygon(x, y, 3);
g2.setPaint(Color.GRAY);
g2.fill(p);
g2.setPaint(Color.GRAY);
g2.draw(p);
g2.dispose();
up_icon = new ImageIcon(close);
}

/**
* 初始化工作
* */
private void init() {
this.createImages();// 获得图标
this.setLayout(new BorderLayout());
if (this.expand) {
this.label = new JLabel(this.title, this.down_icon,
SwingConstants.LEADING);
this.label.setBorder(BorderFactory.createLoweredBevelBorder());
if (null != this.panel) {
this.add(this.panel, BorderLayout.CENTER);
}
} else {
this.label = new JLabel(this.title, this.up_icon,
SwingConstants.LEADING);
this.label.setBorder(BorderFactory.createRaisedBevelBorder());
if (null != this.panel) {
this.add(this.panel, BorderLayout.CENTER);
this.panel.setVisible(false);
}
}
this.add(this.label, BorderLayout.NORTH);

this.label.addMouseListener(this);
}

// ----接口方法的实现---------------------------------------

@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == label) {
expand = !expand;
if (this.expand) {
this.label.setIcon(down_icon);
this.label.setBorder(BorderFactory.createLoweredBevelBorder());
if (null != this.panel) {
panel.setVisible(true);
}
} else {
this.label.setIcon(up_icon);
this.label.setBorder(BorderFactory.createRaisedBevelBorder());
if (null != this.panel) {
panel.setVisible(false);
}
}
this.updateUI();
}
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
}

@Override
public void mouseReleased(MouseEvent e) {
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = f.getContentPane();
c.setLayout(new BorderLayout());

c
.add(new JExtensionPanel("test1", new JButton("test1"),false),
BorderLayout.NORTH);
c
.add(new JExtensionPanel("test2", new JButton("test2"),false),
BorderLayout.CENTER);
f.setSize(360, 500);
f.setLocation(200, 100);
f.setVisible(true);
}
}


看看效果吧:
[img]http://dl.iteye.com/upload/attachment/0071/8714/0fe5989b-7a76-356c-9c2e-6a7835257368.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0071/8716/9c565977-5814-377e-81f1-23ada4bc118f.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0071/8720/a11f7386-db43-30ca-a088-cfdf6110fe2d.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0071/8722/6dc6231d-a248-37fc-8b8f-1026a668d9e3.gif[/img]

自己写的一个软件:

[img]http://dl.iteye.com/upload/attachment/0071/8736/af5d947d-5e46-35f1-b578-28db5ba46fec.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值