package test;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.awt.Component;
import javax.swing.Icon;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
/**
*自定义带关闭按钮的TabbedPane
* @author XiaoQuan
*/
public class ClosableTabbedPane extends JTabbedPane implements MouseListener {
private static final long serialVersionUID = -5154435928212725041L;
public ClosableTabbedPane() {
super();
addMouseListener(this);
}
@Override
public void addTab(String title, Component component) {
super.addTab(title, null, component);
}
public void addTab(String title, Component component, Icon extraIcon) {
super.addTab(title, new CloseTabIcon(), component);
}
@Override
public void mouseClicked(MouseEvent e) {
int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());
if (tabNumber < 0) {
return;
}
if (((CloseTabIcon) getIconAt(tabNumber)) != null) {
Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();
if (rect.contains(e.getX(), e.getY())) {
this.removeTabAt(tabNumber);
}
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public static void main(String args[]){
JFrame f =new JFrame();
ClosableTabbedPane closableTabbedPane =new ClosableTabbedPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
closableTabbedPane.addTab("不带关闭按钮", p1);
closableTabbedPane.addTab("带关闭按钮", p2, null);
f.getContentPane().add(closableTabbedPane);
f.setSize(500,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class CloseTabIcon implements Icon {
private int x_pos;
private int y_pos;
private int width;
private int height;
public CloseTabIcon() {
width = 16;
height = 16;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
this.x_pos = x;
this.y_pos = y;
Color col = g.getColor();
g.setColor(Color.RED.darker());
int y_p = y + 2;
g.drawLine(x + 1, y_p, x + 12, y_p);
g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);
g.drawLine(x, y_p + 1, x, y_p + 12);
g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);
g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);
g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);
g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);
g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);
g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);
g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);
g.setColor(col);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
public Rectangle getBounds() {
return new Rectangle(x_pos, y_pos, width, height);
}
}
分享到:
相关推荐
通过java swing 自定义JTabbedPane组件外观 演示自定义外观组件的方法
在主界面上单击菜单,可以打开子窗体.java swing自带的JTabbedPane没有显示关闭按钮的功能,这里在com.mkmis.controls包下自定义了一个TabbedPane控件,可以实现带关闭按钮的页签面板.应用结构如下图所示: 2 ...
java swing jTabbedPane 自定义的U,包括(宽、高、点击放大、文本字体、背景颜色、选项卡间距等) 简单易懂带注释可直接运行
其中包含五个项目,是博客《解读 LWUIT 之五:LWUIT 控件(下)》中作者写的 Hello TabbedPane、Hello TextArea、Hello TextField、Hello Calendar、Hello Tickering 源代码。博客链接地址:...
java界面开发技术swing的TabbedPane组件的使用实例,在myEclips下可直接编译运行。
Swing GUI的Java ME(J2ME)实现,具有Layouts,Borders,Renderer和许多组件,包括嵌入式TextField,Button,Window,TabbedPane等。 可以完全自定义任何组件的所有视觉和行为方面。
1、通过封装了AbstractTableModel完成JTable的使用。 2、使用TabbedPane完成多页数据的显示
final JTabbedPane tabbedPane =new JTabbedPane(); //定义总和面板tabbedPane 包含提货单和存货单 final JPanel panel_1=new JPanel(); //定义放存货单面板 final JPanel panel_2=new JPanel(); //定义...
为 PHP4/5 编写的 Web 控制库。 完全面向对象的设计。 这包括 TabbedPane、Panels 和其他。 EventController 通过 JavaScript 处理异步数据访问。
该系统基于放置在 tabbedPane 中的修改后的 JPanel(添加了一个十字和一个右键单击下拉菜单)。 对于每个列表,由于表单管理,可以插入、读取、修改或删除元素 (CRUD)。 搜索表单的实现也已经完成。 通过使用列表...