简体   繁体   English

我无法让JScrollPanes实际显示滚动条

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

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. 我需要做的是将JPanel粘贴在某个位置,在该面板中堆放一堆文本,图标等,直到对于我需要的空间来说太大为止,然后在其上滚动。

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

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. 在Swing中,子项的大小和定位是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. 子级通过报告大小调整提示进行协作,因此实现任何自定义组件以在getXXSize方法中返回合理的值。

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 : 这是使用Rob的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的布局为空,因此需要明确声明其首选大小。

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

fixes it. 解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在进行操作时遇到问题 - I'm having trouble getting operations working 我在使用keytool获取SHA1证书时遇到问题 - I'm having trouble getting SHA1 certificate with keytool 我无法让MergeSort正常工作 - I'm having trouble getting MergeSort to work correctly Java-我无法让用户关闭程序 - Java - I'm having trouble getting the user to close the program 我无法理解PhotoView的getDisplayRect()实际返回的内容(构建android照片裁剪工具) - I'm having trouble understand what PhotoView's getDisplayRect() actually returns (building android photo cropping tool) 使用mod运算符时遇到问题; 我没有得到我期待的结果 - Having trouble with the mod operator; I'm not getting the result I'm expecting 我有这个问题。 我不断收到运行时错误,因为它同时打印2行? - I'm having trouble with this. I keep getting a run time error because it is printing 2 lines at the same time? 我在Android中知道此代码,但在以Swift语言获得相同的输出时遇到了麻烦 - I know this code in Android, but I'm having trouble in getting the same output in Swift language 我在使用doGet和doPost方法时遇到麻烦 - I'm having trouble with doGet and doPost methods 我在使用JAVA循环时遇到麻烦 - I'm having trouble with looping in JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM