【发布时间】:2014-10-20 18:31:01
【问题描述】:
我有一个扩展 JFrame 并创建一个窗口的类,它需要调用另一个类中的 paint() 方法。我知道如果它们在同一个类中, setVisible(true) 会调用paint方法,但由于它们在不同的类中,所以不会。我已经创建了 Die 类的对象(一幅画),但我不知道如何使用它们来调用 paint 方法。
这是创建窗口的类:
public class Game extends Frame
{
public void window()
{
setTitle("Roll"); // Title of the window
setLocation(100, 100); // Location of the window
setSize(900, 600); // Size of the window
setBackground(Color.lightGray); // Color of the window
setVisible(true); // Make it appear and call paint
}
对于另一个名为 Die 的类中的绘制方法,我使用了:
public void paint(Graphics pane)
【问题讨论】:
-
一般回答:您需要在
Game类中引用Die,方法是调用构造函数Die die = new Die(),然后调用绘制方法die.paint()或在您创建的Die类中paint()方法static然后这样调用它Die.paint()。 -
@nem 已经尝试过第一个,第二个不起作用,因为我必须返回值?
-
paint()方法具有void返回类型,因此在这两种情况下都不需要返回任何内容
标签: java swing class paint repaint