【发布时间】:2013-10-20 15:38:25
【问题描述】:
有人可以向我解释为什么这不起作用吗? 错误似乎在 Gen 类中,但是 我认为这可能与 BoxMan 有关。 错误说找不到符号变量 g。 我也尝试输入整数和双精度,但它给了我:必需的(Java.awt.Graphics)找到(整数)/(双精度)。那么如何解决这个问题呢?我到处找,找不到答案。帮助初学者!
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.lang.Object.*;
public class JFrame_Test
{
public static void main (String [] args)
{
Gen Gen= new Gen (1500,1000,"A Name"); // this gives parameters for a Jframe later.
}
}
{
Gen (int size1, int size2, String title)
{
JFrame aFrame = new JFrame (title);
aFrame.setSize(size1,size2);
aFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
aFrame.setVisible(true);
//aFrame.getContentPane().add(new Canvas());
//Was trying to get it to work with a canvas
BoxMan.paint (g); // the error pops up here.
}
}
public class BoxMan
{
public Graphics2D g2;
public void paint(Graphics a )
{
g2 = (Graphics2D) g; // i even tried declaring "g" here.
g2.drawRect (10, 10, 200, 200);
}
}
【问题讨论】:
-
您能发布如何在
Gen类中初始化g吗? -
"Swing 程序应该覆盖
paintComponent(),而不是覆盖paint()。"—Painting in AWT and Swing: The Paint Methods。 -
你在哪里找到这个使用 Canvas 和 paint() 方法的例子。我会忽略那个教程和网站。从 Custom Painting 上的 Swing 教程开始,以获得更好的绘画方式。
-
非常感谢您的建议。
标签: java swing graphics2d