【问题标题】:Vertical orientation of JTabbedPane titles when the tab placement is set to LEFTVertical orientation of JTabbedPane titles when the tab placement is set to LEFT
【发布时间】:2012-01-23 11:30:59
【问题描述】:

如下图所示,Java 文本是水平的。我想做的是获得 JTabbedPane 标题的垂直方向。

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

我希望 Title1Title2 垂直而不是水平

【问题讨论】:

    标签: java swing jtabbedpane


    【解决方案1】:

    您可以使用JLabel 和自定义LabelUI,如this answer 中所述,它给出了我预期的结果:

    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);
          }
      }
      

      【讨论】:

      • 它真的适用于禁用的标签吗? (我的意思是渲染正确的颜色 - JButton 没有。)
      • @Andrew Thompson (-: BNI :-) 你能做到吗,太具体了,你的意思是把 JButton 放到 Tab 上吗,因为我的意思只是可视文本属性,JTabbedPane 是在一年中创建的一二,直到现在没有人更新那些无法从外部访问的受保护/私有方法
      • 以及如何更改标题的字体大小,因为它在垂直方向上太大了?
      • 对不起,我没有看到/抓住你的问题,你能通过在 Html 语法中设置 FontSize 来解决这个问题吗? +1
      • 我试过 &lt;html style="font-size:8px"&gt; 。它没有用,它向我显示了整个 html 文本而不是标题
      【解决方案3】:

      如果您使用WebLaf LoolAndFeel Library 有一个名为WebVerticalLabel 的组件可以有竖排文本。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-15
        • 2017-03-22
        • 1970-01-01
        • 2022-12-27
        • 2020-11-11
        • 2022-12-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多