1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| package swing.ui;
import java.awt.Dimension;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalTabbedPaneUI;
public class MyTabbedPaneUI extends MetalTabbedPaneUI {
private Icon southIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("/swing/ui/res/southIcon.png"));
private Icon northIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("/swing/ui/res/northIcon.png"));
private Icon eastIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("/swing/ui/res/eastIcon.png"));
private Icon westIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("/swing/ui/res/westIcon.png"));
public static ComponentUI createUI( JComponent x ) {
return new MyTabbedPaneUI();
}
@Override
protected JButton createScrollButton(int direction) {
System.out.println("getButton");
if (direction != SOUTH && direction != NORTH && direction != EAST &&
direction != WEST) {
throw new IllegalArgumentException("Direction must be one of: " +
"SOUTH, NORTH, EAST or WEST");
}
JButton b = new JButton();
b.setText("");
b.setPreferredSize(new Dimension(southIcon.getIconWidth(), southIcon.getIconHeight()));
if (direction == SOUTH) {
b.setIcon(southIcon);
} else if (direction == NORTH) {
b.setIcon(northIcon);
} else if (direction == WEST) {
b.setIcon(westIcon);
} else {
b.setIcon(eastIcon);
}
return b;
}
public static void main(String[] args) {
JTabbedPane pane = new JTabbedPane();
pane.setUI(new MyTabbedPaneUI());
pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
for(int i=0; i<40; i++) {
pane.addTab("tab "+i, new JLabel(""+i));
}
JFrame f = new JFrame();
f.add(pane);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
} |
Partager