怎样使JTabbedPane的每一个选项可关闭呢?像netbean那样? netbean上的每个选项(还是说成每一个小窗口)都可以关闭和缩小,但我在JTabblePane打不到这样的方法?要怎样才能做到? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://forum.java.sun.com/thread.jspa?threadID=337070&start=0&tstart=0 package test;import javax.swing.JTabbedPane;import java.awt.Rectangle;import javax.swing.UIManager;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import java.awt.BorderLayout;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;public 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 = getUI().tabForCoordinate(this, e.getX(), e.getY()); if (tabNumber < 0) return; Rectangle rect = ( (CloseTabIcon) getIconAt(tabNumber)).getBounds(); if (rect.contains(e.getX(), e.getY())) { //the tab is being closed this.removeTabAt(tabNumber); } } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {}}/** * The class which generates the 'X' icon for the tabs. The constructor * accepts an icon which is extra to the 'X' icon, so you can have tabs * like in JBuilder. This value is null if no extra icon is required. */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("images/middle.jpg"); pane.addTab("tab1", new JButton("first Button"), icon); pane.addTab("tab2", new JButton("sec Button"), icon); pane.addTab("tab3", new JButton("third Button"), icon); pane.addTab("tab4", new JButton("fourth Button"), icon); JFrame frame = new JFrame("Demo"); frame.getContentPane().add(pane, BorderLayout.CENTER); frame.setSize(500, 300); frame.setLocation(300, 200); frame.show(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} 这段程序到底是锁定的那个对象? 如何在万年历中添加记事功能(类似备忘录) 关于文件操作的简单问题!!! JAVA基础知识求助 一个简单的数组问题 java的当前路径问题! CSDN的一个工作日有多长啊????? 帮忙看看代码,关于表格的. 再开一贴 求救:不同的数据库中不同的表,想把其中相关的字段提出来生成一个查询,绑到datagrid上! ejb 环境属性文件是什么 时间格式的转化遇到的问题,请指点
import java.awt.Rectangle;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;
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;public 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 = getUI().tabForCoordinate(this, e.getX(), e.getY());
if (tabNumber < 0)
return;
Rectangle rect = ( (CloseTabIcon) getIconAt(tabNumber)).getBounds();
if (rect.contains(e.getX(), e.getY())) { //the tab is being closed
this.removeTabAt(tabNumber);
}
} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {}
}/** * The class which generates the 'X' icon for the tabs. The constructor * accepts an icon which is extra to the 'X' icon, so you can have tabs * like in JBuilder. This value is null if no extra icon is required. */
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("images/middle.jpg");
pane.addTab("tab1", new JButton("first Button"), icon);
pane.addTab("tab2", new JButton("sec Button"), icon);
pane.addTab("tab3", new JButton("third Button"), icon);
pane.addTab("tab4", new JButton("fourth Button"), icon); JFrame frame = new JFrame("Demo");
frame.getContentPane().add(pane, BorderLayout.CENTER);
frame.setSize(500, 300);
frame.setLocation(300, 200);
frame.show();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}