先定义基础类,为JPanel 绘制关闭按钮
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicButtonUI;
/**
* 选项面板上的关闭按钮
*
* @author black
*
*/
public class ClosePanelComponent extends JPanel {
private static final long serialVersionUID = 1L;
private String title;
/**
* 选项面板
*/
private JTabbedPane pane;
public ClosePanelComponent(final JTabbedPane pane) {
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.pane = pane;
setOpaque(false);
/**
* 显示面板名字的文本控件
*/
JLabel label = new JLabel() {
private static final long serialVersionUID = 1L;
public String getText() {
int i = pane.indexOfTabComponent(ClosePanelComponent.this);
if (i != -1) {
return pane.getTitleAt(i);
}
return null;
}
};
add(label);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
JButton button = new TabButton();
add(button);
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
public ClosePanelComponent(String title, JTabbedPane pane) {
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.pane = pane;
this.title = title;
setOpaque(false);
JLabel label = new JLabel(title);
add(label);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
JButton button = new TabButton();
add(button);
setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
}
/**
* 关闭按钮,用于关闭当前选项面板
*
* @author bruce
*
*/
private class TabButton extends JButton implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 初始化关闭按钮
*/
public TabButton() {
int size = 17;
setPreferredSize(new Dimension(size, size));
setToolTipText("关闭");
setUI(new BasicButtonUI());
setContentAreaFilled(false);
setFocusable(false);
setBorder(BorderFactory.createEtchedBorder());
setBorderPainted(false);
addMouseListener(buttonMouseListener);
setRolloverEnabled(true);
addActionListener(this);
}
/**
* 移除面板
*/
public void actionPerformed(ActionEvent e) {
int i = pane.indexOfTabComponent(ClosePanelComponent.this);
if (i != -1) {
pane.remove(i);
}
}
/**
* 画关闭按钮
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
if (getModel().isPressed()) {
g2.translate(1, 1);
}
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.BLACK);
if (getModel().isRollover()) {
g2.setColor(Color.MAGENTA);
}
int delta = 6;
g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight()
- delta - 1);
g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight()
- delta - 1);
g2.dispose();
}
}
private final static MouseListener buttonMouseListener = new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(true);
}
}
public void mouseExited(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(false);
}
}
};
}
=======================================================================================
下面是关于这个对象的使用,
private javax.swing.JTabbedPane jTabbedPane1; 可关闭Panel的容器对象
static LinkedHashMap tablePanelObject = new LinkedHashMap(); //TesterPanel为自定义的普通Panel,tablePanelObject 用来管理窗口的全局变量
这两个对象是主窗口的内部变量,自己创建即可。
======================================================================================
public void newTerserWindow(String title) {
TesterPanel tester = tablePanelObject.get(title);
if (tester == null) {
//内存中没有,先添加到内存linkedHashMap全局变量里
tester = new TesterPanel(title);
tester.setVisible(true);
tablePanelObject.put(title, tester);
//然后创建可关闭的页签
jTabbedPane1.add(tester);
ClosePanelComponent bt = new ClosePanelComponent(title, jTabbedPane1);
jTabbedPane1.setTabComponentAt(jTabbedPane1.getTabCount() - 1, bt);
jTabbedPane1.setSelectedComponent(tester);
} else {
boolean flag = false;
//如果内存中有,检查页签是否已经被打开
for (int index = 0; index < jTabbedPane1.getTabCount(); index++) {
if (jTabbedPane1.getComponentAt(index).equals(tester)) { //如果找到,选中即可
jTabbedPane1.setSelectedComponent(tester);
flag = true;
break;
}
}
if (!flag) {
//如果没有,说明被关闭了,则从内存拿对象,新建页签
jTabbedPane1.add(tester);
ClosePanelComponent bt = new ClosePanelComponent(sd.getServiceCode(), jTabbedPane1);
jTabbedPane1.setTabComponentAt(jTabbedPane1.getTabCount() - 1, bt);
jTabbedPane1.setSelectedComponent(tester);
}
}
}