我正在使用Swing开发Java上的UI,我想更改JTabbedPane的滚动按钮,所以我使用了我从扩展MetalTabbedPaneUI创建的新UI(MyTabbedPaneUI)。
但是当我创建JTabbedPanel时,会出现2个选项卡,我不想要它们。如果我删除它们,我的滚动条就会消失。
代码:
public class MyTabbedPaneUI extends MetalTabbedPaneUI{
private Icon southIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("south.png"));
private Icon northIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("north.png"));
private Icon eastIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("flecheVerte-gauche-20px.png"));
private Icon westIcon = new ImageIcon(MyTabbedPaneUI.class.getResource("flecheVerte-droite-20px.png"));
public static ComponentUI createUI( JComponent x ) {
return new MyTabbedPaneUI();
}
@Override
protected JButton createScrollButton(int direction) {
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(eastIcon.getIconWidth(), eastIcon.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;
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
@Override
protected JButton createScrollButton(int direction) {
if (direction != SOUTH && direction != NORTH && direction != EAST
&& direction != WEST) {
throw new IllegalArgumentException("Direction must be one of: "
+ "SOUTH, NORTH, EAST or WEST");
}
return new ArrowButton(direction);
}
//
public final class ArrowButton extends JButton implements UIResource, SwingConstants {
protected int direction;
private final Image left, disabledleft, pressedleft;
private final Image right, disabledright, pressedright;
public ArrowButton(int direction) {
super();
setDirection(direction);
left = new ImageIcon(getClass().getResource("/resources/arrowicons/left.png")).getImage();
disabledleft = new ImageIcon(getClass().getResource("/resources/arrowicons/disabledleft.png")).getImage();
pressedleft = new ImageIcon(getClass().getResource("/resources/arrowicons/leftpressed.png")).getImage();
right = new ImageIcon(getClass().getResource("/resources/arrowicons/right.png")).getImage();
disabledright = new ImageIcon(getClass().getResource("/resources/arrowicons/disabledright.png")).getImage();
pressedright = new ImageIcon(getClass().getResource("/resources/arrowicons/rightpressed.png")).getImage();
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
@Override
public void paint(Graphics g) {
boolean isPressed, isEnabled;
int w, h, size;
w = getSize().width;
h = getSize().height;
isPressed = getModel().isPressed();
isEnabled = isEnabled();
Image image = null;
if(direction == EAST){
if(isEnabled && isPressed){
image = pressedright;
}else if(!isEnabled){
image = disabledright;
}else{
image = right;
}
}else{
if(isEnabled && isPressed){
image = pressedleft;
}else if(!isEnabled){
image = disabledleft;
}else{
image = left;
}
}
g.drawImage(image, 0, 0, null);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(32, 32);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(5, 5);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
}
//这是我的解决方案,非常简单实用。您可以轻松使用。实际上,该职位已有8年历史,可能对您来说不再是问题,但其他人可以使用。