I'm having trouble getting JScrollPanes to actually display scroll bars

非 Y 不嫁゛ 提交于 2019-12-01 21:05:00

Better that you should use layout managers to your advantage and to let these managers do the heavy lifting of calculating component sizes and placements for you. For example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.*;

public class ScrollDemo2 extends JPanel {
   public static final Color[] COLORS = {Color.green, Color.red, Color.blue};
   private static final Dimension PANEL_SIZE = new Dimension(300, 300);
   public ScrollDemo2() {
      JPanel innerPanel = new JPanel(new GridLayout(0, 1));
      for (int i = 0; i < COLORS.length; i++) {
         JPanel colorPanel = new JPanel();
         colorPanel.setPreferredSize(PANEL_SIZE);
         colorPanel.setBackground(COLORS[i]);
         innerPanel.add(colorPanel);
      }
      JScrollPane scrollPane = new JScrollPane(innerPanel);
      scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      scrollPane.getViewport().setPreferredSize(PANEL_SIZE);

      int eb = 10;
      setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
      setLayout(new BorderLayout());
      add(scrollPane, BorderLayout.CENTER);
   }

   private static void createAndShowGui() {
      ScrollDemo2 mainPanel = new ScrollDemo2();

      JFrame frame = new JFrame("ScrollDemo2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

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.

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);

Figures. Soon as I post I realize what's wrong.

Because innerPanel has a null Layout it needs to have its preferred size explicitly declared.

    innerPanel.setPreferredSize(new Dimension(200,600));

fixes it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!