我有 Java 小程序来绘制一个数组(只是一个接一个的矩形)。
当用户选择创建大小数组时n
,它将绘制n
连接在一起的矩形。当n
变大时,图形会变大,但是由于我JPanel
用来绘制数组并且JPanel
不会滚动,所以我必须将其添加JPanel
到 aJScrollPane
中,但它仍然不会滚动。用户只能看到整个数组的一部分。
任何人都可以给我一些帮助吗?
这是我的代码:
public class ArrayPanel extends JPanel {
....
public void paintComponent(Graphics g) {
...draw array here..
// I wish to get the updated size of the graphis here,
// then i can reset the preferredSize()....?
System.out.println("width=" + getWidth() + " height=" + getHeight());
}
}
public class ArrayDemo extends JPanel {
public ArrayDemo() {
super(new BorderLayout());
arrayPanel = new ArrayPanel();
arrayPanel.setPreferredSize(new Dimension(400, 300));
JScrollPane container = new JScrollPane(arrayPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
container.setPreferredSize(arrayPanel.getPreferredSize());
add(container, BorderLayout.CENTER);
...
}
}