滚动条没有出现在JScrollPane中 [英] Scrollbars not appearing in JScrollPane

查看:163
本文介绍了滚动条没有出现在JScrollPane中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个窗口,其中两个按钮的高度很高,一侧有一个滚动条。问题是没有滚动条出现。这是我的代码

I want to make a window with two buttons with great heights and a scrollbar on a side. The problem is that no scrollbar appears. Here is my code

public class Window {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //namestanje teme
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedLookAndFeelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JFrame frame = new JFrame("frame");
//  frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(null);

    JButton but1 = new JButton();
    JButton but2 = new JButton();

    panel.add(but1);
    panel.add(but2);

    but1.setSize(50, 505);
    but2.setSize(50, 505);

    but1.setLocation(0, 0);
    but2.setLocation(400, 400);

    but1.setText("1");
    but2.setText("2");


    JScrollPane scroll = new JScrollPane(panel);

    frame.add(scroll);
    frame.setVisible(true);         
}
}

注意:首先,按钮的宽度较大(通过用类似11111111111111111111111111111之类的命名来做到这一点,并会出现一个滚动条。然后我想要很高的高度,不得不在面板中放置null。现在没有滚动条出现。

Note: At first, the buttons had large widths (did that by naming them with something like "11111111111111111111111111111") and a scrollbar would appear. Then I wanted large heights and had to put null in panel. Now no scrollbar appears.

推荐答案

当添加到scollpane的组件的首选大小大于其大小时,会出现滚动条scrollpane。

Scrollbars appear when the preferred size of the component added to the scollpane is greater than the size of the scrollpane.

布局管理器的工作是确定面板的首选大小。布局管理器的工作也是确定添加到面板的组件的大小和位置。

It is the job of the layout manager to determine the preferred size of the panel. It is also the job of the layout manager to determine the size and location of the components added to the panel.

摆脱空布局并使用布局管理器,并在需要时自动显示滚动条。

Get rid of the null layout and use a layout manager and scrollbars will appear when required automatically.

如果您希望组件的显示方式与垂直视图不同,然后您需要使用不同的布局管理器。也许你可以使用带有verticxal布局的 BoxLayout 。您可以使用:

If you want components to be displayed differently from a vertical point of view, then you need to use a different layout manager. Maybe you can use a BoxLayout with a verticxal layout. You can use:

panel.add( Box.createVerticalStrut(400) );

在两个组件之间添加垂直空间。

to add vertical space between the two components.

这篇关于滚动条没有出现在JScrollPane中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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