- 浏览: 452797 次
- 性别:
- 来自: 江门
文章分类
最新评论
-
ctz_131415:
谁能告诉我这个分页控件如何添加到窗口上?谢谢了
C# Winform DataGridView 公共分页实现 -
omymy:
FrmPage_Shown是放在页面加载时的load事件里的, ...
C# Winform DataGridView 公共分页实现 -
qie:
如何使用该分页控件?
C# Winform DataGridView 公共分页实现 -
anyanlonga:
看地图干什么用啊,如果查公交的话,自认为还是白度好
Android 上创建和地图相关的应用,用 Google 地图 API 好,还是百度等第三方地图 API 好 -
dedepro:
果然很强。
CSS层 固定置顶部兼容IE6等以上浏览器
么现在来说下用法吧,首先附上网上找来的一段代码(其实在csdn里面也有这个代码的资源,不过没说怎么用),
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
public class SinaTabbedPaneUI extends BasicTabbedPaneUI {
private static final Insets NO_INSETS = new Insets(0, 0, 0, 0);
private ColorSet selectedColorSet;
private ColorSet defaultColorSet;
private ColorSet hoverColorSet;
private boolean contentTopBorderDrawn = true;
private Color lineColor = new Color(158, 158, 158);
private Color dividerColor = new Color(200, 200, 200);
private Insets contentInsets = new Insets(1, 1, 1, 1);
private int lastRollOverTab = -1;
public static ComponentUI createUI(JComponent c) {
return new SinaTabbedPaneUI();
}
protected LayoutManager createLayoutManager() {
if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) {
return super.createLayoutManager();
} else {
return new TabbedPaneLayout();
}
}
// protected void installComponents() {
// super.installComponents();
// int counts = tabPane.getComponents().length;
// for (int i = 0; i < counts; i++) {
// if (tabPane.getComponentAt(i) instanceof JPanel) {
// ((JPanel) tabPane.getComponentAt(i)).setLayout(new FlowLayout(
// 10, 0, FlowLayout.LEFT));
// ((JPanel) tabPane.getComponentAt(i)).updateUI();
// }
// }
//
// }
public SinaTabbedPaneUI() {
selectedColorSet = new ColorSet();
selectedColorSet.topGradColor1 = new Color(255,0,0);//new Color(233, 237, 248);//选中的最上层
selectedColorSet.topGradColor2 = new Color(225,225,30);//new Color(158, 199, 240);//选中的第二层
selectedColorSet.bottomGradColor1 = new Color(225,225,30);//new Color(112, 173, 239);//选中的第三层
selectedColorSet.bottomGradColor2 = new Color(255,0,0);//new Color(183, 244, 253);//选中的最下层
defaultColorSet = new ColorSet();
defaultColorSet.topGradColor1 = new Color(73,50,205);//new Color(253, 253, 253);//未选的最上层
defaultColorSet.topGradColor2 = new Color(0,255,255);//new Color(237, 237, 237);
defaultColorSet.bottomGradColor1 = new Color(0,255,255);//new Color(222, 222, 222);
defaultColorSet.bottomGradColor2 = new Color(73,50,205);//new Color(255, 255, 255);
hoverColorSet = new ColorSet();
hoverColorSet.topGradColor1 = new Color(245,245,10);//new Color(244, 244, 244);//鼠标在的时候最上层
hoverColorSet.topGradColor2 = new Color(237,18,117);//new Color(223, 223, 223);
hoverColorSet.bottomGradColor1 = new Color(5,232,255);//new Color(211, 211, 211);
hoverColorSet.bottomGradColor2 = new Color(19,236,40);//new Color(235, 235, 235);
maxTabHeight = 21;
setContentInsets(1);
}
public void setContentTopBorderDrawn(boolean b) {
contentTopBorderDrawn = b;
}
public void setContentInsets(Insets i) {
contentInsets = i;
}
public void setContentInsets(int i) {
contentInsets = new Insets(i, i, i, i);
}
// 返回当前运行的用于显示选项卡的选项卡数
public int getTabRunCount(JTabbedPane pane) {
return 1;
}
protected void installDefaults() {
super.installDefaults();
RollOverListener l = new RollOverListener();
tabPane.addMouseListener(l);
tabPane.addMouseMotionListener(l);
tabAreaInsets = NO_INSETS; // tab色insets边框
tabInsets = new Insets(0, 0, 50, 50);
}
protected boolean scrollableTabLayoutEnabled() {
return false;
}
protected Insets getContentBorderInsets(int tabPlacement) {
return contentInsets;
}
// protected Insets getTabAreaInsets(int tabPlacement) {
//
// return contentInsets;
// }
protected int calculateTabHeight(int tabPlacement, int tabIndex,
int fontHeight) {
return 21;
}
protected int calculateTabWidth(int tabPlacement, int tabIndex,
FontMetrics metrics) {
int w = super.calculateTabWidth(tabPlacement, tabIndex, metrics);
int wid = metrics.charWidth('M');
w += wid * 2;
return w;
}
protected int calculateMaxTabHeight(int tabPlacement) {
return 20;
}
protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
Graphics2D g2d = (Graphics2D) g;
// g2d.setPaint(new GradientPaint(0, 0, defaultColorSet.topGradColor1,
// 0,
// 10, defaultColorSet.topGradColor2));
// g2d.fillRect(0, 0, tabPane.getWidth(), 10);
//
// g2d.setPaint(new GradientPaint(0, 10,
// defaultColorSet.bottomGradColor1,
// 0, 21, defaultColorSet.bottomGradColor2));
// g2d.fillRect(0, 10, tabPane.getWidth(), 11);
super.paintTabArea(g, tabPlacement, selectedIndex);
if (contentTopBorderDrawn) {
g2d.setColor(lineColor);
g2d.drawLine(0, 20, tabPane.getWidth() - 1, 20);
}
}
protected void paintTabBackground(Graphics g, int tabPlacement,
int tabIndex, int x, int y, int w, int h, boolean isSelected) {
Graphics2D g2d = (Graphics2D) g;
ColorSet colorSet;
Rectangle rect = rects[tabIndex];
if (isSelected) {
colorSet = selectedColorSet;
} else if (getRolloverTab() == tabIndex) {
colorSet = hoverColorSet;
} else {
colorSet = defaultColorSet;
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int width = rect.width;
int xpos = rect.x;
int yPos = rect.y;
if (tabIndex > -1) {
width--;
xpos++;
yPos += 2;
}
g2d.setPaint(new GradientPaint(xpos, 0, colorSet.topGradColor1, xpos,
h / 2, colorSet.topGradColor2));
g2d.fill(this.getUpArea(xpos, yPos, width, h - 2));
g2d.setPaint(new GradientPaint(0, h / 2, colorSet.bottomGradColor1, 0,
h, colorSet.bottomGradColor2));
g2d.fill(this.getDownArea(xpos, yPos, width, h - 2));
if (contentTopBorderDrawn) {
g2d.setColor(lineColor);
g2d.drawLine(rect.x, 20, rect.x + rect.width - 1, 20);
}
}
private Shape getArea(int x, int y, int w, int h) {
RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
Area a = new Area(rect);
Rectangle2D rec = new Rectangle2D.Float(x, y + h / 2, w, h / 2);
Area b = new Area(rec);
a.add(b);
return a;
}
private Shape getUpArea(int x, int y, int w, int h) {
Rectangle2D rec = new Rectangle2D.Float(x, y, w, h / 2 + 1);
Area a = new Area(rec);
RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
Area b = new Area(rect);
a.intersect(b);
return a;
}
private Shape getDownArea(int x, int y, int w, int h) {
Rectangle2D rec = new Rectangle2D.Float(x, y + h / 2, w, h / 2 + 1);
Area a = new Area(rec);
RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
// Area b = new Area(rect);
// a.intersect(b);
return a;
}
protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
int x, int y, int w, int h, boolean isSelected) {
Rectangle rect = getTabBounds(tabIndex, new Rectangle(x, y, w, h));
// g.setColor(dividerColor);
Graphics2D g2 = (Graphics2D) g;
Composite old = g2.getComposite();
AlphaComposite comp = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.0f);
g2.setComposite(comp);
g2.setColor(dividerColor);
g2.drawLine(rect.x + rect.width, 0, rect.x + rect.width, 20);
g2.setComposite(old);
}
protected void paintContentBorderTopEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
}
protected void paintContentBorderRightEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
// Do nothing
}
protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
// Do nothing
}
protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
// Do nothing
}
protected void paintFocusIndicator(Graphics g, int tabPlacement,
Rectangle[] rects, int tabIndex, Rectangle iconRect,
Rectangle textRect, boolean isSelected) {
// Do nothing
}
protected int getTabLabelShiftY(int tabPlacement, int tabIndex,
boolean isSelected) {
return 0;
}
private class ColorSet {
Color topGradColor1;
Color topGradColor2;
Color bottomGradColor1;
Color bottomGradColor2;
}
private class RollOverListener implements MouseMotionListener,
MouseListener {
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
checkRollOver();
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
checkRollOver();
}
public void mouseExited(MouseEvent e) {
tabPane.repaint();
}
private void checkRollOver() {
int currentRollOver = getRolloverTab();
if (currentRollOver != lastRollOverTab) {
lastRollOverTab = currentRollOver;
Rectangle tabsRect = new Rectangle(0, 0, tabPane.getWidth(), 20);
tabPane.repaint(tabsRect);
}
}
}
public class TabbedPaneLayout extends BasicTabbedPaneUI.TabbedPaneLayout {
public TabbedPaneLayout() {
SinaTabbedPaneUI.this.super();
}
protected void calculateTabRects(int tabPlacement, int tabCount) {
super.calculateTabRects(tabPlacement, tabCount);
for (int i = 0; i < rects.length; i++) {
rects[i].x = rects[i].x + (5 * i);
}
}
protected void padSelectedTab(int tabPlacement, int selectedIndex) {
// don't pad only the selected tab, but all the tabs, to space them.
// for (int i = 0; i < rects.length; i++) {
// Rectangle selRect = rects[i];
// Insets padInsets = getSelectedTabPadInsets(tabPlacement);
// selRect.x += padInsets.left;
// selRect.width -= (padInsets.left + padInsets.right);
// selRect.y -= padInsets.top;
// selRect.height += (padInsets.top + padInsets.bottom);
// }
}
// protected void normalizeTabRuns(int tabPlacement, int tabCount,
// int start, int max) {
// if (tabPlacement == TOP || tabPlacement == BOTTOM) {
// super.normalizeTabRuns(tabPlacement, tabCount, start, max);
// }
// }
//
// // Don't rotate runs!
// protected void rotateTabRuns(int tabPlacement, int selectedRun) {
// }
}
}
这就是那段代码了,我稍微改动了几个地方,接着的一段文字是有关这里用到得几个方法的用处
具体颜色没怎么调,贵在神似阿呵呵
下面简要说一下自定义这种Tab的关键点:
1. 每个JComponent几乎都有其UI 委托对象类,当然JTabbedPane也不例外。我们首先继承BasicTabbedPaneUI类,这其中有我们必须要重写的一个方法,具体形式如下:
public static ComponentUI createUI(JComponent c) {
return new XXXTabbedPaneUI();
}
其中XXXTabbedPaneUI就是自己实现的BasicTabbedPaneUI的子类的名字。
2.下面类出几个改变外观的重要的方法:
a. protectedvoid installDefaults() //可以改变一些BasicTabbedPaneUI中默认的属性。
b.protectedvoid paintTabArea(Graphics g, int tabPlacement, int selectedIndex) //绘制整个选项卡区域
c. protectedvoid paintTabBackground(Graphics g, int tabPlacement,
int tabIndex, int x, int y, int w, int h, boolean isSelected)
//绘制某个选项卡的背景色
d. protectedvoid paintContentBorder(Graphics g, int tabPlacement,
int selectedIndex) //绘制TabbedPane容器的四周边框样式。
e. protectedvoid paintFocusIndicator(Graphics g, int tabPlacement,
Rectangle[] rects, int tabIndex, Rectangle iconRect,
Rectangle textRect, boolean isSelected)
//绘制选中某个Tab后,获得焦点的样式。
3.默认状态下每个选择项卡是连续摆放的,那么我们怎么使得他像新浪网页里那样的Tab中间有一定间隙呢,那么我们还定义一个内部类继承BasicTabbedPaneUI的内部类TabbedPaneLayou重写calculateTabRects方法,因为具体的选项卡标签位置是由变量rects所决定的。
根据上面的介绍是不是你也想自己动手实践一下呢??那还等什么,让我们共同努力让Swing更美丽!!
也许,看到这里你还是一头雾水,写了这个那到底怎么用它呢,其实很简单,在你要用的地方设置就行了,如下
UserJTab = new JTabbedPane();
UserJTab.setTabPlacement(JTabbedPane.TOP);
UserJTab.setBorder(null);
UserJTab.setUI(new SinaTabbedPaneUI());
这里就用到了,这样刚才的效果就实现了
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
public class SinaTabbedPaneUI extends BasicTabbedPaneUI {
private static final Insets NO_INSETS = new Insets(0, 0, 0, 0);
private ColorSet selectedColorSet;
private ColorSet defaultColorSet;
private ColorSet hoverColorSet;
private boolean contentTopBorderDrawn = true;
private Color lineColor = new Color(158, 158, 158);
private Color dividerColor = new Color(200, 200, 200);
private Insets contentInsets = new Insets(1, 1, 1, 1);
private int lastRollOverTab = -1;
public static ComponentUI createUI(JComponent c) {
return new SinaTabbedPaneUI();
}
protected LayoutManager createLayoutManager() {
if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) {
return super.createLayoutManager();
} else {
return new TabbedPaneLayout();
}
}
// protected void installComponents() {
// super.installComponents();
// int counts = tabPane.getComponents().length;
// for (int i = 0; i < counts; i++) {
// if (tabPane.getComponentAt(i) instanceof JPanel) {
// ((JPanel) tabPane.getComponentAt(i)).setLayout(new FlowLayout(
// 10, 0, FlowLayout.LEFT));
// ((JPanel) tabPane.getComponentAt(i)).updateUI();
// }
// }
//
// }
public SinaTabbedPaneUI() {
selectedColorSet = new ColorSet();
selectedColorSet.topGradColor1 = new Color(255,0,0);//new Color(233, 237, 248);//选中的最上层
selectedColorSet.topGradColor2 = new Color(225,225,30);//new Color(158, 199, 240);//选中的第二层
selectedColorSet.bottomGradColor1 = new Color(225,225,30);//new Color(112, 173, 239);//选中的第三层
selectedColorSet.bottomGradColor2 = new Color(255,0,0);//new Color(183, 244, 253);//选中的最下层
defaultColorSet = new ColorSet();
defaultColorSet.topGradColor1 = new Color(73,50,205);//new Color(253, 253, 253);//未选的最上层
defaultColorSet.topGradColor2 = new Color(0,255,255);//new Color(237, 237, 237);
defaultColorSet.bottomGradColor1 = new Color(0,255,255);//new Color(222, 222, 222);
defaultColorSet.bottomGradColor2 = new Color(73,50,205);//new Color(255, 255, 255);
hoverColorSet = new ColorSet();
hoverColorSet.topGradColor1 = new Color(245,245,10);//new Color(244, 244, 244);//鼠标在的时候最上层
hoverColorSet.topGradColor2 = new Color(237,18,117);//new Color(223, 223, 223);
hoverColorSet.bottomGradColor1 = new Color(5,232,255);//new Color(211, 211, 211);
hoverColorSet.bottomGradColor2 = new Color(19,236,40);//new Color(235, 235, 235);
maxTabHeight = 21;
setContentInsets(1);
}
public void setContentTopBorderDrawn(boolean b) {
contentTopBorderDrawn = b;
}
public void setContentInsets(Insets i) {
contentInsets = i;
}
public void setContentInsets(int i) {
contentInsets = new Insets(i, i, i, i);
}
// 返回当前运行的用于显示选项卡的选项卡数
public int getTabRunCount(JTabbedPane pane) {
return 1;
}
protected void installDefaults() {
super.installDefaults();
RollOverListener l = new RollOverListener();
tabPane.addMouseListener(l);
tabPane.addMouseMotionListener(l);
tabAreaInsets = NO_INSETS; // tab色insets边框
tabInsets = new Insets(0, 0, 50, 50);
}
protected boolean scrollableTabLayoutEnabled() {
return false;
}
protected Insets getContentBorderInsets(int tabPlacement) {
return contentInsets;
}
// protected Insets getTabAreaInsets(int tabPlacement) {
//
// return contentInsets;
// }
protected int calculateTabHeight(int tabPlacement, int tabIndex,
int fontHeight) {
return 21;
}
protected int calculateTabWidth(int tabPlacement, int tabIndex,
FontMetrics metrics) {
int w = super.calculateTabWidth(tabPlacement, tabIndex, metrics);
int wid = metrics.charWidth('M');
w += wid * 2;
return w;
}
protected int calculateMaxTabHeight(int tabPlacement) {
return 20;
}
protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
Graphics2D g2d = (Graphics2D) g;
// g2d.setPaint(new GradientPaint(0, 0, defaultColorSet.topGradColor1,
// 0,
// 10, defaultColorSet.topGradColor2));
// g2d.fillRect(0, 0, tabPane.getWidth(), 10);
//
// g2d.setPaint(new GradientPaint(0, 10,
// defaultColorSet.bottomGradColor1,
// 0, 21, defaultColorSet.bottomGradColor2));
// g2d.fillRect(0, 10, tabPane.getWidth(), 11);
super.paintTabArea(g, tabPlacement, selectedIndex);
if (contentTopBorderDrawn) {
g2d.setColor(lineColor);
g2d.drawLine(0, 20, tabPane.getWidth() - 1, 20);
}
}
protected void paintTabBackground(Graphics g, int tabPlacement,
int tabIndex, int x, int y, int w, int h, boolean isSelected) {
Graphics2D g2d = (Graphics2D) g;
ColorSet colorSet;
Rectangle rect = rects[tabIndex];
if (isSelected) {
colorSet = selectedColorSet;
} else if (getRolloverTab() == tabIndex) {
colorSet = hoverColorSet;
} else {
colorSet = defaultColorSet;
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int width = rect.width;
int xpos = rect.x;
int yPos = rect.y;
if (tabIndex > -1) {
width--;
xpos++;
yPos += 2;
}
g2d.setPaint(new GradientPaint(xpos, 0, colorSet.topGradColor1, xpos,
h / 2, colorSet.topGradColor2));
g2d.fill(this.getUpArea(xpos, yPos, width, h - 2));
g2d.setPaint(new GradientPaint(0, h / 2, colorSet.bottomGradColor1, 0,
h, colorSet.bottomGradColor2));
g2d.fill(this.getDownArea(xpos, yPos, width, h - 2));
if (contentTopBorderDrawn) {
g2d.setColor(lineColor);
g2d.drawLine(rect.x, 20, rect.x + rect.width - 1, 20);
}
}
private Shape getArea(int x, int y, int w, int h) {
RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
Area a = new Area(rect);
Rectangle2D rec = new Rectangle2D.Float(x, y + h / 2, w, h / 2);
Area b = new Area(rec);
a.add(b);
return a;
}
private Shape getUpArea(int x, int y, int w, int h) {
Rectangle2D rec = new Rectangle2D.Float(x, y, w, h / 2 + 1);
Area a = new Area(rec);
RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
Area b = new Area(rect);
a.intersect(b);
return a;
}
private Shape getDownArea(int x, int y, int w, int h) {
Rectangle2D rec = new Rectangle2D.Float(x, y + h / 2, w, h / 2 + 1);
Area a = new Area(rec);
RoundRectangle2D rect = new RoundRectangle2D.Float(x, y, w, h, 15, 15);
// Area b = new Area(rect);
// a.intersect(b);
return a;
}
protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex,
int x, int y, int w, int h, boolean isSelected) {
Rectangle rect = getTabBounds(tabIndex, new Rectangle(x, y, w, h));
// g.setColor(dividerColor);
Graphics2D g2 = (Graphics2D) g;
Composite old = g2.getComposite();
AlphaComposite comp = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.0f);
g2.setComposite(comp);
g2.setColor(dividerColor);
g2.drawLine(rect.x + rect.width, 0, rect.x + rect.width, 20);
g2.setComposite(old);
}
protected void paintContentBorderTopEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
}
protected void paintContentBorderRightEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
// Do nothing
}
protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
// Do nothing
}
protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement,
int selectedIndex, int x, int y, int w, int h) {
// Do nothing
}
protected void paintFocusIndicator(Graphics g, int tabPlacement,
Rectangle[] rects, int tabIndex, Rectangle iconRect,
Rectangle textRect, boolean isSelected) {
// Do nothing
}
protected int getTabLabelShiftY(int tabPlacement, int tabIndex,
boolean isSelected) {
return 0;
}
private class ColorSet {
Color topGradColor1;
Color topGradColor2;
Color bottomGradColor1;
Color bottomGradColor2;
}
private class RollOverListener implements MouseMotionListener,
MouseListener {
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
checkRollOver();
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
checkRollOver();
}
public void mouseExited(MouseEvent e) {
tabPane.repaint();
}
private void checkRollOver() {
int currentRollOver = getRolloverTab();
if (currentRollOver != lastRollOverTab) {
lastRollOverTab = currentRollOver;
Rectangle tabsRect = new Rectangle(0, 0, tabPane.getWidth(), 20);
tabPane.repaint(tabsRect);
}
}
}
public class TabbedPaneLayout extends BasicTabbedPaneUI.TabbedPaneLayout {
public TabbedPaneLayout() {
SinaTabbedPaneUI.this.super();
}
protected void calculateTabRects(int tabPlacement, int tabCount) {
super.calculateTabRects(tabPlacement, tabCount);
for (int i = 0; i < rects.length; i++) {
rects[i].x = rects[i].x + (5 * i);
}
}
protected void padSelectedTab(int tabPlacement, int selectedIndex) {
// don't pad only the selected tab, but all the tabs, to space them.
// for (int i = 0; i < rects.length; i++) {
// Rectangle selRect = rects[i];
// Insets padInsets = getSelectedTabPadInsets(tabPlacement);
// selRect.x += padInsets.left;
// selRect.width -= (padInsets.left + padInsets.right);
// selRect.y -= padInsets.top;
// selRect.height += (padInsets.top + padInsets.bottom);
// }
}
// protected void normalizeTabRuns(int tabPlacement, int tabCount,
// int start, int max) {
// if (tabPlacement == TOP || tabPlacement == BOTTOM) {
// super.normalizeTabRuns(tabPlacement, tabCount, start, max);
// }
// }
//
// // Don't rotate runs!
// protected void rotateTabRuns(int tabPlacement, int selectedRun) {
// }
}
}
这就是那段代码了,我稍微改动了几个地方,接着的一段文字是有关这里用到得几个方法的用处
具体颜色没怎么调,贵在神似阿呵呵
下面简要说一下自定义这种Tab的关键点:
1. 每个JComponent几乎都有其UI 委托对象类,当然JTabbedPane也不例外。我们首先继承BasicTabbedPaneUI类,这其中有我们必须要重写的一个方法,具体形式如下:
public static ComponentUI createUI(JComponent c) {
return new XXXTabbedPaneUI();
}
其中XXXTabbedPaneUI就是自己实现的BasicTabbedPaneUI的子类的名字。
2.下面类出几个改变外观的重要的方法:
a. protectedvoid installDefaults() //可以改变一些BasicTabbedPaneUI中默认的属性。
b.protectedvoid paintTabArea(Graphics g, int tabPlacement, int selectedIndex) //绘制整个选项卡区域
c. protectedvoid paintTabBackground(Graphics g, int tabPlacement,
int tabIndex, int x, int y, int w, int h, boolean isSelected)
//绘制某个选项卡的背景色
d. protectedvoid paintContentBorder(Graphics g, int tabPlacement,
int selectedIndex) //绘制TabbedPane容器的四周边框样式。
e. protectedvoid paintFocusIndicator(Graphics g, int tabPlacement,
Rectangle[] rects, int tabIndex, Rectangle iconRect,
Rectangle textRect, boolean isSelected)
//绘制选中某个Tab后,获得焦点的样式。
3.默认状态下每个选择项卡是连续摆放的,那么我们怎么使得他像新浪网页里那样的Tab中间有一定间隙呢,那么我们还定义一个内部类继承BasicTabbedPaneUI的内部类TabbedPaneLayou重写calculateTabRects方法,因为具体的选项卡标签位置是由变量rects所决定的。
根据上面的介绍是不是你也想自己动手实践一下呢??那还等什么,让我们共同努力让Swing更美丽!!
也许,看到这里你还是一头雾水,写了这个那到底怎么用它呢,其实很简单,在你要用的地方设置就行了,如下
UserJTab = new JTabbedPane();
UserJTab.setTabPlacement(JTabbedPane.TOP);
UserJTab.setBorder(null);
UserJTab.setUI(new SinaTabbedPaneUI());
这里就用到了,这样刚才的效果就实现了
发表评论
-
Java中HashMap的用法
2012-04-10 11:31 1098import java.util.Map; import ... -
Eclipse-jee-galileo的简单应用配置(包含Tomcat)
2012-03-23 17:43 1首先:下载 JDK 6,Eclip ... -
JDK官方下载
2011-11-27 22:54 860JDK1.6官方下载_JDK6官方下载地址:http:// ... -
使用Servlet Filter来防止SQL注入的方法
2010-08-28 14:29 1826SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的B ... -
避免XPath 注入的危险
2010-08-28 14:27 1472随着简单 XML API、Web 服 ... -
Xpath注入攻击及其防御技术研究
2010-08-28 14:25 13841 Xpath注入攻击概述 1.1 Xpath定义 近 ... -
突破SQL注入限制的一点想法
2010-08-28 14:19 1055突然想我们是否可以用什么方法绕过SQL注入的限制呢?到网上考察 ... -
JPanel上设置背景图片
2010-08-28 14:12 1500import java.awt.*; import javax ... -
通过Java Swing看透MVC设计模式
2010-08-28 14:11 967一个好的用户界面(GUI) ... -
jtree 默认展开树的全部节点
2010-08-28 14:06 5692public static void expandTr ... -
jTree 背景图片.节点颜色
2010-08-28 14:05 1615import java.awt.*; import java. ... -
JTree用法
2010-08-28 14:04 1294一、JTree用法及JTree使用经验总结 import ja ... -
Swing-JTree树模型的操作
2010-08-28 14:03 2393本文介绍Java Swing中的JTree模型,介绍如何创建、 ... -
常用的数据库连接写法和下载地址
2010-08-28 14:01 8861. MySQL(http://www.mysql.com)m ... -
java中常见的日期时间类
2010-08-28 13:20 819java中常见的日期时间类 以及时间转换 java中常见的 ... -
计算任意2个日期内的工作日(没有考虑到国定假日
2010-08-28 13:08 1100主要思路: 对于任意2个日期比如:date_start=20 ... -
Java获取各种常用时间方法
2010-08-28 13:07 744http://gigixin.iteye.com/blog/4 ... -
计算从beginDate到endDate的工作日
2010-08-28 13:06 1494public static int[] work_rest_a ... -
Java时间格式转换大全
2010-08-28 13:05 553import java.text.*; import java ... -
JAVA计划任务 定时器
2010-08-28 12:57 1414所有类型的 Java 应用程序一般都需要计划重复执行的任务。企 ...
相关推荐
描述:JTabbedPanel的美化标签很详细,找了很久。 根据提供的信息,本文将深入探讨如何通过自定义UI来美化JTabbedPanel中的标签,使其具有更吸引人的视觉效果。JTabbedPane是Java Swing框架中用于创建带有多个标签...
在Java Swing库中,`JTabbedPane`是一个非常重要的组件,它允许用户在多个面板(JPanel)之间切换,每个面板都可以包含不同的内容。自定义`JTabbedPane`的外观可以提升应用程序的用户体验,使其更加个性化和专业。...
java swing jTabbedPane 自定义的U,包括(宽、高、点击放大、文本字体、背景颜色、选项卡间距等) 简单易懂带注释可直接运行
tabbedPane.addTab("示例标签", panel); ``` 这个实现展示了如何通过自定义组件和事件监听来增强Swing的基本组件。通过这样的方式,开发者可以创造出符合特定需求的用户界面,提供更丰富的交互体验。 在提供的`...
在Java的Swing库中,`JTabbedPane`是一个非常重要的组件,用于创建具有选项卡式界面的应用程序。这个组件允许我们将多个组件(如按钮、文本框、面板等)组织成独立的页面,用户可以通过点击不同的选项卡来切换显示的...