- 浏览: 458059 次
- 性别:
- 来自: 江门
-
文章分类
最新评论
-
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 1123import java.util.Map; import ... -
Eclipse-jee-galileo的简单应用配置(包含Tomcat)
2012-03-23 17:43 1首先:下载 JDK 6,Eclip ... -
JDK官方下载
2011-11-27 22:54 882JDK1.6官方下载_JDK6官方下载地址:http:// ... -
使用Servlet Filter来防止SQL注入的方法
2010-08-28 14:29 1850SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的B ... -
避免XPath 注入的危险
2010-08-28 14:27 1500随着简单 XML API、Web 服 ... -
Xpath注入攻击及其防御技术研究
2010-08-28 14:25 14061 Xpath注入攻击概述 1.1 Xpath定义 近 ... -
突破SQL注入限制的一点想法
2010-08-28 14:19 1074突然想我们是否可以用什么方法绕过SQL注入的限制呢?到网上考察 ... -
JPanel上设置背景图片
2010-08-28 14:12 1517import java.awt.*; import javax ... -
通过Java Swing看透MVC设计模式
2010-08-28 14:11 989一个好的用户界面(GUI) ... -
jtree 默认展开树的全部节点
2010-08-28 14:06 5727public static void expandTr ... -
jTree 背景图片.节点颜色
2010-08-28 14:05 1648import java.awt.*; import java. ... -
JTree用法
2010-08-28 14:04 1340一、JTree用法及JTree使用经验总结 import ja ... -
Swing-JTree树模型的操作
2010-08-28 14:03 2430本文介绍Java Swing中的JTree模型,介绍如何创建、 ... -
常用的数据库连接写法和下载地址
2010-08-28 14:01 9071. MySQL(http://www.mysql.com)m ... -
java中常见的日期时间类
2010-08-28 13:20 839java中常见的日期时间类 以及时间转换 java中常见的 ... -
计算任意2个日期内的工作日(没有考虑到国定假日
2010-08-28 13:08 1133主要思路: 对于任意2个日期比如:date_start=20 ... -
Java获取各种常用时间方法
2010-08-28 13:07 769http://gigixin.iteye.com/blog/4 ... -
计算从beginDate到endDate的工作日
2010-08-28 13:06 1509public static int[] work_rest_a ... -
Java时间格式转换大全
2010-08-28 13:05 567import java.text.*; import java ... -
JAVA计划任务 定时器
2010-08-28 12:57 1452所有类型的 Java 应用程序一般都需要计划重复执行的任务。企 ...
相关推荐
描述:JTabbedPanel的美化标签很详细,找了很久。 根据提供的信息,本文将深入探讨如何通过自定义UI来美化JTabbedPanel中的标签,使其具有更吸引人的视觉效果。JTabbedPane是Java Swing框架中用于创建带有多个标签...
在Java Swing库中,`JTabbedPane`是一个非常重要的组件,它允许用户在多个面板(JPanel)之间切换,每个面板都可以包含不同的内容。自定义`JTabbedPane`的外观可以提升应用程序的用户体验,使其更加个性化和专业。...
java swing jTabbedPane 自定义的U,包括(宽、高、点击放大、文本字体、背景颜色、选项卡间距等) 简单易懂带注释可直接运行
拟阵约束下最大化子模函数的模型及其算法的一种熵聚类方法.pdf
内容概要:本文探讨了在两级电力市场环境中,针对省间交易商的最优购电模型的研究。文中提出了一个双层非线性优化模型,用于处理省内电力市场和省间电力交易的出清问题。该模型采用CVaR(条件风险价值)方法来评估和管理由新能源和负荷不确定性带来的风险。通过KKT条件和对偶理论,将复杂的双层非线性问题转化为更易求解的线性单层问题。此外,还通过实际案例验证了模型的有效性,展示了不同风险偏好设置对购电策略的影响。 适合人群:从事电力系统规划、运营以及风险管理的专业人士,尤其是对电力市场机制感兴趣的学者和技术专家。 使用场景及目标:适用于希望深入了解电力市场运作机制及其风险控制手段的研究人员和技术开发者。主要目标是为省间交易商提供一种科学有效的购电策略,以降低风险并提高经济效益。 其他说明:文章不仅介绍了理论模型的构建过程,还包括具体的数学公式推导和Python代码示例,便于读者理解和实践。同时强调了模型在实际应用中存在的挑战,如数据精度等问题,并指出了未来改进的方向。
内容概要:本文探讨了在MATLAB/Simulink平台上针对四机两区系统的风储联合调频技术。首先介绍了四机两区系统作为经典的电力系统模型,在风电渗透率增加的情况下,传统一次调频方式面临挑战。接着阐述了风储联合调频技术的应用,通过引入虚拟惯性控制和下垂控制策略,提高了系统的频率稳定性。文章展示了具体的MATLAB/Simulink仿真模型,包括系统参数设置、控制算法实现以及仿真加速方法。最终结果显示,在风电渗透率为25%的情况下,通过风储联合调频,系统频率特性得到显著提升,仿真时间缩短至5秒以内。 适合人群:从事电力系统研究、仿真建模的技术人员,特别是关注风电接入电网稳定性的研究人员。 使用场景及目标:适用于希望深入了解风储联合调频机制及其仿真实现的研究人员和技术开发者。目标是掌握如何利用MATLAB/Simulink进行高效的电力系统仿真,尤其是针对含有高比例风电接入的复杂场景。 其他说明:文中提供的具体参数配置和控制算法有助于读者快速搭建类似的仿真环境,并进行相关研究。同时强调了参考文献对于理论基础建立的重要性。
内容概要:本文介绍了永磁同步电机(PMSM)无感控制技术,特别是高频方波注入与滑膜观测器相结合的方法。首先解释了高频方波注入法的工作原理,即通过向电机注入高频方波电压信号,利用电机的凸极效应获取转子位置信息。接着讨论了滑膜观测器的作用,它能够根据电机的电压和电流估计转速和位置,具有较强的鲁棒性。两者结合可以提高无传感器控制系统的稳定性和精度。文中还提供了具体的Python、C语言和Matlab代码示例,展示了如何实现这两种技术。此外,简要提及了正弦波注入的相关论文资料,强调了其在不同工况下的优势。 适合人群:从事电机控制系统设计的研发工程师和技术爱好者,尤其是对永磁同步电机无感控制感兴趣的读者。 使用场景及目标:适用于需要减少传感器依赖、降低成本并提高系统可靠性的情况,如工业自动化设备、电动汽车等领域的电机控制。目标是掌握高频方波注入与滑膜观测器结合的具体实现方法,应用于实际工程项目中。 其他说明:文中提到的高频方波注入和滑膜观测器的结合方式,不仅提高了系统的性能,还在某些特殊情况下表现出更好的适应性。同时,附带提供的代码片段有助于读者更好地理解和实践这一技术。
内容概要:本文深入探讨了MATLAB中扩展卡尔曼滤波(EKF)和双扩展卡尔曼滤波(DEKF)在电池参数辨识中的应用。首先介绍了EKF的基本原理和代码实现,包括状态预测和更新步骤。接着讨论了DEKF的工作机制,即同时估计系统状态和参数,解决了参数和状态耦合估计的问题。文章还详细描述了电池参数辨识的具体应用场景,特别是针对电池管理系统中的荷电状态(SOC)估计。此外,提到了一些实用技巧,如雅可比矩阵的计算、参数初始值的选择、数据预处理方法等,并引用了几篇重要文献作为参考。 适合人群:从事电池管理系统开发的研究人员和技术人员,尤其是对状态估计和参数辨识感兴趣的读者。 使用场景及目标:适用于需要精确估计电池参数的实际项目,如电动汽车、储能系统等领域。目标是提高电池管理系统的性能,确保电池的安全性和可靠性。 其他说明:文章强调了实际应用中的注意事项,如数据处理、参数选择和模型优化等方面的经验分享。同时提醒读者关注最新的研究成果和技术进展,以便更好地应用于实际工作中。
内容概要:本文详细介绍了在无电子凸轮功能情况下,利用三菱FX3U系列PLC和威纶通触摸屏实现分切机上下收放卷张力控制的方法。主要内容涵盖硬件连接、程序框架设计、张力检测与读取、PID控制逻辑以及触摸屏交互界面的设计。文中通过具体代码示例展示了如何初始化寄存器、读取张力传感器数据、计算张力偏差并实施PID控制,最终实现稳定的张力控制。此外,还讨论了卷径计算、速度同步控制等关键技术点,并提供了现场调试经验和优化建议。 适合人群:从事自动化生产设备维护和技术支持的专业人士,尤其是熟悉PLC编程和触摸屏应用的技术人员。 使用场景及目标:适用于需要对分切机进行升级改造的企业,旨在提高分切机的张力控制精度,确保材料切割质量,降低生产成本。通过本方案可以实现±3%的张力控制精度,满足基本生产需求。 其他说明:本文不仅提供详细的程序代码和硬件配置指南,还分享了许多实用的调试技巧和经验,帮助技术人员更好地理解和应用相关技术。
内容概要:本文详细介绍了一种基于西门子S7-200和S7-300 PLC以及组态王软件的三泵变频恒压供水系统。主要内容涵盖IO分配、接线图原理图、梯形图程序编写和组态画面设计四个方面。通过合理的硬件配置和精确的编程逻辑,确保系统能够在不同负载情况下保持稳定的供水压力,同时实现节能和延长设备使用寿命的目标。 适合人群:从事工业自动化领域的工程师和技术人员,尤其是熟悉PLC编程和组态软件使用的专业人士。 使用场景及目标:适用于需要稳定供水的各种场合,如住宅小区、工厂等。目标是通过优化控制系统,提升供水效率,减少能源消耗,并确保系统的可靠性和安全性。 其他说明:文中提供了详细的实例代码和调试技巧,帮助读者更好地理解和实施该项目。此外,还分享了一些实用的经验教训,有助于避免常见的错误和陷阱。
内容概要:本文详细介绍了三相三线制静止无功发生器(SVG/STATCOM)在Simulink中的仿真模型设计与实现。主要内容涵盖ip-iq检测法用于无功功率检测、dq坐标系下的电流解耦控制、电压电流双闭环控制系统的设计、SVPWM调制技术的应用以及具体的仿真参数设置。文中不仅提供了理论背景,还展示了具体的Matlab代码片段,帮助读者理解各个控制环节的工作原理和技术细节。此外,文章还讨论了实际调试中遇到的问题及解决方案,强调了参数调整的重要性。 适合人群:从事电力系统自动化、电力电子技术研究的专业人士,特别是对SVG/STATCOM仿真感兴趣的工程师和研究人员。 使用场景及目标:适用于希望深入了解SVG/STATCOM工作原理并掌握其仿真方法的研究人员和工程师。目标是在实践中能够正确搭建和优化SVG/STATCOM的仿真模型,提高无功补偿的效果。 其他说明:文章提供了丰富的实例代码和调试技巧,有助于读者更好地理解和应用所学知识。同时,文中提及的一些经验和注意事项来源于实际项目,具有较高的参考价值。
基于SIMULINK的风力机发电效率建模探究.pdf
内容概要:本文介绍了如何将CarSim的动力学模型与Simulink的智能算法相结合,利用模型预测控制(MPC)实现车辆的智能超车换道。主要内容包括MPC控制器的设计、路径规划算法、联合仿真的配置要点以及实际应用效果。文中提供了详细的代码片段和技术细节,如权重矩阵设置、路径跟踪目标函数、安全超车条件判断等。此外,还强调了仿真过程中需要注意的关键参数配置,如仿真步长、插值设置等,以确保系统的稳定性和准确性。 适合人群:从事自动驾驶研究的技术人员、汽车工程领域的研究人员、对联合仿真感兴趣的开发者。 使用场景及目标:适用于需要进行自动驾驶车辆行为模拟的研究机构和企业,旨在提高超车换道的安全性和效率,为自动驾驶技术研发提供理论支持和技术验证。 其他说明:随包提供的案例文件已调好所有参数,可以直接导入并运行,帮助用户快速上手。文中提到的具体参数和配置方法对于初学者非常友好,能够显著降低入门门槛。
内容概要:本文详细介绍了利用MATLAB进行信号与系统实验的具体步骤和技术要点。首先讲解了常见信号(如方波、sinc函数、正弦波等)的生成方法及其注意事项,强调了时间轴设置和参数调整的重要性。接着探讨了卷积积分的两种实现方式——符号运算和数值积分,指出了各自的特点和应用场景,并特别提醒了数值卷积时的时间轴重构和步长修正问题。随后深入浅出地解释了频域分析的方法,包括傅里叶变换的符号计算和快速傅里叶变换(FFT),并给出了具体的代码实例和常见错误提示。最后阐述了离散时间信号与系统的Z变换分析,展示了如何通过Z变换将差分方程转化为传递函数以及如何绘制零极点图来评估系统的稳定性。 适合人群:正在学习信号与系统课程的学生,尤其是需要完成相关实验任务的人群;对MATLAB有一定基础,希望通过实践加深对该领域理解的学习者。 使用场景及目标:帮助学生掌握MATLAB环境下信号生成、卷积积分、频域分析和Z变换的基本技能;提高学生解决实际问题的能力,避免常见的编程陷阱;培养学生的动手能力和科学思维习惯。 其他说明:文中不仅提供了详细的代码示例,还分享了许多实用的小技巧,如如何正确保存实验结果图、如何撰写高质量的实验报告等。同时,作者以幽默风趣的语言风格贯穿全文,使得原本枯燥的技术内容变得生动有趣。
KUKA机器人相关文档
内容概要:本文详细介绍了无传感器永磁同步电机(PMSM)控制技术,特别是针对低速和中高速的不同控制策略。低速阶段采用I/F控制,通过固定电流幅值和斜坡加速的方式启动电机,确保平稳启动。中高速阶段则引入滑模观测器进行反电动势估算,从而精确控制电机转速。文中还讨论了两者之间的平滑切换逻辑,强调了参数选择和调试技巧的重要性。此外,提供了具体的伪代码示例,帮助读者更好地理解和实现这一控制方案。 适合人群:从事电机控制系统设计的研发工程师和技术爱好者。 使用场景及目标:适用于需要降低成本并提高可靠性的应用场景,如家用电器、工业自动化设备等。主要目标是掌握无传感器PMSM控制的基本原理及其优化方法。 其他说明:文中提到的实际案例和测试数据有助于加深理解,同时提醒开发者注意硬件参数准确性以及调试过程中可能出现的问题。
智能家居与物联网培训材料.ppt
内容概要:本文详细介绍了使用Matlab解决车辆路径规划问题的四种经典算法:TSP(旅行商问题)、CVRP(带容量约束的车辆路径问题)、CDVRP(带容量和距离双重约束的车辆路径问题)和VRPTW(带时间窗约束的车辆路径问题)。针对每个问题,文中提供了具体的算法实现思路和关键代码片段,如遗传算法用于TSP的基础求解,贪心算法和遗传算法结合用于CVRP的路径分割,以及带有惩罚函数的时间窗约束处理方法。此外,还讨论了性能优化技巧,如矩阵运算替代循环、锦标赛选择、2-opt局部优化等。 适合人群:具有一定编程基础,尤其是对物流调度、路径规划感兴趣的开发者和技术爱好者。 使用场景及目标:适用于物流配送系统的路径优化,旨在提高配送效率,降低成本。具体应用场景包括但不限于外卖配送、快递运输等。目标是帮助读者掌握如何利用Matlab实现高效的路径规划算法,解决实际业务中的复杂约束条件。 其他说明:文中不仅提供了详细的代码实现,还分享了许多实践经验,如参数设置、数据预处理、异常检测等。建议读者在实践中不断尝试不同的算法组合和优化策略,以应对更加复杂的实际问题。
软考网络工程师2010-2014真题及答案完整版 全国计算机软考 适合软考中级人群
包括:源程序工程文件、Proteus仿真工程文件、论文材料、配套技术手册等 1、采用51/52单片机作为主控芯片; 2、采用1602液晶显示:测量酒精值、酒驾阈值、醉驾阈值; 3、采用PCF8591进行AD模数转换; 4、LED指示:正常绿灯、酒驾黄灯、醉驾红灯; 5、可通过按键修改酒驾醉驾阈值;