import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JClosableTabbedPane extends JTabbedPane implements MouseListener {
public JClosableTabbedPane() {
super();
addMouseListener(this);
}
public void addTab(String title, Component component) {
this.addTab(title, component, null);
}
public void addTab(String title, Component component, Icon extraIcon) {
super.addTab(title, new CloseTabIcon(extraIcon), component);
}
public void mouseClicked(MouseEvent e) {
int tabNumber = this.getSelectedIndex();
if (tabNumber < 0)
return;
Rectangle rect = ( (CloseTabIcon) getIconAt(tabNumber)).getBounds();
if (rect.contains(e.getX(), e.getY())) {
this.remove(tabNumber);
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
class CloseTabIcon implements Icon {
private int x_pos;
private int y_pos;
private int width;
private int height;
private Icon fileIcon;
public CloseTabIcon(Icon fileIcon) {
this.fileIcon = fileIcon;
width = 16;
height = 16;
}
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.black);
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);
if (fileIcon != null) {
fileIcon.paintIcon(c, g, x + width, y_p);
}
}
public int getIconWidth() {
return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);
}
public int getIconHeight() {
return height;
}
public Rectangle getBounds() {
return new Rectangle(x_pos, y_pos, width, height);
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception e) {
e.printStackTrace();
}
JClosableTabbedPane pane = new JClosableTabbedPane();
ImageIcon icon = new ImageIcon("");
pane.addTab("tab1",new JButton("按钮1"),icon);
pane.addTab("tab2",new JButton("按钮2"),icon);
pane.addTab("tab3",new JButton("按钮3"),icon);
pane.addTab("tab4",new JButton("按钮4"),icon);
JFrame frame = new JFrame("Demo");
frame.getContentPane().add(pane,BorderLayout.CENTER);
frame.setSize(500,300);
frame.setLocation(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
上面的程序在关闭一个tab时发生了异常(你可以试试看先关闭tab4,再关闭tab3),看不出有什么问题,麻烦大家帮帮忙,谢谢.
PS:异常我就不贴出来了,你可以拷到机子上运行试试看,3q
----------------解决方案--------------------------------------------------------
我顶啊顶,快来帮我看看吧~~~~
----------------解决方案--------------------------------------------------------
我这里运行了啊,一点异常也没有
----------------解决方案--------------------------------------------------------
oh,i can't beleive that!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
----------------解决方案--------------------------------------------------------
我把异常贴出来,那位帮我看看是怎么回事??
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:
3
at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPane
UI.java:1313)
at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPane
UI.java:1248)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mousePressed(BasicTa
bbedPaneUI.java:3217)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:22
1)
at java.awt.Component.processMouseEvent(Component.java:5485)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:
2
at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPane
UI.java:1313)
at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPane
UI.java:1248)
at com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI.setRolloverTab(Wi
ndowsTabbedPaneUI.java:82)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneLayout.layoutConta
iner(BasicTabbedPaneUI.java:2149)
at java.awt.Container.layout(Container.java:1401)
at java.awt.Container.doLayout(Container.java:1390)
at java.awt.Container.validateTree(Container.java:1473)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validate(Container.java:1448)
at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.j
ava:379)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System
EventQueueUtilities.java:113)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:
2
at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPane
UI.java:1313)
at javax.swing.plaf.basic.BasicTabbedPaneUI.getTabBounds(BasicTabbedPane
UI.java:1248)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.focusLost(BasicTabbe
dPaneUI.java:3258)
at java.awt.Component.processFocusEvent(Component.java:5380)
at java.awt.Component.processEvent(Component.java:5244)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.ja
va:1810)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeybo
ardFocusManager.java:836)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFoc
usManager.java:526)
at java.awt.Component.dispatchEventImpl(Component.java:3841)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:93)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
PS:请执行以下操作,先关闭tab4,再关闭tab3,我不相信只有我才碰到异常~~~
----------------解决方案--------------------------------------------------------
顶啊顶,谁来帮帮忙啊,难道只有我一个人遇到这种情况吗??
----------------解决方案--------------------------------------------------------
好,结贴。
----------------解决方案--------------------------------------------------------
这是各个JVM的实现不同而出现的异常
我用的是JDK6.0,不知道你是用哪个版本的JDK?
----------------解决方案--------------------------------------------------------
你没有发现异常里面没有一个是你的程序抛出来的吗,说明你的程序没有问题
----------------解决方案--------------------------------------------------------
的确是版本问题,我装了6.0后就可以了,但是不知道是为什么,可能是5。0自身的bug吧~~
----------------解决方案--------------------------------------------------------