我在JDesktopPane里面有一个JScrollPane。在JDesktopPane内部有许多以编程方式添加的JInternalFrame。我希望有这样的行为:
JDesktopPane应该有固定的大小(比主要的JFrame大小大得多)JScrollPane大小应与主JFrame相应地变化。基本上每个图像编辑器都有相同的行为(即Photoshop),但是视图中有JInternalFrames,而不是图像。
起初,我认为这种行为很容易得到,但我不能完全正确。我肯定是错过了一些布局或者其他相关的东西..。
这是相关的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;
}
}发布于 2017-10-01 17:00:05
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));https://stackoverflow.com/questions/46514524
复制相似问题