在没有布局管理器的情况下将JScrollPane添加到JPanel [英] Adding JScrollPane to a JPanel without a Layout Manager

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

问题描述

在开始之前,我知道不使用布局管理器是一个坏主意,通常我会使用布局管理器,但是,我也会根据窗口的大小自动调整所有组件的大小和重新定位。此外,我正在开发的程序仅用于在整个生命周期内在一台机器上运行。请不要因为缺少布局管理器而向我投票,我发现这是我需要的特定程序。

Before I start, I'm aware that its a bad idea to not use a Layout Manager and usually I do use one, however, I also have all my components automatically re-size and relocate based on the size of the window. In addition the program I'm working on is only intended to run on 1 machine throughout its entire lifetime. Please don't downvote me just because of lack of layout manager, I found it to be what I need for this particular program.

对于我的问题,我发现了一个类似的在stackoverflow上发布,但从未实现过解决方案。
我在我的 JPanel 中添加了动态数量的 JLabels ,我注意到当没有使用布局管理器,滚动条不起作用。

To my issue, I found a similar post on stackoverflow but a solution was never achieved. I'm adding a dynamic amount of JLabels to my JPanel, I've noticed that when not using a layout manager, the scroller doesn't work.

这是我的初始化代码的简化版本。

This is a simplified version of my initialization code.

JPanel mypanel = new JPanel();
mypanel.setLayout(null);
mypanel.setSize(800,450);
mypanel.setForeground(Color.WHITE);
mypanel.setBackground(Color.BLACK);
scrollablePanel = new JScrollPane(mypanel);
scrollablePanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollablePanel.setPreferredSize(new Dimension(800,300));
scrollablePanel.setSize(800,300);
scrollablePanel.setLocation(250,156);
myContainer.add(scrollablePanel);

其中 myContainer 将是容器(也是没有布局管理员)。稍后将 JLabels 添加到 JPanel ,其中包含:

where myContainer would be the container (also without a layout manager). The JLabels are added later on to the JPanel with:

在此处输入代码 mypanel.add(label1);

enter code heremypanel.add(label1);

为标签设置了一些基本设置后as setForeground setBackground

after some basic settings are set for the labels such as setForeground and setBackground.

不幸的是,在运行时,滚动条看起来好像不需要(见图)

Unfortunately when run, the scrollbar appears as if its not required (see image)

http://i.imgur.com/zp0QKGG.png

图片中显示的表格文字由多个<$ c $组成C>的JLabel 。
如果不使用布局管理器就无法解决问题我将切换到BoxLayout,我只是希望有一个解决方案。

The table text seen in the image is made up of multiple JLabels. If it's not possible to resolve the issue without using a Layout Manager I will switch to BoxLayout, I was just hoping there would be a solution.

推荐答案

问题是 JScrollPanel 需要视图组件的首选大小来确定视图何时超出滚动窗格的可视区域。

The problem is JScrollPanel needs the preferred size of the view component to determine when the view exceeds the scroll pane's viewable area.

组件的首选大小通常通过布局管理器确定。虽然您可以使用 setPreferredSize ,但通常不鼓励这样做,您只会遇到同样的问题,因为内容超出了您决定设置的值。

The preferred size of a component is normally determined via the layout manager. While you can use setPreferredSize, it is typically discouraged and you will simply run into the same problem as the content exceeds what ever value you decide to set.

更好的解决方案是使用 LayoutManager 或复合布局(在多个容器上使用多个布局管理器)来实现所需的结果。 ..或写自己的......

A better solution would be to use a LayoutManager or compound layout (using multiple layout managers over multiple containers) to achieve the result you desired...or write your own...

这篇关于在没有布局管理器的情况下将JScrollPane添加到JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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