سؤال

مشكلة:

تخطيط البرنامج الخاص بي على ما يرام ، كما هو موضح أدناه قبل أن أضيف jtoolbar إلى BorderLayout.PAGE_STARTإليك لقطة شاشة قبل إضافة JToolbar:alt text

إليكم كيف بدا الأمر بعد إضافة JToolbar:alt text

هل لي أن أعرف ماذا فعلت خطأ؟

هذا هو الرمز الذي استخدمته:

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

الرجاء المساعدة ، أنا جديد على بناء واجهة المستخدم الرسومية ، ولا أشعر باستخدام NetBeans لسحب واجهة المستخدم لي ... شكرًا لك مقدمًا.

هل كانت مفيدة؟

المحلول

بدلا من استخدام 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) لرؤية الفرق.

نصائح أخرى

لا أعرف ما هي القضية. حاولت تشغيله على نظامي عن طريق إصلاح مشكلات التجميع. هنا هو الرمز ولقطة الشاشة.

test frame

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

    }
}

تحرير: أنا أفهم لماذا الآن.

اعتدت على الطلاء لإعطائي تقديرًا تقريبيًا للبكسل ، ولم أكن أعرف سابقًا أن الارتفاع من بداية شريط عنوان الإطار العلوي يتم حسابه! بحيث يضيف ما يصل إلى ~ = 504. أحصل عليه الآن.

لذلك في المرة القادمة عندما يتعين علي ضبط الارتفاع تقريبًا ، أعتقد أنني سأستخدم الطلاء.

alt text


هممم غريب. لا بد لي من التغيير من:

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

إلى

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

ثم يعمل فقط.

هل يمكن لأي شخص أن يعلمني كيفية تقدير أو ضبط العرض/الارتفاع للمكونات؟ لأنني في البداية أردت أن يكون 640,480 ولكن يبدو الآن أنها تحتاج 640,504.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top