Frage

Problem:

Mein Programm-Layout ist in Ordnung, wie unten, bevor ich JToolBar zu BorderLayout.PAGE_START hinzufügen Hier ist ein Screenshot vor JToolBar angefügt: alt text

Hier ist, wie es sah aus wie nach JToolBar und fügte hinzu: alt text

May Ich weiß, was habe ich falsch gemacht?

Hier ist der Code, den ich verwendet:

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

Bitte Hilfe, ich bin neu in GUI-Gebäuden, und ich fühle mich nicht wie Netbeans mit Drag & Drop die Benutzeroberfläche für mich ... Vielen Dank im Voraus.

War es hilfreich?

Lösung

Anstelle der Verwendung von setSize() auf dem JFrame, die bevorzugte Größe der Zentralkomponente eingestellt, wie Sie jetzt tun und invoke pack() , die "dieses Fenster verursacht werden bemessen, um die gewünschte Größe und Layouts seiner Subkomponenten passen." die Erweiterung auf @ Bragaadeesh dem Beispiel,

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

Dann Änderung scrollPane.setPreferredSize(new Dimension(500, 300)) oder JTextArea changeLog = new JTextArea(10, 30) den Unterschied zu sehen.

Andere Tipps

Ich weiß nicht, was das Problem ist. Ich versuchte es auf meinem System laufen zu lassen, indem sie die Kompilierung Probleme beheben. Hier ist der Code und Screenshot.

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

    }
}

EDIT:. Ich verstehe, warum jetzt

verwenden Ich male mir eine grobe Schätzung der Pixel zu geben, und vorher wusste ich nicht, dass die Höhe vom Beginn der oberen Rahmentitelleiste wird gezählt! So fügt hinzu, dass bis zu ~ = 504. ich es bekommen.

Also das nächste Mal, wenn ich in etwa die Höhe einzustellen, ich glaube, ich Farbe verwenden werden.

alt text


Hmmm seltsam. Ich muss Wechsel von:

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

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

Dann nur funktioniert es.

Kann jemand mir beibringen, wie zu schätzen oder die Breite / Höhe für die Komponenten eingestellt? Denn zunächst wollte ich es sein 640,480 aber anscheinend jetzt braucht es 640,504.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top