我正在尝试将JEditorPane添加到JFrame中,并且我正在使用pack()调整JFrame的大小。当我启动程序时,默认大小什么都不做,并且打包使JEditorPane的高度约为10像素。如何使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);
}
}
答案 0 :(得分:2)
使用setPreferredSize():
editPane.setPreferredSize(new Dimension(500, 500));