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

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

我可能也需要能够对该面板进行空布局,因为我可能不得不在其中手动定位内容。

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

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

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

更好的是,您应该使用布局管理器,并让这些管理人员为您计算组件大小和放置的繁重工作。 例如:

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

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

当你感到不可抗拒的手动干扰的冲动时,至少让经理尽可能地做。 在您的上下文中,可能会接管定位但让管理器处理大小调整,特别是计算父级的大小调整提示。 这是使用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); 

数据。 我发布后不久就意识到错了。

因为innerPanel具有null布局,所以需要显式声明其首选大小。

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

解决它。