代码之家  ›  专栏  ›  技术社区  ›  Ararararagi

JDesktopPane内部JScrollPane调整大小问题

  •  1
  • Ararararagi  · 技术社区  · 9 年前

    JDesktopPane a内部 JScrollPane JDESKTOPANE公司 JInternalFrame s、 以编程方式添加。我希望有这样的行为:

    • 这个 应具有固定尺寸(比主JFrame尺寸大得多)

    • 滚动面板 调整大小时,大小应根据主框架而变化

    与每个图像编辑器(即Photoshop)的行为基本相同,但 内部框架 s而不是图像。

    起初我以为这种行为很容易发生,但我不能完全正确。当然我错过了一些关于布局或相关的东西。。。

    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;
        }
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Hovercraft Full Of Eels    9 年前

    首选

    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));