Why is my JScrollPane not working?

戏子无情 提交于 2019-12-11 09:37:49

问题


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 **

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.


回答1:


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

visibleArea.add(frameContent);

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

visibleArea.add(mainImageScrollPane);

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.




回答2:


You didn't specify any size for your frameContent. Is it intentional?

Additionally your frameContent is later on being added to visibleArea. Meaning no longer in the mainImageScrollPane JScrollPane

Maybe you wanna have: visibleArea.add(mainImageScrollPane);, but you need to set your panel size




回答3:


mainImageScrollPane.setViewportView(<component_to_be_scrollable>);


来源:https://stackoverflow.com/questions/11117233/why-is-my-jscrollpane-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!