2010-05-02 20 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