2

问题:

我的程序布局很好,如下在我添加 JToolbarBorderLayout.PAGE_START 之前这是添加 JToolbar 之前的屏幕截图: 替代文字

这是添加 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 Building 的新手,我不想使用 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

编辑:我现在明白为什么了。

我用Paint粗略估计了像素,之前不知道从顶框标题栏开始的高度算不算!所以加起来〜= 504。我现在明白了。

所以下次我必须大致设置高度时,我想我会使用Paint。

替代文字


嗯很奇怪。我必须改变:

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

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

然后只有它起作用。

有人可以教我如何估计或设置组件的宽度/高度吗?因为最初我希望它是,640,480但显然现在它需要640,504.

于 2010-05-02T14:00:42.870 回答