我在JScrollPane
中有一个JDesktopPane
。在JDesktopPane
中有许多以编程方式添加的JInternalFrame
。我希望有这种行为:
>
JDesktopPane
应该有一个固定的大小(比主要的JFrame大小大得多)
JScrollPane
大小应根据调整大小时的主JFrame而变化
基本上与每个图像编辑器(即Photoshop)的行为相同,但是在视窗中使用了< code>JInternalFrame而不是图像。
一开始我以为这种行为会很容易得到,但我不能完全正确地得到它。当然,我错过了一些关于布局或相关的东西...
下面是相关的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));