异度之刃pro手柄2和pro手柄二者2选1还怎么做选择

有没有办法设置JtabbedPane的tab(选项卡)的高度和宽度?该如何处理_读书人
有没有办法设置JtabbedPane的tab(选项卡)的高度和宽度?该如何处理
&来源:读书人网&【读书人网():综合教育门户网站】
有没有办法设置JtabbedPane的tab(选项卡)的高度和宽度?如题.[解决办法]可以, 自己写一个 YourPaneUI exten
有没有办法设置JtabbedPane的tab(选项卡)的高度和宽度?如题.[解决办法]可以, 自己写一个 YourPaneUI extends BasicTabbedPaneUI
下面2个方法就是画tab的高度和宽度
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics)
protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight)
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
return super.calculateTabWidth(tabPlacement, tabIndex, metrics)+80;
protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight)
return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight)+80;
就画出了大的tab
JTabbedPane yourTabPane = new JTabbedPane();
YourPaneUI yourPaneUI = new YourPaneUI();
youtTabPane.setUI(yourPaneUI);JTabbedPanel的美化标签(转)
那么现在来说下用法吧,首先附上网上找来的一段代码(其实在csdn里面也有这个代码的资源,不过没说怎么用),
import java.awt.AlphaC
import java.awt.C
import java.awt.FontM
import java.awt.GradientP
import java.awt.G
import java.awt.Graphics2D;
import java.awt.I
import java.awt.LayoutM
import java.awt.R
import java.awt.RenderingH
import java.awt.S
import java.awt.event.MouseE
import java.awt.event.MouseL
import java.awt.event.MouseMotionL
import java.awt.geom.A
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JC
import javax.swing.JTabbedP
import javax.ponentUI;
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 selectedColorS
&private ColorSet defaultColorS
&private ColorSet hoverColorS
&private boolean contentTopBorderDrawn =
&private Color lineColor = new Color(158, 158,
&private Color dividerColor = new Color(200, 200,
&private Insets contentInsets = new Insets(1, 1,
&private int lastRollOverTab = -1;
&public static ComponentUI
createUI(JComponent c) {
&&return new
SinaTabbedPaneUI();
&protected LayoutManager
createLayoutManager() {
(tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT)
super.createLayoutManager();
&&} else {
&&&return new
TabbedPaneLayout();
&// protected void installComponents()
&// super.installComponents();
&// int counts =
tabPane.getComponents().
&// for (int i = 0; i &
&// if (tabPane.getComponentAt(i) instanceof
&// ((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
&&contentTopBorderDrawn =
&public void setContentInsets(Insets i)
&&contentInsets =
&public void setContentInsets(int i) {
&&contentInsets = new Insets(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,
&protected boolean
scrollableTabLayoutEnabled() {
&protected Insets getContentBorderInsets(int
tabPlacement) {
&&return contentI
&// protected Insets getTabAreaInsets(int
tabPlacement) {
&// return contentI
&protected int calculateTabHeight(int
tabPlacement, int tabIndex,
fontHeight) {
&&return 21;
&protected int calculateTabWidth(int
tabPlacement, int tabIndex,
&&&FontMetrics
metrics) {
super.calculateTabWidth(tabPlacement, tabIndex, metrics);
&&int wid =
metrics.charWidth('M');
&&w += wid * 2;
&protected int calculateMaxTabHeight(int
tabPlacement) {
&&return 20;
&protected void paintTabArea(Graphics g, int
tabPlacement, int selectedIndex) {
&&Graphics2D g2d = (Graphics2D)
&&// g2d.setPaint(new
GradientPaint(0, 0, defaultColorSet.topGradColor1,
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)
&&ColorSet colorS
&&Rectangle rect =
rects[tabIndex];
&&if (isSelected) {
&&&colorSet =
selectedColorS
&&} else if (getRolloverTab() ==
tabIndex) {
&&&colorSet =
hoverColorS
&&} else {
&&&colorSet =
defaultColorS
&&g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
&&&&RenderingHints.VALUE_ANTIALIAS_ON);
&&int width =
&&int xpos = rect.x;
&&int yPos = rect.y;
&&if (tabIndex &
&&&width--;
&&&xpos++;
&&&yPos +=
&&g2d.setPaint(new
GradientPaint(xpos, 0, colorSet.topGradColor1, xpos,
/ 2, colorSet.topGradColor2));
&&g2d.fill(this.getUpArea(xpos,
yPos, width, h - 2));
&&g2d.setPaint(new
GradientPaint(0, h / 2, colorSet.bottomGradColor1, 0,
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,
&&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);
&private Shape getUpArea(int x, int y, int w, int
&&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);
&private Shape getDownArea(int x, int y, int w,
&&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);
&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)
&&Composite old =
g2.getComposite();
&&AlphaComposite comp =
AlphaComposite.getInstance(
&&&&AlphaComposite.SRC_OVER,
&&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,
selectedIndex, int x, int y, int w, int h) {
&protected void
paintContentBorderRightEdge(Graphics g, int tabPlacement,
selectedIndex, int x, int y, int w, int h) {
&&// Do nothing
&protected void
paintContentBorderLeftEdge(Graphics g, int tabPlacement,
selectedIndex, int x, int y, int w, int h) {
&&// Do nothing
&protected void
paintContentBorderBottomEdge(Graphics g, int tabPlacement,
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;
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() {
currentRollOver = getRolloverTab();
(currentRollOver != lastRollOverTab) {
&&&&lastRollOverTab
= currentRollO
&&&&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. 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. i++) {
//&&&&Rectangle
selRect = rects[i];
//&&&&Insets
padInsets = getSelectedTabPadInsets(tabPlacement);
//&&&&selRect.x
+= padInsets.
//&&&&selRect.width
-= (padInsets.left + padInsets.right);
//&&&&selRect.y
-= padInsets.
//&&&&selRect.height
+= (padInsets.top + padInsets.bottom);
//&&protected void
normalizeTabRuns(int tabPlacement, int tabCount,
start, int max) {
(tabPlacement == TOP || tabPlacement == BOTTOM) {
//&&&&super.normalizeTabRuns(tabPlacement,
tabCount, start, max);
//&&// Don't rotate runs!
//&&protected void
rotateTabRuns(int tabPlacement, int selectedRun) {
这就是那段代码了,我稍微改动了几个地方,接着的一段文字是有关这里用到得几个方法的用处
具体颜色没怎么调,贵在神似阿呵呵
下面简要说一下自定义这种Tab的关键点:
每个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());
这里就用到了,这样刚才的效果就实现了
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。有没有办法设置JtabbedPane的tab(选项卡)的高度和宽度?如题.
回答1:可以, 自己写一个 YourPaneUI extends BasicTabbedPaneUI
下面2个方法就是画tab的高度和宽度
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics)
protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight)
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
return super.calculateTabWidth(tabPlacement, tabIndex, metrics)+80;
protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight)
return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight)+80;
就画出了大的tab
JTabbedPane yourTabPane = new JTabbedPane();
YourPaneUI yourPaneUI = new YourPaneUI();
youtTabPane.setUI(yourPaneUI);17542人阅读
swing(24)
这篇文章来自
JTabbedPane组件为我们提供了一种非常好的方法在窗体上显示很多的控件。我们可以将不同类别的控件放到不同的Tab页上,然后通过需要点击相应的Tab页。在传统的 Tab页上只能防止文本的图标。而在 Java SE 6中使我们可以直接将控件放到 Tab上。我们可以通过 setTabComponentAt方法将控件放到Tab上。这个方法有两个参数,一个是Tab的索引,另一个是要放置的对象。
JTabbedPane pane = new JTabbedPane();
pane.setTabComponentAt(1, component);
在JTabbedPane控件中有3个常用的方法,setTabComponentAt(int index, Component comp), getTabComponentAt (int index)和indexOfTabComponent(Component)。最后一个方法将替换Tab上的控件。下面的代码是一个关于JTabbedPane控件的演示。import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class TestTabbedPane
static void addIt(JTabbedPane tabbedPane, String text)
JLabel label = new JLabel(text);
JButton button = new JButton(text);
JPanel panel = new JPanel();
panel.add(label);
panel.add(button);
tabbedPane.addTab(text, panel);
if(text.equals ("tab4"))
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1,
new JTextField("插入了文本控件" ));
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() -
public static void main(String args[])
JFrame f = new JFrame(" JTabbedPane演示");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
addIt(tabbedPane, "tab1");
addIt(tabbedPane, "tab2");
addIt(tabbedPane, "tab3");
addIt(tabbedPane, "tab4");
addIt(tabbedPane, "tab5");
f.add(tabbedPane, BorderLayout.CENTER);
f.setSize(400, 200);
f.setVisible(true);
下图是显示界面,其中在Tab4上插入了一个文本控件,在Tab1至Tab5上各插入了一个按钮控件。
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:738301次
积分:6309
积分:6309
排名:第3483名
原创:82篇
转载:42篇
评论:197条
(1)(3)(3)(7)(12)(1)(1)(1)(2)(5)(2)(5)(1)(1)(1)(1)(2)(6)(4)(2)(2)(2)(6)(1)(7)(1)(1)(1)(3)(3)(3)(4)(1)(9)(5)(1)(2)(3)(2)(3)(2)(1)}

我要回帖

更多关于 ns异度之刃2pro手柄 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信