2013-03-11 27 views
1

我正在嘗試將JEditorPane添加到JFrame中,並使用pack()調整JFrame的大小。當我啓動該程序時,默認大小不起作用,並且pack使JEditorPane的高度約爲10個像素。如何使JEditorPane以我定義的大小啓動?調整JEditor中的JEditorPane

public class GUIManager extends JFrame { 

JButton copyButton = new JButton("Copy"); 
JButton cutButton = new JButton("Cut"); 
JButton pasteButton = new JButton("Paste"); 
JButton selectButton = new JButton("Select All"); 
JButton clearButton = new JButton("Clear"); 
JButton searchButton = new JButton("Search"); 
JButton replaceButton = new JButton("Replace"); 

JEditorPane editPane; 
JScrollPane scrollPane; 

JPanel buttons = new JPanel(); 

public GUIManager(){ 

    buttons.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10)); 

    editPane = new JEditorPane(){ 

     public boolean getScrollableTrackViewportWidth(){ 

      return true; 

     } 

    }; 
    editPane.setSize(500, 500); 
    scrollPane = new JScrollPane(editPane); 

    buttons.add(copyButton); 
    buttons.add(cutButton); 
    buttons.add(pasteButton); 
    buttons.add(selectButton); 
    buttons.add(clearButton); 
    buttons.add(searchButton); 
    buttons.add(replaceButton); 

    this.add(buttons, BorderLayout.NORTH); 
    this.add(scrollPane, BorderLayout.CENTER); 
} 

} 

回答

2

使用了setPreferredSize():

editPane.setPreferredSize(new Dimension(500, 500)); 
+0

更好重寫['的getPreferredSize()'](http://stackoverflow.com/q/7229226/230513)。 – trashgod 2013-03-11 20:39:50