java JScrollPane 布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2344971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:42:54  来源:igfitidea点击:

JScrollPane layout

javaswingjscrollpane

提问by blue igloo

I want to add table2 into the scrollpanel (called feedback) which already has table1 in there. But only one table shows up. If I use feedback.add(table2), only the 1st table shows (I guess the 2nd table is behind the first one, but I don't know how to make the second one below the first one). if I use feedback.getViewport().add(table2, null), only the 2nd table shows. Do I need to use some layout manager here? i tried to search online about scrollpanel layout but didn't get any solutions. Could anyone tell me what's the problem or give me some related example links? Thanks a lot. The relative code are:

我想将 table2 添加到已经有 table1 的滚动面板(称为反馈)中。但是只有一张桌子出现。如果我使用feedback.add(table2),只显示第一个表格(我猜第二个表格在第一个表格后面,但我不知道如何在第一个表格下面制作第二个表格)。如果我使用feedback.getViewport().add(table2, null),则只显示第二个表。我需要在这里使用一些布局管理器吗?我试图在线搜索有关滚动面板布局的信息,但没有得到任何解决方案。谁能告诉我有什么问题或给我一些相关的示例链接?非常感谢。相关代码为:

        content = getContentPane();
        content.setLayout(new FlowLayout());
        scrollPane = new JScrollPane(tree);
        feedback = new JScrollPane(table1);
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scrollPane, feedback);
        content.add(splitPane);
            .
            .
            .
            .
        feedback.add(table2);
        //i add this, but still doesn't work 
        content.add(table2);

回答by camickr

Only a single component can be added to the "viewport" of a JScrollPane. This is done by using:

只能将单个组件添加到 JScrollPane 的“视口”。这是通过使用:

JScrollPane scrollPane = new JScrollPane( table );

or

或者

scrollPane.setViewportView( table );

If you want multiple component to appear in a scrollPanethen add the component to a panel first and add the panel to the viewport.

如果要在一个中显示多个组件,请先scrollPane将组件添加到面板,然后将面板添加到视口。

Read the JScrollPane API for more information and follow the link to the Swing tutorial as well for examples.

阅读 JScrollPane API 以获取更多信息,并访问 Swing 教程的链接以及示例。