JAVA GUI代码没有';I don’我没有按预期运行

JAVA GUI代码没有';I don’我没有按预期运行,java,user-interface,Java,User Interface,我从书上输入了这个代码 public class SimpleGui3C { JFrame frame; public static void main (String[] args) { SimpleGui3C gui = new SimpleGui3C(); gui.go(); } public void go() { frame = new JFrame("My own code"); frame.setDefaultCloseOper

我从书上输入了这个代码

public class SimpleGui3C  {

JFrame frame;


public static void main (String[] args) {
   SimpleGui3C gui = new SimpleGui3C();
   gui.go();
}

public void go() {
   frame = new JFrame("My own code");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  
   MyDrawPanel drawPanel = new MyDrawPanel();

   frame.getContentPane().add(BorderLayout.CENTER, drawPanel);

   frame.setSize(420,300);
   frame.setVisible(true);
}}

class MyDrawPanel extends JPanel {

public void painComponent(Graphics g) {
    
    g.fillRect(0,0,this.getWidth(), this.getHeight());

    
    int red = (int) (Math.random() * 255);
    int green = (int) (Math.random() * 255);
    int blue = (int) (Math.random() * 255);

    Color randomColor = new Color(red, green, blue);
    g.setColor(randomColor);
    g.fillOval(70,70,100,100);
}}
结果是:

这是这本书的原始代码,我是从这本书的网站上下载的

public class SimpleGui3C  {

JFrame frame;

public static void main (String[] args) {
   SimpleGui3C gui = new SimpleGui3C();
   gui.go();
}

public void go() {
   frame = new JFrame();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  
   MyDrawPanel drawPanel = new MyDrawPanel();
   

   frame.getContentPane().add(BorderLayout.CENTER, drawPanel);


   frame.setSize(420,300);
   frame.setVisible(true);
}}

class MyDrawPanel extends JPanel {

  public void paintComponent(Graphics g) {
     
     g.fillRect(0,0,this.getWidth(), this.getHeight());

     // make random colors to fill with
     int red = (int) (Math.random() * 255);
     int green = (int) (Math.random() * 255);
     int blue = (int) (Math.random() * 255);

     Color randomColor = new Color(red, green, blue);
     g.setColor(randomColor);
     g.fillOval(70,70,100,100);
  }}
结果如下:

我一次又一次地检查以确保它们是一样的,看起来它们是一样的。但是为什么GUI不同呢


请帮助,提前感谢。

在您自己的代码中,方法名称是
painComponent
,而它应该是
painComponent

提示:将
@Override
添加到重写的方法中,编译器将告诉您以下错误:

@Override
public void painComponent(Graphics g) {

在您自己的代码中,方法名称应该是
painComponent
,而方法名称应该是
paintComponent

提示:将
@Override
添加到重写的方法中,编译器将告诉您以下错误:

@Override
public void painComponent(Graphics g) {

事实上,
@Override
使事情变得不那么痛苦。查看问题了解有关
@Override
的更多信息确实,
@Override
使事情变得不那么痛苦。查看问题了解有关
@Override
的更多信息这是一个打字错误:painComponent这是一个打字错误:painComponent