当选项卡位置设置为 LEFT 时 JTabbedPane 标题的垂直方向

发布于 2024-12-28 16:49:37 字数 275 浏览 2 评论 0原文

从下图中可以看到,Java 文本是水平的。我想做的是获得 JTabbedPane 标题的垂直方向。

在谷歌搜索时,我发现唯一的方法是添加额外的库。但我想知道这是否可以在没有任何额外库的情况下完成?

我希望 Title1Title2 是垂直方向而不是水平方向

在此处输入图像描述

As you can see from the image below, the Java text is horizontal. What I would like to do is get a vertical orientation of the JTabbedPane Titles.

While googling, I found that the only way is to add extra library. But I was wondering if this can be done without any extra library?

I would like for Title1 and Title2 to be vertically oriented and not horizontally

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鱼窥荷 2025-01-04 16:49:37

您可以将 JLabel 与自定义 LabelUI 结合使用,如此答案,它给出了我预期的结果:

Vertical text on 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

You can use a JLabel with a custom LabelUI as described in this answer, it gives the result I expected:

Vertical text on 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
燕归巢 2025-01-04 16:49:37

您必须使用 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);
    }
}

you have to use Html syntax, for any changes to the disabled Tab too

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

EDIT

SSCCE for question about Html text formatting and alignment

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);
    }
}
蓝咒 2025-01-04 16:49:37

如果您使用 WebLaf LoolAndFeel 库,有一个名为 WebVerticalLabel 的组件可以有垂直文本。

JTabbedPane .setTabComponentAt(1, new WebVerticalLabel("Title1"));

If you use WebLaf LoolAndFeel Library There is a component called WebVerticalLabel that can have vertical text.

JTabbedPane .setTabComponentAt(1, new WebVerticalLabel("Title1"));
枯寂 2025-01-04 16:49:37

对于使用 Swing 'FlatLaf' LookAndFeel 的用户,从版本 3.3 开始,您可以使用属性 TabbedPane.tabRotation 设置为 none,即正常的水平方式,auto,按照自然方式,leftright自动将文本旋转到垂直方向,如果您愿意的话你有一个首选旋转来应用。请参阅此处的文档

例如使用 auto 应用它:

UIManager.put("TabbedPane.tabRotation", "auto");
SwingUtilities.updateComponentTreeUI(jTabbedPane1);

For those who use the Swing 'FlatLaf' LookAndFeel, since version 3.3, you can use the property TabbedPane.tabRotation set to none that is the normal horizontal way, auto that puts automatically rotated the text to the vertical following a natural way, left or right that changes this as you wish if you have a preferred rotation to apply. See documentation here.

Apply it for example with auto:

UIManager.put("TabbedPane.tabRotation", "auto");
SwingUtilities.updateComponentTreeUI(jTabbedPane1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文