首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JDesktopPane内部JScrollPane调整大小问题

JDesktopPane内部JScrollPane调整大小问题
EN

Stack Overflow用户
提问于 2017-10-01 16:40:29
回答 1查看 279关注 0票数 1

我在JDesktopPane里面有一个JScrollPane。在JDesktopPane内部有许多以编程方式添加的JInternalFrame。我希望有这样的行为:

  • JDesktopPane应该有固定的大小(比主要的JFrame大小大得多)
  • 当调整大小时,JScrollPane大小应与主JFrame相应地变化。

基本上每个图像编辑器都有相同的行为(即Photoshop),但是视图中有JInternalFrames,而不是图像。

起初,我认为这种行为很容易得到,但我不能完全正确。我肯定是错过了一些布局或者其他相关的东西..。

这是相关的SSCCE

代码语言:javascript
复制
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Rectangle;

class Main extends JFrame {

    JDesktopPane container = new JDesktopPane();

    // New frame with 2 JInternalFrames inside a JDesktopPane inside a JScrollPane
    Main(){
        super("JDesktopPane SS");
        setSize(1280, 720);

        setLayout(new ScrollPaneLayout());

        container.setBounds(new Rectangle(1920, 1080));

        JScrollPane scrollContainer = new JScrollPane(container, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        setContentPane(scrollContainer);

        container.add(createFrame());
        container.add(createFrame());
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(() -> {
            // Create new frame with 2 JInternalFrames inside a JDesktopPane inside a JScrollPane
            JFrame main_frame = new Main();

            main_frame.setVisible(true);
        });
    }

    // Create new InternalFrame with a TextPane
    private JInternalFrame createFrame(){
        JInternalFrame new_frame = new JInternalFrame("Document");
        new_frame.setResizable(true);

        JTextPane txt = new JTextPane();
        txt.setPreferredSize(new Dimension(100, 80));
        new_frame.add(txt);

        new_frame.pack();
        new_frame.setVisible(true);

        return new_frame;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-01 17:00:05

JScrollPane视图不尊重大小或边界,而是首选的大小。所以

代码语言:javascript
复制
class Main extends JFrame {

    private static final int DT_WIDTH = 1920;
    private static final int DT_HEIGHT = 1080;
    private JDesktopPane container = new JDesktopPane();

    public Main(){
        super("JDesktopPane SS");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1280, 720);

        // setLayout(new ScrollPaneLayout()); // ?????

        // container.setBounds(new Rectangle(1920, 1080));
        container.setPreferredSize(new Dimension(DT_WIDTH, DT_HEIGHT));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46514524

复制
相关文章

相似问题

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