为什么我的JScrollPane不起作用? [英] Why is my JScrollPane not working?

查看:120
本文介绍了为什么我的JScrollPane不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以找出为什么我的JScrollPane无法正常工作的原因.也许我可能错过了一些东西.我意识到,如果没有比我所展示的更多的背景信息,这可能是愚蠢的,但是请问,我将乐意提供更多信息.

Can someone work out why my JScrollPane is not working. Perhaps something I may have missed. I realize this might be silly without any more context than what I've shown but please ask and I shall be happy to provide more.

public ApplicationFrame(String title, int x, int y, int width, int height) {
        // Constructor for the ApplicationFrame, no implicit Construc.
        setTitle(title);
        setResizable(true);
        setBounds(x, y, width, height);
                    setLayout(new BorderLayout());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setIconImage(new ImageIcon(getClass().getResource("resources/topLeft.png")).getImage());
        topMostMenuBar = new TopMenuBar(this);
        setJMenuBar(topMostMenuBar.getMyJMenuBar());
        paneEdge = BorderFactory.createLineBorder(Color.gray);
        blackline = BorderFactory.createLineBorder(Color.black);
        this.frameContent = new ApplicationPanel() {
            //@Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(TI, 0, 0, null);
            }
        };
        mainImageScrollPane = new JScrollPane(frameContent);
        statusPanel = new ApplicationPanel(new Color(0xfff0f0f0));
        leftPanel = new ApplicationPanel(new Color(0xfff0f0f0));
        testPanel = new ColorPanel(new Color(0xfff0f0f0));
        testPanel.setPreferredSize(new Dimension(50,300));
        add(mainImageScrollPane, BorderLayout.CENTER );
        add(statusPanel, BorderLayout.SOUTH);
        add(leftPanel, BorderLayout.WEST);
        Container visibleArea = getContentPane();
        visibleArea.add(frameContent);
        setVisible(true);
        do {
            loadImageIn();
        } while (!initLoadSuccess);
        initButtons();
        leftPanel.add(testPanel, BorderLayout.SOUTH);
    } // end Constructor **

这是一段很大的代码,所以我不确定如何用它来制作SSCCE.您正在查看的是我的JFrame子类的构造函数,该子类包含3个面板.此时,ApplicationPanel只是一个JPanel. loadImageIn()方法打开一个文件选择器,然后加载选择的图像并将其绘制到frameContent上.图像显示正常,一切正常,但当我调整窗口大小时,没有滚动条.

This is a big piece of code so I'm not sure how to make an SSCCE out of it. What youre looking at is the constructor to my subclass of a JFrame, which holds 3 panels. The ApplicationPanel is at this point just a JPanel. The loadImageIn() method opens a filechooser and then loads the chosen image which is painted onto frameContent. The image displays fine, everything works, except when I resize the window, there are no scrollbars.

推荐答案

您具有这一行,它将ApplicationPanel添加到visibleArea ...

You have this line, which adds the ApplicationPanel to the visibleArea...

visibleArea.add(frameContent);

也许您是真的意思,这会将JScrollPane添加到visibleArea(并且JScrollPane已经包含ApplicationPanel)...

Maybe you actually mean this, which adds the JScrollPane to the visibleArea (and the JScrollPane already contains the ApplicationPanel)...

visibleArea.add(mainImageScrollPane);

调用new JScrollPane(frameContent)时,它对内部面板没有任何作用,只是在外部添加了一个包装器.因此,如果要具有滚动功能,则需要引用JScrollPane包装器,而不是面板本身.

When you call new JScrollPane(frameContent), it doesn't do anything to the panel inside it, it just adds a wrapper around the outside. So, if you want the ability to scroll, you need to refer to the JScrollPane wrapper, rather than the panel itself.

这篇关于为什么我的JScrollPane不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆