java 将 JScrollPane 添加到 JFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16505239/
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-31 23:09:37  来源:igfitidea点击:

Add a JScrollPane to a JFrame

javaswingjframejscrollpane

提问by user2351151

I have a question in relation to adding components to a Java frame.

我有一个关于向 Java 框架添加组件的问题。

I have a JPanel with two buttons and also a JScrollPane that has a JTable added to it. I am wanting to add both these to a JFrame.

我有一个带有两个按钮的 JPanel,还有一个添加了 JTable 的 JScrollPane。我想将这两个添加到 JFrame。

I can add either the JPanel to the JFrame, or the JScrollPane to the JFrame and they display correctly. I am having trouble adding them both to the JFrame and displaying them both.

我可以将 JPanel 添加到 JFrame,或者将 JScrollPane 添加到 JFrame,它们都可以正确显示。我无法将它们都添加到 JFrame 并同时显示它们。

Is there something related to JFrames that do not allow this? Any help will be appreciated.

是否有与 JFrames 相关的内容不允许这样做?任何帮助将不胜感激。

EDIT

编辑

The problem is not with the layout (at least I do not think that it is), the problem is that the ScrollPane is not displaying correctly. Here is an image to show what I mean:

问题不在于布局(至少我认为不是),问题在于 ScrollPane 显示不正确。这是一张图片来说明我的意思:

http://canning.co.nz/Java/ScrollPane.png

http://canning.co.nz/Java/ScrollPane.png

Here is the code:

这是代码:

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    guiPanel.add(scrollPane, gbc);
    guiPanel.add(buttons, gbc);        

    guiFrame.add(guiPanel, BorderLayout.CENTER);
    guiFrame.setVisible(true);

回答by Himanshu Upadhyay

By Default, JFrame has Borderlayout. BorderLayout have five flags. When you will not specify any flag, It will add you component to frame with center flag. Center flag give all space to component which is added with center flag, if no other flag is specified with component. for more information on border layout, visit following link: http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

默认情况下,JFrame 具有 Borderlayout。BorderLayout 有五个标志。当您不指定任何标志时,它会将您的组件添加到带有中心标志的框架中。如果没有为组件指定其他标志,则中心标志为添加了中心标志的组件提供所有空间。有关边框布局的更多信息,请访问以下链接:http: //docs.oracle.com/javase/tutorial/uiswing/layout/border.html

You can add both by using following statement:

您可以使用以下语句添加两者:

JFrame frame = new JFrame();

frame.add(new JPanel(), BorderLayout.NORTH);

frame.add(new JScrollpane(), BorderLayout.CENTER);

回答by Himanshu Upadhyay

GridBagLayout is not good for this situation. With lesser code you can get desired Code.

GridBagLayout 不适合这种情况。使用较少的代码,您可以获得所需的代码。

JPanel pnlButton = new JPanel(new FlowLayout(FlowLayout.CENTER));
pnlButton.add(button1);
pnlButton.add(button2);

JFrame frame = new JFrame();
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(pnlButton, BorderLayout.PAGE_END);

If you want to go only with Gridbaglayout. Then you will have to set weightX, weightY, gridwidth and other properties like that

如果您只想使用 Gridbaglayout。然后你必须设置 weightX、weightY、gridwidth 和其他类似的属性

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx=1.0;
gbc.weighy=1.0;
gbc.fill = GridBagConstraints.BOTH;
guiPanel.add(scrollPane, gbc);
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE
guiPanel.add(buttons, gbc);        
guiFrame.add(guiPanel, BorderLayout.CENTER);
guiFrame.setVisible(true);

Please try and let me know, if it does not work, I will send complete code with GridBaglayout.

请尝试让我知道,如果它不起作用,我将使用 GridBaglayout 发送完整的代码。