java容器tabbed_Java BasicTabbedPaneUI.java 中paint,paintTabArea,paintTab等

由于希望改变TabbedPanel的tab的形状,查看了BasicTabbedPaneUI.java的一部分代码,

其中:

void paint ->paintContentBorder

paintTabArea (整个区域?)         -> paintTab -> paintTabBackground

paintTabBorder(外框)

paintText(标题)

paintIcon(图片)

paintFocusIndicator

据我目前的理解应该是:要修改形状,主要是修改paintTabBackground和paintTabBorder?

Tab上Label的位置是由layoutLabel来决定。

下面是一个例子:

大部分函数是从BasicTabbedPaneUI.java 拷贝过来,但是修改了斜体字部分。

将Tab的形状修改为类似: 52ec014be5fa22d396f21adb054337e0.gif

Codes:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.Shape;

import java.awt.geom.Area;

import java.awt.geom.Path2D;

import java.awt.geom.Rectangle2D;

import javax.swing.Icon;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTabbedPane;

import javax.swing.SwingUtilities;

import javax.swing.text.View;

public class TabShapeTest extends JFrame{

/**

*

*/

private static final long serialVersionUID = 1L;

public static void main(String [] args)  {

new TabShapeTest().setVisible(true);

}

public TabShapeTest() {

JTabbedPane tpShape = new JTabbedPane();

tpShape.setUI(new ShapeTabTabbedPaneUI());

add(tpShape);

setPreferredSize(new Dimension(280,300));

tpShape.addTab("first",new JPanel());

tpShape.addTab("second?????", new JPanel());

tpShape.addTab("third", new JPanel());

tpShape.setBackgroundAt(1, Color.yellow);

tpShape.setBackgroundAt(2, Color.BLUE);

pack();

}

public static class ShapeTabTabbedPaneUI extends javax.swing.plaf.basic.BasicTabbedPaneUI {

private   Color selectedColor;

protected void installDefaults() {

super.installDefaults();

}

public ShapeTabTabbedPaneUI() {

}

protected void paintTabBackground(Graphics g, int tabPlacement,

int tabIndex, int x, int y, int w, int h, boolean isSelected) {

Graphics2D g2d = (Graphics2D) g;

g.setColor(!isSelected || selectedColor == null?

tabPane.getBackgroundAt(tabIndex) : selectedColor);

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);

g2d.fill(this.getTabArea(x+1, y+1, w-11, h-1));            }

}

private Shape getTabArea(int x, int y, int w, int h) {

Rectangle2D rec = new Rectangle2D.Double(x, y, w, h );

Area aRec = new Area(rec);

Path2D.Double triangle=new Path2D.Double();

triangle.moveTo(x+w, y);

triangle.lineTo(x+w+10, y+h/2);

triangle.lineTo(x+w, y+h);

triangle.closePath();

aRec.add(new Area(triangle));

triangle.reset();

return aRec;

}

protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,

int x, int y, int w, int h, boolean isSelected) {

g.setColor(lightHighlight);

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.setColor(shadow);

g.drawLine(x+2, y+h-2, x+w-1, y+h-2); // bottom shadow

g.setColor(darkShadow);

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.setColor(shadow);

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.setColor(darkShadow);

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.setColor(shadow);

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.setColor(darkShadow);

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+2, x, y+h-1); // left highlight

g.drawLine(x+1, y+1, x+1, y+1); // top-left highlight

//  g.drawLine(x+2, y, x+w-3, y); // top highlight

g.drawLine(x+2,y, x+w-10, y);

g.setColor(shadow);

//  g.drawLine(x+w-2, y+2, x+w-2, y+h-1); // right shadow

g.drawLine(x+w-10,y, x+w-1, y+h/2-1);

g.drawLine(x+w-1,y+h/2+1,x+w-10,y+h);

g.setColor(darkShadow);

//g.drawLine(x+w-1, y+2, x+w-1, y+h-1); // right dark-shadow

//g.drawLine(x+w-2, y+1, x+w-2, y+1); // top-right shadow

g.drawLine(x+w-1,y+h/2+2,x+w-10,y+h+1);                }

}

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(tabPane,

metrics, title, icon,

SwingUtilities.CENTER,

SwingUtilities.CENTER,

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;

iconRect.y += yNudge;

textRect.y += yNudge;

switch (tabPlacement) {

case LEFT:

textRect.x += xNudge;

break;

case RIGHT:

textRect.x += xNudge;

break;

case BOTTOM:

textRect.x += xNudge;

break;

case TOP:

default:

textRect.x += xNudge-4;                }

}

}

}

结果:

e4f28b6b53e9a9ce1a4dd096a3a47e27.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值