我尝试了两个小时制作一个带滚动条的JEditorPane,我即将放弃!
这是我的代码的一部分:
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)面板显示。即使使用this.add(新的JLabel(“test”),c)框架也是空的。
我的错误在哪里?
谢谢
P.S。我无法发布整个代码,因为它非常大。
答案 0 :(得分:3)
提供鼓励使用更多可用空间的GridBagLayout
的约束或不依赖于组件的首选大小的布局管理器(如BorderLayout
)
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);
}
});
}
}
答案 1 :(得分:0)
在editorPane上设置首选大小。 scrollPane正在为其视口大小寻找它。您可能还想在框架上设置最小尺寸。