JScrollPane 内的 JDesktopPane 调整大小问题
JDesktopPane inside JScrollPane resize issue
我在 JScrollPane
里面有一个 JDesktopPane
。在 JDesktopPane
中有许多 JInternalFrame
s,以编程方式添加。我想要这种行为:
JDesktopPane
应该有一个固定大小(比主 JFrame 大小大很多)
调整大小时,JScrollPane
大小应根据主 JFrame 的不同而有所不同
基本上每个图像编辑器(即 Photoshop)都具有相同的行为,但视口中有 JInternalFrame
s 而不是图像。
起初我认为这种行为很容易获得,但我不能完全正确。我肯定遗漏了一些关于布局或相关的东西......
这是相关的 SSCCE
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;
}
}
JScrollPane 视口不考虑大小或边界,而是首选 大小。所以
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));
我在 JScrollPane
里面有一个 JDesktopPane
。在 JDesktopPane
中有许多 JInternalFrame
s,以编程方式添加。我想要这种行为:
JDesktopPane
应该有一个固定大小(比主 JFrame 大小大很多)调整大小时,
JScrollPane
大小应根据主 JFrame 的不同而有所不同
基本上每个图像编辑器(即 Photoshop)都具有相同的行为,但视口中有 JInternalFrame
s 而不是图像。
起初我认为这种行为很容易获得,但我不能完全正确。我肯定遗漏了一些关于布局或相关的东西......
这是相关的 SSCCE
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;
}
}
JScrollPane 视口不考虑大小或边界,而是首选 大小。所以
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));