JScrollPane 内部的内部 JScrollPane 无法正常工作

我正在尝试创建一个带有滚动条的容器,并且容器内部有两个内部面板。顶部内面板内还有另一个 JScrollPane。

但目前我面临的问题是,当我的顶部内面板太长(宽度)时,顶部内面板内的滚动条被禁用,我只能滚动容器的滚动条。

http://img3.mukewang.com/649be0d70001d42304850152.jpg

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.FlowLayout;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;


public class TestFrame {


    public static void main(String... args) {

        JFrame frame = new JFrame();

        JPanel panel = new JPanel();

        for (int i = 0; i < 10; i++) {

            panel.add(new JButton("Hello-" + i));

        }

        JScrollPane scrollPane = new JScrollPane(panel);

        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        JPanel contentPane = new JPanel(new BorderLayout());


        JPanel contentPaneSub = new JPanel();

        contentPaneSub.add(scrollPane);


        contentPane.add(contentPaneSub, BorderLayout.NORTH);


        JPanel centerPanel = new JPanel(new FlowLayout());

        centerPanel.add(new JButton("Example"));

        contentPane.add(centerPanel, BorderLayout.CENTER);


        JScrollPane scrollPane1 = new JScrollPane(contentPane);

        scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);


        frame.setContentPane(scrollPane1);

        //for demo purpose we set this using hard coded way

        //in real life project the java will auto adjust it size based on windows resolution

        frame.setSize(new Dimension(500, 160));

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.setVisible(true);

    }

}

我希望得到的是,如果顶部内面板的宽度太长,那么顶部内面板内的滚动条将可见并允许滚动。不是容器中的滚动条。


喵喵时光机
浏览 122回答 1
1回答

www说

我在尝试解决这个问题时遇到了两个不同的问题。第一个是将JScrollPane包含在窗口内,第二个是JScrollPane动态调整 的大小。我能够解决第一个问题,但无法使用自定义类完全解决第二个问题。随着窗口的增加动态JScrollPane增加其宽度,但不会随着窗口大小动态缩小。这是因为当窗口尺寸减小时,outerJScrollPane会锁定内部内容的宽度,包括inner JScrollPane。我无法找到一种方法让内部窗格动态收缩,而无需有效删除外部窗格的功能,这是行不通的,因为您的问题是专门针对另一个功能内JScrollPane的JScrollPane。public class TestFrame {&nbsp; &nbsp; public static void main(String... args) {&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp; JPanel panel = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panel.add(new JButton("Hello-" + i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; MyCustomPane scrollPane = new MyCustomPane(panel); //changed this line&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);&nbsp; &nbsp; &nbsp; &nbsp; JPanel contentPane = new JPanel(new BorderLayout());&nbsp; &nbsp; &nbsp; &nbsp; JPanel contentPaneSub = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; contentPaneSub.add(scrollPane);&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.setOuterContainer(contentPaneSub); //added this line&nbsp; &nbsp; &nbsp; &nbsp; contentPane.add(contentPaneSub, BorderLayout.NORTH);&nbsp; &nbsp; &nbsp; &nbsp; JPanel centerPanel = new JPanel(new FlowLayout());&nbsp; &nbsp; &nbsp; &nbsp; centerPanel.add(new JButton("Example"));&nbsp; &nbsp; &nbsp; &nbsp; contentPane.add(centerPanel, BorderLayout.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; JScrollPane scrollPane1 = new JScrollPane(contentPane);&nbsp; &nbsp; &nbsp; &nbsp; scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);&nbsp; &nbsp; &nbsp; &nbsp; scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);&nbsp; &nbsp; &nbsp; &nbsp; frame.setContentPane(scrollPane1);&nbsp; &nbsp; &nbsp; &nbsp; //for demo purpose we set this using hard coded way&nbsp; &nbsp; &nbsp; &nbsp; //in real life project the java will auto adjust it size based on windows resolution&nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(new Dimension(500, 160));&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; }}MyCustomPane 类的代码:public class MyCustomPane extends JScrollPane {&nbsp; &nbsp; Container outerContainer;&nbsp; &nbsp; public MyCustomPane(Component view) {&nbsp; &nbsp; &nbsp; &nbsp; super(view);&nbsp; &nbsp; }&nbsp; &nbsp; public void setOuterContainer(Container outerContainer) {&nbsp; &nbsp; &nbsp; &nbsp; this.outerContainer = outerContainer;&nbsp; &nbsp; }&nbsp; &nbsp; private Dimension getCustomDimensions() {&nbsp; &nbsp; &nbsp; &nbsp; if (outerContainer == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Dimension(0, 0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return new Dimension(outerContainer.getWidth() - 10, 60); //10 pixels less than container width, arbitrary height&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Dimension getMaximumSize() {&nbsp; &nbsp; &nbsp; &nbsp; return getCustomDimensions();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Dimension getMinimumSize() {&nbsp; &nbsp; &nbsp; &nbsp; return getCustomDimensions();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Dimension getPreferredSize() {&nbsp; &nbsp; &nbsp; &nbsp; return getCustomDimensions();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java