我有一个 JFrame
和一个扩展 JPanel
并重写 paintComponent()
的类(名为 DrawingPanel
)。 DrawingPanel
的实例被添加到 JFrame
中。
这是 DrawingPanel
的 paintComponent()
方法:
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Rectangle rect = new Rectangle(80,180,50,50);
g2d.fill(rect);
}
我运行了程序,似乎在创建rect
时指定了坐标80、180,引用了包含DrawingPanel<实例的
。我希望这些坐标引用实际绘制矩形的 JFrame
/JPanel
。
1)事情应该是这样吗?
2)可以改变这个吗?
谢谢
最佳答案
I would expect that these coordinates would refer to the JPanel where the rectangle is actually painted.
它们确实指的是 JPanel,而不是框架。
你认为他们为什么会提到框架?发布您的SSCCE这说明了问题。
关于java - 关于在屏幕上放置矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20790103/