首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JScrollPane中的内部JScrollPane不能正常工作

JScrollPane中的内部JScrollPane不能正常工作
EN

Stack Overflow用户
提问于 2019-08-30 15:28:23
回答 1查看 153关注 0票数 0

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

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

代码语言:javascript
复制
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);
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-31 01:58:18

我在尝试解决这个问题时遇到了两个不同的问题。第一个是将JScrollPane包含在窗口中,第二个是将JScrollPane的大小调整为动态的。

我能够解决第一个问题,但不能使用自定义类完全解决第二个问题。JScrollPane的宽度会随着窗口的增加而动态增加,但不会随着窗口大小的增加而动态缩小。这是因为当窗口大小减小时,外部JScrollPane会锁定内部内容的宽度,包括内部JScrollPane

我找不到一种方法来让内部窗格动态收缩,而不是有效地删除外部窗格的功能,这是不起作用的,因为您的问题是专门针对另一个功能性JScrollPane中的JScrollPane的。

代码语言:javascript
复制
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));
        }

        MyCustomPane scrollPane = new MyCustomPane(panel); //changed this line

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

        scrollPane.setOuterContainer(contentPaneSub); //added this line

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

MyCustomPane类的代码如下:

代码语言:javascript
复制
public class MyCustomPane extends JScrollPane {
    Container outerContainer;

    public MyCustomPane(Component view) {
        super(view);
    }

    public void setOuterContainer(Container outerContainer) {
        this.outerContainer = outerContainer;
    }

    private Dimension getCustomDimensions() {
        if (outerContainer == null) {
            return new Dimension(0, 0);
        }
        return new Dimension(outerContainer.getWidth() - 10, 60); //10 pixels less than container width, arbitrary height
    }
    @Override
    public Dimension getMaximumSize() {
        return getCustomDimensions();
    }
    @Override
    public Dimension getMinimumSize() {
        return getCustomDimensions();
    }
    @Override
    public Dimension getPreferredSize() {
        return getCustomDimensions();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57722385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档