简体   繁体   English

JScrollPane里面的Java JPanel?

[英]Java JPanel inside JScrollPane?

I have a JFrame, in this JFrame I have a JPanel that I draw on, this Panel can be any size and so I placed it into a JScrollpane to let me scroll when the panel is larger than the window screen size.我有一个 JFrame,在这个 JFrame 中我有一个 JPanel,我可以在上面画画,这个面板可以是任何大小,所以我把它放在 JScrollpane 中,以便在面板大于窗口屏幕大小时滚动。

Unfortunately does not work as I expected:不幸的是,它不像我预期的那样工作:

  1. Making the JFrame window smaller than the JPanel size does not show scroll bars使 JFrame 窗口小于 JPanel 大小不显示滚动条

  2. The JScrollPane size now seems locked to the size of the JPanel I have added to it, where as before it resized to the bounds of it's JFrame window (it still kinda does this but only vertically now?!) JScrollPane 的大小现在似乎锁定到我添加到它的 JPanel 的大小,和以前一样,它调整到它的 JFrame 窗口的边界(它仍然有点这样做,但现在只是垂直的?!)

  3. The JPanel seems to assume the size of the JScrollpane regardless of what I set for preferred size JPanel 似乎假定 JScrollpane 的大小,而不管我为首选大小设置了什么

I am sure I'm doing something stupid, if someone could point out what I would be most grateful!我敢肯定我在做一些愚蠢的事情,如果有人能指出我最感激的事情!

JPanel imageCanvas = new JPanel(); // 'Canvas' to draw on
JScrollPane scrollPane = new JScrollPane();
    
// set size of 'canvas'
imageCanvas.setMinimumSize(new Dimension(100,100));

// Scroll pane smaller then the size of the canvas so we should get scroll bars right?
scrollPane.setMinimumSize(new Dimension(50,50)); 
    
// Add a border to 'canvas'
imageCanvas.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    
scrollPane.setViewportView(imageCanvas);

setPreferredSize() is the trick, setMinimumSize() and even setSize() on the component will be ignored by JScrollPane. setPreferredSize() 是诀窍,组件上的 setMinimumSize() 甚至 setSize() 将被 JScrollPane 忽略。 Here's a working example using a red border.这是一个使用红色边框的工作示例。

import java.awt.*;

import javax.swing.*;

public class Scroller extends JFrame {

    public Scroller() throws HeadlessException {
        final JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.red));
        panel.setPreferredSize(new Dimension(800, 600));

        final JScrollPane scroll = new JScrollPane(panel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(scroll, BorderLayout.CENTER);
        setSize(300, 300);
        setVisible(true);
    }

    public static void main(final String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Scroller().setVisible(true);
            }
        });
    }
}
// suggest a size of 'canvas'
_ImageCanvas.setPreferredSize(new Dimension(100,100));

// Scroll pane smaller then the size of the canvas so we should get scroll bars right?
_ScrollPane.setPreferredSize(new Dimension(50,50)); 

// ..later 
_Frame.pack();
  • Set preferred size on the canvas.在画布上设置首选大小。
  • Increase dimensions 100,100 is too small atleast on my computer.增加尺寸 100,100 至少在我的电脑上太小了。
  • You may want to use new GridLayout(1,1);你可能想使用 new GridLayout(1,1); for you JFrame if you want the scrollpane to expand when you expand the frame.如果您希望在展开框架时展开滚动窗格,则为您提供 JFrame。

As far as I remember there are 2 options:据我所知,有两种选择:

For more details, see the Javadoc: http://docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html有关更多详细信息,请参阅 Javadoc: http : //docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html

Check out the Scrollable interface, this may help with the sizing issues.查看Scrollable界面,这可能有助于解决大小问题。

These two method maybe helpful:这两种方法可能有帮助:

boolean getScrollableTracksViewportWidth();

boolean getScrollableTracksViewportHeight();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM