Java 为什么我的相框是空的?
我花了两个小时试图用滚动条制作一个绝地武士窗格,我就要放弃了 这是我代码的一部分:Java 为什么我的相框是空的?,java,swing,frame,Java,Swing,Frame,我花了两个小时试图用滚动条制作一个绝地武士窗格,我就要放弃了 这是我代码的一部分: JEditorPane editorPane = new JEditorPane(); URL helpURL = GUIMain.class .getResource("/resources/einleitungstext1.html"); this.setLayout(new GridBagLayout()); GridBagConstraints c
JEditorPane editorPane = new JEditorPane();
URL helpURL = GUIMain.class
.getResource("/resources/einleitungstext1.html");
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
try {
editorPane.setPage(helpURL);
} catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + helpURL);
}
editorPane.setEditable(false);
JScrollPane editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScrollPane.setMinimumSize(new Dimension(100, 100));
editorScrollPane.setPreferredSize(new Dimension(main.screenWidth-200, main.screenHeight-200));
c.gridx = 0;
c.gridy = 0;
this.add(editorScrollPane, c);
this.setVisible(true);
当我这样做时.add(editorScrollPane,c)框架是空的,但是当我这样做时.add(editorPane,c)面板显示出来。即使使用这个.add(new JLabel(“test”),c)框架也是空的
我的错误在哪里
多谢各位
另外,我不能发布整个代码,因为它相当大。在editorPane上设置一个首选大小。滚动窗格正在根据其视口大小查找它。您可能还需要在相框上设置最小尺寸。在editorPane上设置首选尺寸。滚动窗格正在根据其视口大小查找它。您可能还需要在框架上设置最小尺寸
GridBagLayout
提供约束以鼓励使用更多可用空间,或者提供不依赖组件首选大小的布局管理器(如BorderLayout
)
GridBagLayout
提供约束以鼓励使用更多可用空间,或者提供不依赖组件首选大小的布局管理器(如BorderLayout
)
这是什么课?它是从什么继承的?它在哪个类中?它继承了什么?当我在editorPane上设置首选大小时,没有任何变化。无论如何,框架都是最大化的。您还有其他想法吗?视口通常不关心其内容的首选大小,这就是重点,它将使用
可滚动
界面提供的信息(如果可用)。如果你真的想遵循这条思路,您最好创建一个自定义版本的编辑器,实现可滚动
界面,并从可滚动#getPreferredScrollableViewportSize
返回一个适当的值,但如果对其进行一些调整或使用不同的布局管理器可以解决问题,那么这将是一项大量工作编辑纸没有任何变化。无论如何,框架都是最大化的。您还有其他想法吗?视口通常不关心其内容的首选大小,这就是重点,它将使用可滚动
界面提供的信息(如果可用)。如果你真的想遵循这条思路,您最好创建一个自定义版本的编辑器,实现可滚动
界面,并从可滚动#getPreferredScrollableViewportSize
返回一个适当的值-但如果对其进行一些调整或使用不同的布局管理器来解决问题,这将是一项大量工作
public class TestLayout18 {
public static void main(String[] args) {
new TestLayout18();
}
public TestLayout18() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JEditorPane editorPane = new JEditorPane();
try {
editorPane.setPage(new URL("http://docs.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html"));
} catch (IOException e) {
System.err.println("Attempted to read a bad URL");
}
editorPane.setEditable(false);
JScrollPane editorScrollPane = new JScrollPane(editorPane);
frame.add(editorScrollPane);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}