BorderLayout problema con JSplitPane dopo l'aggiunta JToolBar (Java)
-
02-10-2019 - |
Domanda
Problema:
Il mio layout di programma va bene, come sotto prima aggiungo JToolBar a BorderLayout.PAGE_START
Ecco uno screenshot prima che si aggiunge JToolBar:
Ecco come sembrava dopo l'aggiunta JToolBar:
Si può sapere che cosa ho fatto di sbagliato?
Ecco il codice che ho usato:
//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);
Si prega di aiuto, Sono nuovo di GUI Building, e non mi sento come l'utilizzo di Netbeans per trascinare e rilasciare l'interfaccia utente per me ... Grazie in anticipo.
Soluzione
Invece di usare setSize()
sul JFrame
, impostare la dimensione preferita del componente centrale come si fa ora e richiamare pack()
, che 'causa questa finestra per essere di dimensioni adatte alla dimensione preferita e layout delle sue sottocomponenti.' Ampliando l'esempio di @ Bragaadeesh,
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.build();
frame.pack();
frame.setVisible(true);
}
Quindi, passare alla scrollPane.setPreferredSize(new Dimension(500, 300))
o JTextArea changeLog = new JTextArea(10, 30)
per vedere la differenza.
Altri suggerimenti
Non so qual è il problema. Ho provato a farlo funzionare sul mio sistema per la risoluzione dei problemi di compilazione. Ecco il codice e 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:. Capisco perché ora
ho usato vernice di darmi una stima approssimativa dei pixel, e in precedenza non sapevo che l'altezza dall'inizio della barra superiore del titolo telaio è contato! Quindi, che aggiunge fino a ~ = 504. Ho capito ora.
Quindi, la prossima volta quando devo impostare l'altezza o meno, penso di usare la vernice.
Hmmm strano. Devo cambiare da:
//Display the window.
frame.setSize(640, 480);
a
//Display the window.
frame.setSize(640, 504);
Solo allora funziona.
Qualcuno può insegnarmi a stimare o impostare la larghezza / altezza per i componenti? Perché Inizialmente ho voluto che fosse 640,480
ma a quanto pare la società ha bisogno di 640,504
.