看下我昨天的帖子,图片发不出去,大概描述了一下

解决方案 »

  1.   

    看到了,准确的说,就是像接近eclipse的那种风格?
      

  2.   

    在下也很佩服sunyiz,要是能跟他一起做项目就能提高的很快了。
      

  3.   

    楼主看下这个的效果吧
    不知道是不是你要的
    上图的话,先传到自己的CSDN相册
    然后就能发到论坛上了import java.awt.Color;
    import java.awt.Component;
    import java.awt.FontMetrics;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.LayoutManager;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.Vector;import javax.swing.Icon;
    import javax.swing.JComponent;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingUtilities;
    import javax.swing.plaf.basic.BasicTabbedPaneUI;
    import javax.swing.text.View;public class STabbedPane extends JTabbedPane {
    private static final long serialVersionUID = 1L;
    private Vector<Boolean> closable;
    private boolean showClose; public STabbedPane() {
    super();
    initialize();
    } /**
     * 构造方法
     * @param arg0 该参数为true时,无论是否选中,都显示可关闭按钮,
     * 为false时,只有选中时才显示
     */
    public STabbedPane(boolean arg0) {
    super();
    showClose = arg0;
    initialize();
    } private void initialize() {
    closable = new Vector<Boolean>(0);
    setUI(new STabbedPaneUI());
    } /**
     * 加入组件
     * @param title 标题
     * @param icon 图标
     * @param component 组件
     * @param tip 提示信息
     * @param closabel 是否可关闭
     */
    public void addTab(String title, Icon icon, Component component,
    String tip, boolean closable) {
    addTab(title, icon, component, tip);
    this.closable.add(closable);
    } /**
     * 移除组件
     * @param index 组件序号
     */
    public void removeTab(int index) {
    super.removeTabAt(index);
    closable.remove(index);
    } class STabbedPaneUI extends BasicTabbedPaneUI {
    private Rectangle[] closeRects = new Rectangle[0];
    private int nowIndex = -1;
    private int oldIndex = -1;
    private Color colorNorth = Color.WHITE;
    private Color colorSouth = new Color(157,167,195); public STabbedPaneUI() {
    super();
    addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    for (int i = 0; i < getTabCount(); i++) {
    if (closeRects[i].contains(e.getPoint())
    && closable.get(i)) {
    removeTab(i);
    }
    }
    }
    });
    addMouseMotionListener(new MouseAdapter() {
    public void mouseMoved(MouseEvent e) {
    nowIndex = -1;
    for (int i = 0; i < getTabCount(); i++) {
    if (closeRects[i].contains(e.getPoint())
    && closable.get(i)) {
    nowIndex = i;
    break;
    }
    }
    if (oldIndex != nowIndex) {
    if (nowIndex != -1) {
    repaint(closeRects[nowIndex]);// 控制重绘区域
    } else {
    if (oldIndex < getTabCount()) {
    repaint(closeRects[oldIndex]);// 控制重绘区域
    }
    }
    oldIndex = nowIndex;
    }
    }
    });
    } @Override
    protected void paintTab(Graphics g, int tabPlacement,
    Rectangle[] rects, int tabIndex, Rectangle iconRect,
    Rectangle textRect) {
    super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect);
    if (closable.get(tabIndex)
    && (showClose || tabIndex == getSelectedIndex())) {
    paintCloseIcon(g, tabIndex, tabIndex == nowIndex);
    }
    }

    @Override
        protected void paintTabBorder(Graphics g, int tabPlacement,
                    int tabIndex, int x, int y, int w, int h, boolean isSelected ) {
    g.setColor(Color.GRAY);
            switch (tabPlacement) {
              case LEFT:
                  g.drawLine(x+1, y+h-2, x+1, y+h-2); // bottom-left highlight
                  g.drawLine(x, y+2, x, y+h-3); // left highlight
                  g.drawLine(x+1, y+1, x+1, y+1); // top-left highlight
                  g.drawLine(x+2, y, x+w-1, y); // top highlight
                  g.drawLine(x+2, y+h-2, x+w-1, y+h-2); // bottom shadow
                  g.drawLine(x+2, y+h-1, x+w-1, y+h-1); // bottom dark shadow
                  break;
              case RIGHT:
                  g.drawLine(x, y, x+w-3, y); // top highlight
                  g.drawLine(x, y+h-2, x+w-3, y+h-2); // bottom shadow
                  g.drawLine(x+w-2, y+2, x+w-2, y+h-3); // right shadow
                  g.drawLine(x+w-2, y+1, x+w-2, y+1); // top-right dark shadow
                  g.drawLine(x+w-2, y+h-2, x+w-2, y+h-2); // bottom-right dark shadow
                  g.drawLine(x+w-1, y+2, x+w-1, y+h-3); // right dark shadow
                  g.drawLine(x, y+h-1, x+w-3, y+h-1); // bottom dark shadow
                  break;              
              case BOTTOM:
                  g.drawLine(x, y, x, y+h-3); // left highlight
                  g.drawLine(x+1, y+h-2, x+1, y+h-2); // bottom-left highlight
                  g.drawLine(x+2, y+h-2, x+w-3, y+h-2); // bottom shadow
                  g.drawLine(x+w-2, y, x+w-2, y+h-3); // right shadow
                  g.drawLine(x+2, y+h-1, x+w-3, y+h-1); // bottom dark shadow
                  g.drawLine(x+w-2, y+h-2, x+w-2, y+h-2); // bottom-right dark shadow
                  g.drawLine(x+w-1, y, x+w-1, y+h-3); // right dark shadow
                  break;
              case TOP:
              default:           
                  g.drawLine(x, y, x, y+h-1);
                  g.drawLine(x, y, x+w-1, y);
                  g.drawLine(x+w-1, y, x+w-1, y+h-1);
            }
        } @Override
    protected void paintTabBackground(Graphics g, int tabPlacement,
    int tabIndex, int x, int y, int w, int h, boolean isSelected) {
    GradientPaint gradient = new GradientPaint(x, y, colorNorth,
    x, y+h, colorSouth, true);
    Graphics2D g2d = (Graphics2D)g;
    g2d.setPaint(gradient);
            switch(tabPlacement) {
              case LEFT:
                  g.fillRect(x+1, y+1, w-1, h-3);
                  break;
              case RIGHT:
                  g.fillRect(x, y+1, w-2, h-3);
                  break;
              case BOTTOM:
                  g.fillRect(x+1, y, w-3, h-1);
                  break;
              case TOP:
              default:
                  g.fillRect(x+1, y+1, w-3, h-1);
            }
    } private void paintCloseIcon(Graphics g, int tabIndex, boolean entered) {
    Rectangle rect = closeRects[tabIndex];
    int x = rect.x;
    int y = rect.y;
    int[] xs = { x, x + 2, x + 4, x + 5, x + 7, x + 9, x + 9, x + 7,
    x + 7, x + 9, x + 9, x + 7, x + 5, x + 4, x + 2, x, x,
    x + 2, x + 2, x };
    int[] ys = { y, y, y + 2, y + 2, y, y, y + 2, y + 4, y + 5, y + 7,
    y + 9, y + 9, y + 7, y + 7, y + 9, y + 9, y + 7, y + 5,
    y + 4, y + 2 };
    if (entered) {
    g.setColor(new Color(252, 160, 160));
    } else {
    g.setColor(Color.WHITE);
    }
    g.fillPolygon(xs, ys, 20);
    g.setColor(Color.DARK_GRAY);
    g.drawPolygon(xs, ys, 20);
    } @Override
    protected void layoutLabel(int tabPlacement, FontMetrics metrics,
    int tabIndex, String title, Icon icon, Rectangle tabRect,
    Rectangle iconRect, Rectangle textRect, boolean isSelected) {
    textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
    View v = getTextViewForTab(tabIndex);
    if (v != null) {
    tabPane.putClientProperty("html", v);
    }
    SwingUtilities.layoutCompoundLabel((JComponent) tabPane, metrics,
    title, icon, SwingUtilities.CENTER, SwingUtilities.LEFT,
    SwingUtilities.CENTER, SwingUtilities.TRAILING, tabRect,
    iconRect, textRect, textIconGap);
    tabPane.putClientProperty("html", null); int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
    int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
    iconRect.x += xNudge + 3;
    iconRect.y += yNudge;
    textRect.x += xNudge + 3;
    textRect.y += yNudge;
    } @Override
    protected LayoutManager createLayoutManager() {
    return new TabbedPaneLayout();
    } @Override
    protected void assureRectsCreated(int tabCount) {
    super.assureRectsCreated(tabCount);
    int rectArrayLen = closeRects.length;
    if (tabCount != rectArrayLen) {
    Rectangle[] tempRectArray = new Rectangle[tabCount];
    System.arraycopy(closeRects, 0, tempRectArray, 0, Math.min(
    rectArrayLen, tabCount));
    closeRects = tempRectArray;
    for (int rectIndex = rectArrayLen; rectIndex < tabCount; rectIndex++) {
    closeRects[rectIndex] = new Rectangle();
    }
    }
    } class TabbedPaneLayout extends BasicTabbedPaneUI.TabbedPaneLayout {
    @Override
    protected void calculateTabRects(int tabPlacement, int tabCount) {
    super.calculateTabRects(tabPlacement, tabCount);
    for (int i = 0; i < tabCount; i++) {
    closeRects[i].x = rects[i].x + rects[i].width - 14;
    closeRects[i].y = rects[i].y + 6;
    closeRects[i].width = 10;
    closeRects[i].height = 10;
    }
    }
    }
    }
    }
      

  4.   

    代码太长了,我写博客里了
    http://blog.csdn.net/sunyiz/archive/2010/12/10/6068655.aspx
      

  5.   

    嗯。非常谢谢你,那个关闭的x,用图标drawImage实现了。很方便