3

当我想在我的 jpanel 中添加选项卡时,我遇到了一个简单的问题。选项卡的对齐方式是水平而不是垂直,看起来像废话 =/。

它看起来像这样:

在此处输入图像描述

如果我放弃面板并将 tabbedPane 直接添加到框架中,一切正常。如果您取消注释三行代码并删除,getContentPane().add(jtp);您可以重现我的问题。

工作代码:

public class TabbedPane extends JFrame
{
    public TabbedPane()
    {
        setTitle("Tabbed Pane");
        setSize(300, 300); // set size so the user can "see" it
        JTabbedPane jtp = new JTabbedPane();

        // JPanel panel = new JPanel();//uncomment all three lines
        // panel.add(jtp);
        // getContentPane().add(panel);

        getContentPane().add(jtp);//remove me

        JPanel jp1 = new JPanel();// This will create the first tab
        JPanel jp2 = new JPanel();// This will create the second tab

        JLabel label1 = new JLabel();
        label1.setText("This is Tab 1");
        jp1.add(label1);

        jtp.addTab("Tab1", jp1);
        jtp.addTab("Tab2", jp2);

        JButton test = new JButton("Press");
        jp2.add(test);

        setVisible(true); // otherwise you won't "see" it
    }

    public static void main(String[] args)
    {
        TabbedPane tab = new TabbedPane();
    }

}

非常感谢!

4

5 回答 5

4

如果我放弃面板并将其tabbedPane直接添加到框架中,一切正常。

的默认布局JPanelFlowLayout,“让每个组件呈现其自然(首选)大小”。的默认布局JFrameBorderLayoutCENTER忽略首选大小。在任何一种情况下,调用都会setSize()阻止布局最初运行。重新调整框架大小以查看效果。取而代之的是pack(),它“导致它Window的大小适合其子组件的首选大小和布局”。

setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true); // otherwise you won't "see" it
于 2013-01-28T00:34:45.673 回答
3

从@trashgod 的建议开始,我会在该代码中更改许多内容。OTOH 这是将选项卡式窗格拉伸到父容器的宽度/高度所需的最小更改。

// give the panel a layout that will stretch components to available space
JPanel panel = new JPanel(new GridLayout());//uncomment all three lines
panel.add(jtp);
getContentPane().add(panel);

//getContentPane().add(jtp);//remove me

有关更多详细信息,请参阅此答案

于 2013-01-28T05:31:57.780 回答
1

试试GridbagLayout。一旦你掌握了它,你就可以使用这种布局设计任何类型的 UI。

于 2013-01-28T16:31:38.543 回答
1

那么首先你可以试试这个:

    JPanel panel = new JPanel();//uncomment all three lines
    panel.setLayout(new GridLayout());
    JPanel jp1 = new JPanel();// This will create the first tab
    JPanel jp2 = new JPanel();// This will create the second tab

    JLabel label1 = new JLabel();
    label1.setText("This is Tab 1");
    jp1.add(label1);

    jtp.addTab("Tab1", jp1);
    jtp.addTab("Tab2", jp2);

    JButton test = new JButton("Press");
    jp2.add(test);
    getContentPane().add(jtp);

主要是:

    TabbedPane tab = new TabbedPane();
    tab.pack();
    tab.setVisible(true);

我可以建议使用MigLayout来设置布局,它会让你的生活更轻松。希望能帮助到你。

于 2013-01-27T23:01:47.250 回答
0

我同意prasanth关于使用GridBagLayout

我曾经遇到过这个问题,我通过添加JTabbedPane到面板中解决了这个问题GridBagLayout,请确保根据您的对象中的要求添加JTabbedPane使用ipadxipadyGridBagConstraints

JPanel myPanel=new JPanel();
myPanel.setLayout(new GridBagLayout());
JTabbedPane jTP=new JTabbedPane();
jTP.add("Tab1",new JPanel());//substitute your component instead of "new JPanel"
GridBagConstraints myConstraints=new GridBagConstraints();
myConstraints.ipadx=400;//streches the component being added along x axis - 200 px on both sides
myConstraints.ipady=600;//streches the component being added along y axis - 200 px on both sides
myPanel.add(jTP,myConstraints);

您可以根据最适合您的需要调整这两个属性

于 2013-09-03T10:21:01.053 回答