【问题标题】:java swing TabbedPane position tabheaders bottom right to leftjava swing JTabbedPane位置选项卡标题从右下到左
【发布时间】:2012-06-06 06:12:36
【问题描述】:

我想知道是否有可能将选项卡式窗格放置在右下角, 我想将容器放在彼此之上,一个标签标签在右上角到左(没问题/默认),另一个标签标签在底部(到目前为止很好),从右到左开始。 我想象的设计是:

| tab display                                 |
| tab display                                 |
| tab display                                 |
-----------------------------------------------
_________________________  | tab top | another|
| Bottom Tab| Second Tab|______________________
|                                              |

好吧,我的 ascy 绘图能力有限,但我希望你能明白。 有什么办法,我可以将布局管理器添加到选项卡式窗格以布局选项卡标签?我在哪里可以找到这样的例子? Goodling 建议仅将菜单栏与 CardLayout 结合用于选项卡式窗格。但是我不知道如何绘制一个菜单栏,使它看起来像标签。 (我刚刚查看了自定义绘图的摇摆轨迹,但它只是继续讨论一般可能性,除了如果我这样做,我会失去样式功能。我现在唯一能想到的就是使用没有内容的选项卡式窗格和让它通过事件监听器控制另一个带有卡片布局的容器。

有什么建议吗?

P.S.:我必须用 java 1.6 实现解决方案

【问题讨论】:

    标签: java swing jtabbedpane


    【解决方案1】:

    在这种情况下,您可以为 JTabbedPane 设置 ComponentOrientation

    import java.awt.ComponentOrientation;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    
    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.add(two, "<html> Tittle  1 </html>");
            tabbedPane.setEnabledAt(2, false);
            tabbedPane.setTitleAt(2, "<html><font color="
                    + (tabbedPane.isEnabledAt(2) ? "black" : "red") + ">"
                    + tabbedPane.getTitleAt(2) + "</font></html>");
            tabbedPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
        }
    
        public static void main(String args[]) {
            TestTabbedPane frame = new TestTabbedPane();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    }
    

    【讨论】:

    • 似乎可以解决问题,但为什么是垂直的Title 1(或者更确切地说是Tittle 1 - 我更喜欢)?
    • 请 psssttt,我从另一个线程中回收了示例(如何更改禁用选项卡标题的颜色)
    • 回收?很高兴看到你帮助地球。 ;)
    • 我想称你为 java 之神,但这可能会引起对垃圾神的敌意;)。感谢您的回答和示例,为我节省了很多麻烦(想象一下我的解决方法想法(一个带有 cardboxlayout 的额外容器,以及更多东西......)。还有一个“额外的问题”:我可以制作正确的标签标签(null)吗?边缘与容器的右边缘完美对齐?
    • One more 'bonus question': can I make the right tablabels (null) right edge align perfectly to the right edge of the container? 不知道如何正确地解决这个问题,并且在屏幕上输出很好,maybe you have to bothering with use proper Look and Feel, some of Swing & Java Gods override this awfull container,这不是一件容易的事,因为大多数方法都是私有的,然后无法从为 @ 实施的 Swing 方法中访问987654326@
    【解决方案2】:

    使用 setTabPlacement(JTabbedPane.TOP) 方法检查 可用的有 TOP 、 LEFT 、 BOTTOM 、 RIGHT

    http://www.exampledepot.com/egs/javax.swing/tabbed_TpTabLoc.html

    【讨论】:

    • 正如我写的“...底部的标签标签(到目前为止这么好)..”没问题,问题是在我将它们放置在容器
    • 嗯,我认为是布局的问题,尝试使用 Box 或 Grid 来解决您的问题
    猜你喜欢
    • 1970-01-01
    • 2010-12-19
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多