| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1106 人关注过本帖
标题:[求助]麻烦看看哪里错了
取消只看楼主 加入收藏
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
 问题点数:0 回复次数:9 
[求助]麻烦看看哪里错了

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

搜索更多相关主题的帖子: 麻烦 
2007-03-28 18:43
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 
我顶啊顶,快来帮我看看吧~~~~

天下归心
2007-03-28 18:49
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 
oh,i can't beleive that!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

天下归心
2007-03-28 20:10
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 

我把异常贴出来,那位帮我看看是怎么回事??
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,我不相信只有我才碰到异常~~~


天下归心
2007-03-28 20:13
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 
顶啊顶,谁来帮帮忙啊,难道只有我一个人遇到这种情况吗??

天下归心
2007-03-28 21:11
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 
好,结贴。

天下归心
2007-03-28 22:40
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 

的确是版本问题,我装了6.0后就可以了,但是不知道是为什么,可能是5。0自身的bug吧~~


天下归心
2007-03-29 10:51
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 
唉,用5。0这么久都有感情了,不舍得把它给卸了啊,伤心~~~

天下归心
2007-03-29 11:09
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 

呵呵,对极了~~


天下归心
2007-03-29 11:28
ILoveMK
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:487
专家分:0
注 册:2007-2-12
收藏
得分:0 
!!!!!!!!!!!!!!!!!!!!!!!!!!!

天下归心
2007-03-29 11:49
快速回复:[求助]麻烦看看哪里错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024804 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved