当选项卡位置设置为左侧时,如何垂直显示JTabbedPane标题

4
如下图所示,Java文本是水平的。我想做的是让JTabbedPane标题垂直排列。
在谷歌上搜索时,我发现唯一的方法是添加额外的库。但我想知道是否可以在不添加任何额外库的情况下完成?
我希望标题1标题2是垂直排列而不是水平的。
请看下图:enter image description here
3个回答

11
你可以按照这个答案所述,使用自定义LabelUIJLabel,它会得到我期望的结果:

JTabbedPane上的垂直文本

JTabbedPane tabPane = new JTabbedPane(JTabbedPane.LEFT);

// Add tabs with no text
tabPane.addTab(null, component1);
tabPane.addTab(null, component2);

// Create vertical labels to render tab titles
JLabel labTab1 = new JLabel("Tab #1");
labTab1.setUI(new VerticalLabelUI(false)); // true/false to make it upwards/downwards
tabPane.setTabComponentAt(0, labTab1); // For component1

JLabel labTab2 = new JLabel("Tab #2");
labTab2.setUI(new VerticalLabelUI(false));
tabPane.setTabComponentAt(1, labTab2); // For component2

2

您需要使用HTML语法来更改禁用的选项卡。

tabbedPane.addTab("<html>T<br>i<br>t<br>t<br>l<br>e<br>1</html>", panel1);  

编辑

SSCCE 用于关于HTML文本格式和对齐的问题。

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

/**
 *
 * @author korbel
 */
public class TestTabbedPane extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTabbedPane tabbedPane;

    public TestTabbedPane() {
        tabbedPane = new JTabbedPane();
        tabbedPane.setPreferredSize(new Dimension(300, 200));
        getContentPane().add(tabbedPane);
        JPanel panel = new JPanel();
        tabbedPane.add(panel, "null");
        JTextField one = new JTextField("one");
        tabbedPane.add(one, "one");
        JTextField two = new JTextField("two");
        tabbedPane.add(two, "<html> T<br>i<br>t<br>t<br>l<br>e <br> 1 </html>");
        tabbedPane.setEnabledAt(2, false);
        /*int comp = tabbedPane.getComponentCount();
        for (Component sc : tabbedPane.getComponents()) {
        if (sc instanceof javax.swing.JLabel) {
        JLabel lbl = (JLabel) sc;
        lbl.setForeground(Color.red);
        }
        if (sc instanceof javax.swing.JPanel) {
        JPanel pnl = (JPanel) sc;
        pnl.setName(pnl.getName());
        }
        if (sc instanceof javax.swing.JTextField) {
        JTextField txt = (JTextField) sc;
        txt.setForeground(Color.blue);
        txt.setDisabledTextColor(Color.red);
        }
        }
        UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));
        UIManager.put("TabbedPane.highlight", new Color(255, 0, 0));
        UIManager.put("TabbedPane.lightHighlight", new Color(0, 255, 0));
        UIManager.put("TabbedPane.darkShadow", new Color(0, 255, 0));
        UIManager.put("TabbedPane.shadow",new Color(0, 0, 255));
        UIManager.put("TabbedPane.light" ,  new Color(0, 255, 0));
        UIManager.put("TabbedPane.foreground", new Color(0, 0, 0));
        UIManager.put("JTabbedPane.font", new Font("Dialog", Font.ITALIC, 12));
        UIManager.put("TabbedPane.selected", new Color(255, 0, 0));
        UIManager.put("disable", new Color(255, 0, 0));
        UIManager.put("TabbedPane.selectHighlight" , new Color(0, 0, 0));
        UIManager.put("TabbedPane.background",  new Color(0, 0, 0));
        SwingUtilities.updateComponentTreeUI(tabbedPane);*/
        tabbedPane.setTitleAt(2, "<html><font color="
                + (tabbedPane.isEnabledAt(2) ? "black" : "red") + ">"
                + tabbedPane.getTitleAt(2) + "</font></html>");
        tabbedPane.setTabPlacement(JTabbedPane.LEFT);
    }

    public static void main(String args[]) {
        TestTabbedPane frame = new TestTabbedPane();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

@Andrew Thompson (-: BNI :-),你能不能再具体一点,你是指要将JButton放到Tab中吗?因为我只是指视觉文本属性,JTabbedPane是在一两年前创建的,直到现在没有人更新那些受保护/私有方法,这些方法从外部无法访问。 - mKorbel
抱歉我没看到/听到你的问题,你能通过在HTML语法中设置FontSize来解决吗?+1 - mKorbel
我尝试了 <html style="font-size:8px">,但它没有起作用,它显示了整个 HTML 文本而不是标题。 - Adel Boutros
@Adel Boutros,我会尝试寻找一个代码,关于更改禁用选项卡的选项卡标题颜色,我认为有一个完整的选项卡标题的黑客。 - mKorbel
请问一个与HTML相关的问题,如何将文本对齐到中心并更改字体和大小,因为我无法同时设置,没有这些设置一切都太丑了。 - mKorbel
显示剩余2条评论

1
如果您使用WebLafLoolAndFeel库,那么有一个名为WebVerticalLabel的组件可以显示垂直文本。
JTabbedPane .setTabComponentAt(1, new WebVerticalLabel("Title1"));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接