我无法让JScrollPanes实际显示滚动条 [英] I'm having trouble getting JScrollPanes to actually display scroll bars

查看:98
本文介绍了我无法让JScrollPanes实际显示滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一些分散在互联网上的例子涉及获取图像或文本框以显示滚动条,但它们都涉及一个基本上在滚动窗格中显示其全部内容的程序。我需要做的是将JPanel粘贴到某个地方,将一堆文本,图标等堆放到该面板中,直到它对于我所拥有的空间来说太大,然后滚动它。

I've found some examples scattered around the internet involving getting an image or a textbox to display scroll bars, but they all involve a program that basically displays its entire contents in a scroll pane. What I need to get it to do is stick a JPanel somewhere, pile a bunch of text, icons, etc into that panel until its too big for the space I've got for it, and then scroll across it.

我可能需要能够对该面板进行空布局,因为我可能需要尽快在其中手动定位内容或之后。

I'm probably going to need to be able to null-layout that panel, too, because I'm probably going to have to manually position things within it sooner or later.

所以我尝试制作一个非常简单的程序,将三个彩色块放在一个足够大的空间中,并显示一个滚动条,让你向下滚动看第三个。它不会添加滚动条。

So I tried to make a pretty simply program that would put three colored blocks in a space big enough for two, and display a scrollbar that would let you scroll down to see the third one. It doesn't add the scroll bar.

导入有点混乱和多余,因为这是从几个不同的文件拼凑而成,但它们是没有产生任何错误。

The imports are a bit of a mess and redundant, because this is cobbled together from a couple different files, but they're not generating any errors.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

public class ScrollDemo extends JFrame {

  JScrollPane scrollpane;

  public ScrollDemo() {
    super("JScrollPane Demonstration");
    setSize(800, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    init();
    setVisible(true);
  }

  public void init() {
    setLayout(null);
    JPanel innerPanel = new JPanel();
    JPanel outerPanel = new JPanel();
    getContentPane().setBounds(0,0,800,600);
    outerPanel.setBounds(400,0,400,400);
    innerPanel.setBounds(0,0,600,600);
    innerPanel.setLayout(null);

    JPanel greenPanel = new JPanel();
    JPanel yellowPanel = new JPanel();
    JPanel bluePanel = new JPanel();

    greenPanel.setBounds(0,0,200,200);
    yellowPanel.setBounds(0,200,200,200);
    bluePanel.setBounds(0,400,200,200);
    greenPanel.setBackground(Color.GREEN);
    yellowPanel.setBackground(Color.YELLOW);
    bluePanel.setBackground(Color.BLUE);

    innerPanel.add(greenPanel);
    innerPanel.add(yellowPanel);
    innerPanel.add(bluePanel);
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(innerPanel);
    scrollPane.setBounds(200,0,200,400);
    add(scrollPane);


  }

  public static void main(String args[]) {
    new ScrollDemo();
  }
}


推荐答案

In儿童的摆动,尺寸和位置是LayoutManager的专属工作。选择一个支持您的要求,作为最后的手段实现高度专业化。子项通过报告大小提示进行协作,因此实现任何自定义组件以在getXXSize方法中返回合理的内容。

In Swing, sizing and positioning of children is the exclusive job of a LayoutManager. Choose one that supports your requirements, as a last resort implement a highly specialized one. Children collaborate by reporting sizing hints, so implement any custom components to return something reasonable in the getXXSize methods.

当你感到不可抗拒的手动干扰的冲动时,至少让经理尽可能地做。在您的上下文中,可能会接管定位但让管理器处理大小调整,特别是计算父级的大小调整提示。这是使用 Rob's DragLayout 的代码段:

When you feel an irresistable urge to manually interfere, at least let the manager do as much as possible. In your context that might be to take over the positioning but let the manager handle the sizing, particularly calculating the sizing hints of the parent. Here's a code snippet using Rob's DragLayout:

DragLayout layout = new DragLayout();
JComponent field = new JPanel(layout);
JComponent player = new JLabel("I'm moving around");
field.add(player);
player.setLocation(200, 200);
frame.add(new JScrollPane(field));
frame.pack();
frame.setVisible(true);

这篇关于我无法让JScrollPanes实际显示滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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