📄 tabbedpanel.java
字号:
case TOP:
//
g.setColor(darkShadow);
g.drawLine(0, s.height - 1, s.width - 1, s.height - 1);
g.drawLine(s.width - 1, s.height - 1, s.width - 1, r.height - 1);
//
g.setColor(shadow);
g.drawLine(1, s.height - 2, s.width - 2, s.height - 2);
g.drawLine(s.width - 2, s.height - 2, s.width - 2, r.height);
//
g.setColor(highlight);
g.drawLine(0, s.height - 2, 0, r.height);
// Paint the tabs
int x = 0;
int selx = -1;
for (int i = 0; i < ncomponents; i++) {
Component comp = getComponent(i);
TabWrapper tab = (TabWrapper) tabs.get(comp);
if (tab != null) {
int hw = metrics.stringWidth(tab.text) + (HORIZONTAL_GAP * 2);
tab.bounds = new Rectangle(x, 0, hw, r.height);
if (sel == i) {
selx = x;
// g.setColor(darkShadow);
}
else {
g.setColor(lowlight);
g.fillRect(x, 3, hw - 1, r.height - 4);
//
g.setColor(midlight);
g.drawLine(x, 3, x, r.height - 2);
g.drawLine(x + 1, 2, x + hw - 2, 2);
//
g.setColor(darkShadow);
g.drawLine(x + hw - 1, 3, x + hw - 1, r.height - 2);
//
g.setColor(shadow);
g.drawLine(x + hw - 2, 4, x + hw - 2, r.height - 2);
//
g.setColor(highlight);
g.drawLine(x, r.height - 1, x + hw - 1, r.height - 1);
g.setColor(getForeground());
g.drawString(tab.text, x + HORIZONTAL_GAP,
(r.height / 2) + (metrics.getHeight() / 2) - 1);
// g.setColor(darkShadow);
}
x += hw;
}
}
g.setColor(highlight);
g.drawLine(x, r.height - 1, s.width - 2, r.height - 1);
if (selx != -1) {
x = selx;
Component comp = getComponent(sel);
TabWrapper tab = (TabWrapper) tabs.get(comp);
int hw = metrics.stringWidth(tab.text) + (HORIZONTAL_GAP * 2);
// g.setColor(getBackground());
// g.fillRect(x, 3, hw - 1, r.height - 3);
//
g.setColor(highlight);
g.drawLine(x, 1, x, r.height - 2);
g.drawLine(x + 1, 0, x + hw - 1, 0);
//
g.setColor(darkShadow);
g.drawLine(x + hw, 1, x + hw, r.height - 2);
//
g.setColor(shadow);
g.drawLine(x + hw - 1, 2, x + hw - 1, r.height - 2);
g.setColor(getForeground());
g.drawString(tab.text, x + HORIZONTAL_GAP,
(r.height / 2) + (metrics.getHeight() / 2) - 3);
}
break;
}
}
//
private Dimension getHeadingSize() {
Dimension s = new Dimension();
int c = getComponentCount();
for (int i = 0; i < c; i++) {
Component comp = getComponent(i);
TabWrapper tab = (TabWrapper) tabs.get(comp);
if (tab != null) {
int thw =
(HORIZONTAL_GAP * 2)
+
(metrics != null ?
metrics.stringWidth(tab.text == null ? "" : tab.text) : 0)
+ (tab.image == null ? 0 : (IMAGE_TEXT_GAP + tab.image.getWidth(this)));
int thh = (VERTICAL_GAP * 2) +
Math.max(metrics != null ? metrics.getHeight() : 0,
tab.image == null ? 0 : tab.image.getHeight(this));
if (position == TOP || position == BOTTOM) {
s.width += thw;
s.height = Math.max(s.height, thh);
}
else {
s.height += thh;
s.width = Math.max(s.width, thw);
}
}
else {
/*DEBUG*/System.err.println("TabWrapper for " + i + " [" + comp +
" could not be found");
}
}
return s;
}
/*
* Get the bounds of the tab headings
*/
private Rectangle getHeadingBounds() {
Dimension s = getHeadingSize();
Point loc = new Point();
switch (position) {
case BOTTOM:
loc.y = getSize().height - s.height;
break;
case RIGHT:
loc.x = getSize().width - s.width;
break;
}
return new Rectangle(loc.x, loc.y, s.width, s.height);
}
private int indexOfTab(TabWrapper tab) {
int c = getComponentCount();
for (int i = 0; i < c; i++) {
if (tab.component == getComponent(i)) {
return i;
}
}
return -1;
}
// Debug
public static void main(String[] args) {
Frame frame = new Frame("Tabs");
frame.setLayout(new BorderLayout());
Label l1 = new Label("Test label 1");
Panel p1 = new Panel(new BorderLayout());
p1.add(l1, BorderLayout.CENTER);
Label l2 = new Label("Test label 2");
Panel p2 = new Panel(new BorderLayout());
p2.add(l2, BorderLayout.CENTER);
Label l3 = new Label("Test label 3");
Panel p3 = new Panel(new BorderLayout());
p3.add(l3, BorderLayout.CENTER);
Label l4 = new Label("Test label 4");
Panel p4 = new Panel(new BorderLayout());
p4.add(l4, BorderLayout.CENTER);
TabbedPanel tabs = new TabbedPanel(TabbedPanel.TOP);
tabs.add("Test Tab 1", p1);
tabs.add("Test Tab 2", p2);
tabs.add("Test Tab 3", p3);
tabs.add("Test Tab 1", p4);
tabs.setSelectedTab(1);
frame.add(tabs, BorderLayout.CENTER);
frame.pack();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
frame.setVisible(true);
}
// Supporting classes
class TabWrapper {
Component component;
String text;
Image image;
Rectangle bounds;
Object constraints;
String name;
TabWrapper(String name, String text, Image image, Component component,
Object contraints) {
this.name = name;
this.text = text;
this.image = image;
this.component = component;
this.constraints = contraints;
}
}
class TabbedLayout
extends CardLayout {
TabbedLayout(int hgap, int vgap) {
super(hgap, vgap);
}
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
Component comp = null;
boolean currentFound = false;
Dimension s = getHeadingSize();
int headingSize = position == TOP || position == BOTTOM ? s.height :
s.width;
insets =
new Insets(
insets.top + (position == TOP ? headingSize : 0),
insets.left + (position == LEFT ? headingSize : 0),
insets.bottom + (position == BOTTOM ? headingSize : 0),
insets.right + (position == BOTTOM ? headingSize : 0));
for (int i = 0; i < ncomponents; i++) {
comp = parent.getComponent(i);
comp.setBounds(
getHgap() + insets.left,
getVgap() + insets.top,
parent.getSize().width -
(getHgap() * 2 + insets.left + insets.right),
parent.getSize().height -
(getVgap() * 2 + insets.top + insets.bottom));
if (comp.isVisible()) {
currentFound = true;
}
}
if (!currentFound && ncomponents > 0) {
parent.getComponent(0).setVisible(true);
}
}
}
/*
* (non-Javadoc)
*
* @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
*/
public Dimension minimumLayoutSize(Container parent) {
Dimension s = super.maximumLayoutSize(parent);
Dimension c = getHeadingSize();
return position == TOP
|| position == BOTTOM
? new Dimension(Math.max(s.width, c.width), s.height + c.height)
: new Dimension(s.width + c.width, Math.max(s.height, c.height));
}
/*
* (non-Javadoc)
*
* @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
*/
public Dimension preferredLayoutSize(Container parent) {
Dimension s = super.preferredLayoutSize(parent);
Dimension c = getHeadingSize();
return position == TOP
|| position == BOTTOM
? new Dimension(Math.max(s.width, c.width), s.height + c.height)
: new Dimension(s.width + c.width, Math.max(s.height, c.height));
}
}
/**
* @return
*/
public int getSelectedTabIndex() {
return sel;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -