Pregunta

Problema:

Mi diseño del programa está muy bien, como a continuación antes de agregar JToolBar a BorderLayout.PAGE_START Aquí hay una captura de pantalla antes de añadir JToolBar: text alt

Así es como parecía después de añadir JToolBar: text alt

mayo sé lo que hice mal?

Este es el código utilicé:

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

Por favor, ayuda, soy nuevo en interfaz gráfica de usuario de construcción, y no me siento como el uso de Netbeans para arrastrar y soltar la interfaz de usuario para mí ... Gracias de antemano.

¿Fue útil?

Solución

En lugar de utilizar setSize() en el JFrame, establecer el tamaño preferido de su componente central como lo hace ahora e invocar pack() , que 'hace que esta ventana para ser de un tamaño para ajustarse al tamaño y la disposición de sus subcomponentes preferido.' Ampliando @ ejemplo de Bragaadeesh,

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

A continuación, cambiar a scrollPane.setPreferredSize(new Dimension(500, 300)) o JTextArea changeLog = new JTextArea(10, 30) para ver la diferencia.

Otros consejos

No sé cuál es el problema. Traté de ejecutarlo en mi sistema mediante la fijación de los problemas de compilación. Aquí está el código y pantalla.

marco de ensayo

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:. Entiendo por qué ahora

He utilizado pintura que me diera una estimación aproximada de los píxeles, y anteriormente no sabía que la altura desde el inicio de la barra superior del marco del título se cuenta! Por lo que se suma a ~ = 504. Ahora lo entiendo.

Así que la próxima vez que tenga que ajustar la altura más o menos, creo que voy a utilizar la pintura.

text alt


Hmmm raro. Tengo que cambiar de:

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

a

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

A continuación, solamente funciona.

Puede alguien me enseñe cómo estimar o establecer la anchura / altura de los componentes? Debido a que inicialmente quería que fuera 640,480 pero al parecer ahora necesita 640,504.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top