2

問題:

JToolbarを追加する前の以下のように、プログラムのレイアウトは問題ありません。JToolbarを追加する前BorderLayout.PAGE_START のスクリーンショットは次のとおりです。 代替テキスト

JToolbarを追加した後の外観は次のとおりです。 代替テキスト

何を間違えたのかわかりますか?

これが私が使用したコードです:

    //Create the text pane and configure it.
    textPane = new JTextPane();
    -snipped code-
    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(300, 300));

    //Create the text area for the status log and configure it.
    changeLog = new JTextArea(5, 30);
    changeLog.setEditable(false);
    JScrollPane scrollPaneForLog = new JScrollPane(changeLog);

    //Create a split pane for the change log and the text area.
    JSplitPane splitPane = new JSplitPane(
            JSplitPane.VERTICAL_SPLIT,
            scrollPane, scrollPaneForLog);
    splitPane.setOneTouchExpandable(true);

    //Create the status area.
    JPanel statusPane = new JPanel(new GridLayout(1, 1));
    CaretListenerLabel caretListenerLabel =
            new CaretListenerLabel("Caret Status");
    statusPane.add(caretListenerLabel);

    //Create the toolbar
    JToolBar toolBar = new JToolBar();
    -snipped code-

    //Add the components.
    getContentPane().add(toolBar, BorderLayout.PAGE_START);
    getContentPane().add(splitPane, BorderLayout.CENTER);
    getContentPane().add(statusPane, BorderLayout.PAGE_END);

    //Set up the menu bar.
    actions = createActionTable(textPane);
    JMenu editMenu = createEditMenu();
    JMenu styleMenu = createStyleMenu();
    JMenuBar mb = new JMenuBar();
    mb.add(editMenu);
    mb.add(styleMenu);
    setJMenuBar(mb);

助けてください、私はGUI構築に不慣れで、Netbeansを使用してUIをドラッグアンドドロップする気がしません...よろしくお願いします。

4

3 に答える 3

3

setSize()で を使用する代わりに、JFrame現在行っているように中心コンポーネントの優先サイズを設定して を呼び出しますpack()。これにより、「このウィンドウは、そのサブコンポーネントの優先サイズとレイアウトに合わせてサイズ変更されます」。@Bragaadeeshの例を拡張すると、

public static void main(String[] args) {
    TestFrame frame = new TestFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.build();
    frame.pack();
    frame.setVisible(true);
}

次に、scrollPane.setPreferredSize(new Dimension(500, 300))またはに変更しJTextArea changeLog = new JTextArea(10, 30)て違いを確認します。

于 2010-05-02T15:16:54.250 に答える
2

何が問題なのかわからない。コンパイルの問題を修正して、自分のシステムで実行しようとしました。これがコードとスクリーンショットです。

テスト フレーム

import java.awt.*;
import javax.swing.*;

public class TestFrame extends JFrame{
    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.build();
        frame.setVisible(true);

    }

    public void build(){
        setSize(600,600);
        //Create the text pane and configure it.
        JTextPane textPane = new JTextPane();

        JScrollPane scrollPane = new JScrollPane(textPane);
        scrollPane.setPreferredSize(new Dimension(300, 300));

        //Create the text area for the status log and configure it.
        JTextArea changeLog = new JTextArea(5, 30);
        changeLog.setEditable(false);
        JScrollPane scrollPaneForLog = new JScrollPane(changeLog);

        //Create a split pane for the change log and the text area.
        JSplitPane splitPane = new JSplitPane(
                JSplitPane.VERTICAL_SPLIT,
                scrollPane, scrollPaneForLog);
        splitPane.setOneTouchExpandable(true);

        //Create the status area.
        JPanel statusPane = new JPanel(new GridLayout(1, 1));
        JLabel caretListenerLabel =
                new JLabel("Caret Status");
        statusPane.add(caretListenerLabel);

        //Create the toolbar
        JToolBar toolBar = new JToolBar();
        toolBar.add(new JButton("Btn1"));
        toolBar.add(new JButton("Btn2"));
        toolBar.add(new JButton("Btn3"));
        toolBar.add(new JButton("Btn4"));

        //Add the components.
        getContentPane().add(toolBar, BorderLayout.PAGE_START);
        getContentPane().add(splitPane, BorderLayout.CENTER);
        getContentPane().add(statusPane, BorderLayout.PAGE_END);

        //Set up the menu bar.
        JMenu editMenu = new JMenu("test");
        JMenu styleMenu = new JMenu("test");
        JMenuBar mb = new JMenuBar();
        mb.add(editMenu);
        mb.add(styleMenu);
        setJMenuBar(mb);

    }
}
于 2010-05-02T13:36:16.520 に答える
1

編集:私は今なぜか理解しています。

ペイントを使用してピクセルの概算を取得しましたが、以前は、トップフレームのタイトルバーの先頭からの高さがカウントされていることを知りませんでした。つまり、合計すると〜=504になります。今すぐ取得します。

なので、次回は大まかに高さを設定するときは、ペイントを使うと思います。

代替テキスト


うーん、変だ。私はから変更する必要があります:

//Display the window.
frame.setSize(640, 480);

//Display the window.
frame.setSize(640, 504);

その後、それだけが機能します。

コンポーネントの幅/高さを推定または設定する方法を誰かに教えてもらえますか?最初はそれを望んでいたのです640,480が、どうやら今は必要640,504です。

于 2010-05-02T14:00:42.870 に答える