我有Java小程序绘制一个数组(只是一个接一个的矩形)。如何更改图形的大小
当用户选择创建大小为n
的数组时,它将绘制连接在一起的n
矩形。当n
变大,图形变大,但由于我用JPanel
来绘制数组,而JPanel
不会滚动,我不得不将JPanel
加入JScrollPane
,但它仍然不会滚动。用户只能看到整个阵列的一部分。
任何人都可以给我一些帮助吗?
这里是我的代码:
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);
...
}
}
@ohana:我已经重新格式化你的问题的更好的可读性,和您提供的代码以避免滚动。如果您不同意,请随时回滚我的更改。 – 2010-02-21 09:52:13