修改JTabbedPan的滚动按钮

问题描述 投票:1回答:2

我正在使用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;
        }

    }
java swing jtabbedpane
2个回答
1
投票

请参阅我的帖子进行澄清

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9QUnNoNC5qcGcifQ==” alt =“在此处输入图像描述”>

[based on code,请注意需要添加tb.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);


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年历史,可能对您来说不再是问题,但其他人可以使用。

© www.soinside.com 2019 - 2024. All rights reserved.