将JScrollPane添加到JFrame [英] Add a JScrollPane to a JFrame

查看:146
本文介绍了将JScrollPane添加到JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于将组件添加到Java框架中,我有一个问题.

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

我有一个带有两个按钮的JPanel,还有一个添加了JTable的JScrollPane.我想将它们都添加到JFrame中.

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添加到JFrame中,也可以将JScrollPane添加到JFrame中,并且它们可以正确显示.我在将它们都添加到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.

是否有与JFrames相关的东西不允许这样做?任何帮助将不胜感激.

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

编辑

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

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:

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

这是代码:

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

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

推荐答案

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

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

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

You can add both by using following statement:

JFrame frame = new JFrame();

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

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

这篇关于将JScrollPane添加到JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆