【发布时间】:2014-09-19 00:13:18
【问题描述】:
我正在编写一些代码。 JFrame 包含具有特定尺寸的 JPanel。这是我的代码:
import javax.swing.*;
import java.awt.*;
public class ScrollPane extends JFrame {
public ScrollPane() {
super("Title");
setLayout(new BorderLayout());
setSize(320,240);
JScrollPane scroller = new JScrollPane(new DrawingPane());
scroller.setPreferredSize(new Dimension(320,240));
add(scroller);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class DrawingPane extends JPanel {
public DrawingPane(){
super();
setSize(640,480);
setMinimumSize(new Dimension(320,240));
}
}
public static void main(String[] args) {
new ScrollPane();
}
}
即使为JPanel 设置了最小尺寸,滚动也不会出现。
【问题讨论】:
-
private class DrawingPane extends JPanel { .. setSize(640,480);我会将DrawingPane替换为BufferedImage中显示的JLabel.. -
+1 用于解决方法
-
在覆盖
getPreferredSize()@camickr (IMO 应该是公认的答案)指出之后,调用frame.pack()而不是frame.setSize()。pack()方法将考虑所有内部组件的首选大小,然后相应地调整框架的大小
标签: java swing jpanel jscrollpane preferredsize