0

スクロールバー付きの JEditorPane を 2 時間作成しようとしましたが、あきらめようとしています。

これは私のコードの一部です:

    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);

this.add(editorScrollPane,c) を実行するとフレームが空になりますが、this.add(editorPane, c) を実行するとパネルが表示されます。this.add(new JLabel("test"),c) でもフレームは空です。

私のエラーはどこですか?

ありがとうございました

PS かなり大きいので、コード全体を投稿することはできません。

4

2 に答える 2

3
  1. エディタ ペインはコンテンツをバックグラウンドでロードします。これは、コンテナをレイアウトする準備ができた時点で、コンテンツがまだロードされていないことを意味します。
  2. 使用しているレイアウト マネージャーと指定した制約は、スクロール ペインの優先サイズを使用することを意味します。これは、コンテンツのニーズを満たすのに十分ではない可能性があります (これはスクロール ペインの機能であり、これが方法です)。設計されています)。

より多くの利用可能なスペースを使用することを奨励する制約を に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);
            }
        });
    }
}
于 2013-01-07T22:42:05.883 に答える
0

editorPane で適切なサイズを設定します。scrollPane は、そのビューポート サイズを探しています。フレームに最小サイズを設定することもできます。

于 2013-01-07T20:53:36.820 に答える