Why is my JScrollPane not working?

2019-08-07 05:44发布

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.

3条回答
2楼-- · 2019-08-07 06:24
mainImageScrollPane.setViewportView(<component_to_be_scrollable>);
查看更多
SAY GOODBYE
3楼-- · 2019-08-07 06:36

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

查看更多
够拽才男人
4楼-- · 2019-08-07 06:45

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.

查看更多
登录 后发表回答