调整 JScrollPane 和 JTabbedPane 大小时出现问题

发布于 2024-08-23 10:56:12 字数 2012 浏览 6 评论 0原文

我在调整 java 应用程序中某些组件的大小时遇到​​问题。当我调整主 JFrame 的大小时,它们会保持以前的大小,而不是填充周围的组件。

例如,我的程序中有一个 JTabbedPane,位于另一个 JTabbedPane(位于主 JFrame 内)的选项卡中。我通过各种 .setPreferredSize() 将添加到内部 JTabbedPane 的选项卡填充整个周围空间。然后,当我通过拖动窗口的一角来调整 JFrame 的大小时,外部 JTabbedPane 的选项卡大小调整得很好,但位于其中的 JTabbedPane 保持相同的旧大小。

当我使用 JScrollPane 测试它时,会发生同样的事情:外部 JTabbedPane 通过 JScrollPane 调整大小保持相同的大小。

下面是创建选项卡式面板的代码,以防您看到我错过的明显内容。这是 createEndGamePane() 中的 tabPane/jsp 对象在 JFrame 调整大小时不会调整大小。 mainTabPane 按其应有的方式调整大小。

class MyFrame extends JFrame {
   private JTabbedPane mainTabPane;
   private JPanel endGameInfo;
   //....

   private void createTabbedCenterPane(){
       this.mainTabPane = new JTabbedPane();

       this.endGameInfo = new JPanel();

       this.createEndGamePane();

       this.mainTabPane.addTab("Turneringschema", null, this.scheduleHolder, "Spelschemat för turneringen");
       this.mainTabPane.addTab("Grupper & Pooler", null, this.poolInfo, "Information om grupper, lag och pooler");
       this.mainTabPane.addTab("Slutspelsträd", null, this.endGameInfo, "Information om slutspelen");

       this.add(this.mainTabPane, BorderLayout.CENTER);
    }

    private void createEndGamePane(){
        JTabbedPane tabPane = new JTabbedPane();
        tabPane.setPreferredSize(this.endGameInfo.getSize());

        for (int i = 0; i < this.tournament.data.getGroups().size(); i++) {
            EndGame e = this.tournament.data.getEndGames().get(i);;

            //Create the gameTree
            EndGameTreeCanvas gameTree = new EndGameTreeCanvas(e, this.endGameInfo.getSize());

            //Create the ScrollPane
            JScrollPane jsp = new JScrollPane(gameTree);
            jsp.setPreferredSize(tabPane.getSize());
            jsp.setBorder(BorderFactory.createEmptyBorder());


            //Add to the endGameIndo panel
            tabPane.addTab(this.tournament.data.getGroups().get(i).getName(), jsp);
        }

        this.endGameInfo.add(tabPane);
    }

}

I have a problem with resizing some of my components in my java application. When I resize my main JFrame they keep their previous size instead of filling out the surrounding component.

For example I have in my program a JTabbedPane within a tab of another JTabbedPane (which is located within the main JFrame). I make tabs added to the inner JTabbedPane fill the whole surrounding space via various .setPreferredSize(). And then when I resize the JFrame by dragging the corner of the window, the tabs out the outer JTabbedPane resizes just fine but the JTabbedPane located within it stays the same old size.

The same thing with happens when I test it with a JScrollPane: the outer JTabbedPane resizes by the JScrollPane stays the same size.

Here below is the code for creating the tabbed panels in case you can see something obvious I missed. It's the tabPane/jsp objects in createEndGamePane() than wont resize when the JFrame is resized. mainTabPane resizes as it should.

class MyFrame extends JFrame {
   private JTabbedPane mainTabPane;
   private JPanel endGameInfo;
   //....

   private void createTabbedCenterPane(){
       this.mainTabPane = new JTabbedPane();

       this.endGameInfo = new JPanel();

       this.createEndGamePane();

       this.mainTabPane.addTab("Turneringschema", null, this.scheduleHolder, "Spelschemat för turneringen");
       this.mainTabPane.addTab("Grupper & Pooler", null, this.poolInfo, "Information om grupper, lag och pooler");
       this.mainTabPane.addTab("Slutspelsträd", null, this.endGameInfo, "Information om slutspelen");

       this.add(this.mainTabPane, BorderLayout.CENTER);
    }

    private void createEndGamePane(){
        JTabbedPane tabPane = new JTabbedPane();
        tabPane.setPreferredSize(this.endGameInfo.getSize());

        for (int i = 0; i < this.tournament.data.getGroups().size(); i++) {
            EndGame e = this.tournament.data.getEndGames().get(i);;

            //Create the gameTree
            EndGameTreeCanvas gameTree = new EndGameTreeCanvas(e, this.endGameInfo.getSize());

            //Create the ScrollPane
            JScrollPane jsp = new JScrollPane(gameTree);
            jsp.setPreferredSize(tabPane.getSize());
            jsp.setBorder(BorderFactory.createEmptyBorder());


            //Add to the endGameIndo panel
            tabPane.addTab(this.tournament.data.getGroups().get(i).getName(), jsp);
        }

        this.endGameInfo.add(tabPane);
    }

}

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

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

发布评论

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

评论(3

亽野灬性zι浪 2024-08-30 10:56:12

如果您希望组件动态调整大小,则应避免告诉 Swing 它们的大小是多少。相反,这样怎么样:

JTabbedPane tabs = new JTabbedPane();
JScrollPane inner1 = new JScrollPane(myOtherComponent);
tabs.add("Scroll", inner1);
JTabbedPane inner2 = new JTabbedPane();
tabs.add("Tabs", inner2);

仅此而已。没有setPreferredSize()

If you want the components to resize dynamically, you should avoid telling Swing what their size is. Instead, how about this:

JTabbedPane tabs = new JTabbedPane();
JScrollPane inner1 = new JScrollPane(myOtherComponent);
tabs.add("Scroll", inner1);
JTabbedPane inner2 = new JTabbedPane();
tabs.add("Tabs", inner2);

And that's all. No setPreferredSize().

梦回梦里 2024-08-30 10:56:12

您应该阅读 swing 布局教程:
http://java.sun.com/docs/books/教程/uiswing/layout/index.html

我更喜欢的布局管理器是gridbaglayout,它允许最大的灵活性
( http://java.sun.com/docs/books /tutorial/uiswing/layout/gridbag.html )。

特别是,如果您希望组件填充其周围的所有可用空间,则不应使用 BorderLayout.CENTER。

一个 gridbaglayout 管理器的例子:

this.add(this.mainTabPane,
  new GridBagConstraints(
    0,0, // position
    1,1, // size
    1.0,1.0, // fill ratio
    GridBagConstraints.CENTER, GridBagConstraints.BOTH, // position inside the cell
    new Insets(2,2,2,2),0,0)); // margins

You should read the swing layout tutorial :
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

The layout manager i prefer is the gridbaglayout that allow maximum flexibility
( http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html ).

In particular, you should not use the BorderLayout.CENTER if you wish the component to fill all the available space around him.

an example with a gridbaglayout manager :

this.add(this.mainTabPane,
  new GridBagConstraints(
    0,0, // position
    1,1, // size
    1.0,1.0, // fill ratio
    GridBagConstraints.CENTER, GridBagConstraints.BOTH, // position inside the cell
    new Insets(2,2,2,2),0,0)); // margins
咽泪装欢 2024-08-30 10:56:12

正如其他人所说,您需要使用 布局管理器 处理 GUI 的布局要求。

不过,我建议您的主要布局管理器是基于表格的布局,例如 MatrixLayoutMigLayoutTableLayout,或任何其他免费的。

使用基于表格的布局将简化 GUI 编码大约一个数量级,因为它将大大减少所需的布局嵌套量。我发现 90% 的 GUI 是通过一个布局管理器完成的,其余 10% 利用 Java 提供的更简单的布局管理器。

我从来没有理由使用 GridBagLayout 并且我绝对推荐它 - 它不必要地复杂并且使持续维护变得更加困难。

As others have said, you need to be using layout managers to handle your GUI's layout requirements.

However I recommend that your primary layout manager be a table-based layout like MatrixLayout, MigLayout, TableLayout, or any of a number of other free ones.

Using a table-based layout will simplify your GUI coding by about an order of magnitude, because it will vastly reduce the amount of layout nesting required. I find that 90% of my GUI is done with one layout manager, and the remaining 10% draws on the simpler layout managers provided with Java.

I have never had cause to use GridBagLayout and I absolute do not recommend it - it's unnecessarily complicated and make ongoing maintenance harder.

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