2010-05-02 69 views
2

問題:BorderLayout的問題,調整JSplitPane將JToolBar中(JAVA)後

我的程序設計是好的,下面我之前添加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); 

請幫幫忙,我是新來的GUI應用,我不喜歡使用NetBeans拖放用戶界面,我...謝謝您提前。

回答

3

而不是在JFrame使用setSize()的,設置您的中心組件的首選大小,你現在和調用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)看出區別。

+0

@trashgod:哦!所以這就是它應該是的。謝謝! – 2010-05-03 05:24:28

2

我不知道是什麼問題。我試圖通過修復編譯問題在我的系統上運行它。這裏是代碼和截圖。

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

    } 
} 
+0

@Bragaadeesh:好吧......那很奇怪。我會嘗試複製粘貼您使用的並測試並看到的內容。謝謝。 – 2010-05-02 13:43:26

1

編輯:爲什麼我現在明白了。

我用Paint來給我一個粗略的像素估計,之前我不知道從頂部框架標題欄開始的高度是否被計數!所以這加起來〜= 504。我現在明白了。

所以下一次當我不得不大致設置高度時,我想我會使用Paint。

alt text


嗯怪異。我必須要改變從:

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

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

那麼只有它的工作原理。

有人可以教我如何估計或設置組件的寬度/高度?因爲最初我希望它是640,480,但顯然現在它需要640,504