使 Swing-JTabbedPane-Tab 填充其容器的整个宽度的最简单方法

发布于 2024-10-16 00:32:09 字数 336 浏览 6 评论 0原文

我在 JPanel 中有一个 JTabbedPane (顶部有水平选项卡),我希望此 JTabbedPane 的选项卡适合 JPanel 的整个宽度,这样如果只有一个选项卡,该选项卡将填充整个宽度;当有两个选项卡时,每个选项卡将恰好填充一半,依此类推。

使用自定义 JComponent 作为选项卡的标题并设置 PreferredSize 没有解决方案,因为 JTabbedPanel 似乎在标题组件周围放置了某种边框。

有没有比扩展和/或重写部分/全部 JTabbedPane 更简单的解决方案?

(也许我应该补充一点,当我谈论选项卡时,我指的是这些您可以单击以切换显示的组件的导航组件,而不是组件本身。)

I'm having a JTabbedPane (with horizontal tabs on the top) in a JPanel, and I want the tabs of this JTabbedPane to fit the whole width of the JPanel, so that if there's only one tab, the tab would fill the whole width; when there are two tabs, each tab would fill excactly the half, and so on.

Using a custom JComponent as a the title of the tabs and setting the PreferredSize there isn't a solution since it seems that JTabbedPanel puts some sort of border around the title-components.

Is there any easier solution than extending and/or rewriting part/all of JTabbedPane?

(Maybe I should add that when I'm talking about tabs, I refer to these navigational components you can click on to switch the component shown, and not the components themselves.)

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

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

发布评论

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

评论(1

天冷不及心凉 2024-10-23 00:32:09

测试这段代码:

public class Tabs extends javax.swing.JFrame {


    public Tabs() {
        initComponents();
    }


       private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());


        jTabbedPane1.addTab("tab1", jPanel2);


        jPanel3.setLayout(new java.awt.BorderLayout());


        jTabbedPane1.addTab("tab2", jPanel3);

        jPanel1.add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        add(jPanel1);

        pack();
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tabs().setVisible(true);
            }
        });
    }


    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTabbedPane jTabbedPane1;
}

Test this code :

public class Tabs extends javax.swing.JFrame {


    public Tabs() {
        initComponents();
    }


       private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());


        jTabbedPane1.addTab("tab1", jPanel2);


        jPanel3.setLayout(new java.awt.BorderLayout());


        jTabbedPane1.addTab("tab2", jPanel3);

        jPanel1.add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        add(jPanel1);

        pack();
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tabs().setVisible(true);
            }
        });
    }


    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTabbedPane jTabbedPane1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文