简体   繁体   English

超类不使用子类方法

[英]Superclass not using subclass method

My Superclass is failing to use the method I created in my subclass.我的超类无法使用我在子类中创建的方法。 I get an error because I'm using the Graphics argument.我收到一个错误,因为我使用的是 Graphics 参数。 What am I missing here?我在这里缺少什么?

I've tried the suggestions given by eclipse, however they result in more errors.我已经尝试过 eclipse 给出的建议,但是它们会导致更多的错误。

Here's my super class这是我的超级班

  import java.awt.Canvas;
  import java.awt.Color;
  import java.awt.Graphics;
  import javax.swing.JFrame;
public class GameScreen extends Canvas{

public GameScreen() {

}

public static void main(String[] args) {
    // TODO Auto-generated method stub
//Sets Screen
    JFrame jframe = new JFrame("Game Screen");
    Canvas canvas = new GameScreen();
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvas.setBackground(Color.cyan);
        canvas.setSize(1000,800);
        jframe.add(canvas);
        jframe.pack();
        jframe.setVisible(true);

        Paddle1.paint(Graphics g);

}



 }

And here's my subclass这是我的子类

    import java.awt.Color;
    import java.awt.Graphics;
public class Paddle1 extends GameScreen {

public void paint(Graphics g) {
    g.setColor(Color.white);
    g.fillRect(50, 50, 40, 130);
}
 }

The subclass creates a rectangle that is supposed to appear on top of the canvas子类创建一个应该出现在画布顶部的矩形

If you would like to trigger method "paint" of Paddle1, you have to modify your code as below:如果你想触发 Paddle1 的方法“paint”,你必须修改你的代码如下:

JFrame jframe = new JFrame("Game Screen");
Canvas canvas = new Paddle1();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.cyan);
canvas.setSize(1000,800);
jframe.add(canvas);
jframe.pack();
jframe.setVisible(true);
canvas.paint(g);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM