因此,我正在遵循一个教程,并且我完全按照它进行了学习,但是当我进行测试时,它会抛出一个错误。

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code -   constructor Main in class spritesheet.Main cannot be applied to given types;
  required: java.awt.Color
  found: no arguments
  reason: actual and formal argument lists differ in length
at spritesheet.Main.main(Main.java:48)
Java Result: 1


错误!

这是我的代码:

package spritesheet;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class Main extends JFrame {

BufferedImage sprite;
  private final Color Color;

public Main(Color white){
    setSize(800, 600);
    setVisible(true);
    setResizable(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setBackground(Color=white);
    init();
}


private void init(){
    BufferedImageLoader loader = new BufferedImageLoader();
    BufferedImage spriteSheet = null;
    try {
        spriteSheet = loader.loadImage("spritesheet.png");
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    SpriteSheet ss = new SpriteSheet(spriteSheet);

    sprite = ss.grabSprite(0, 0, 16, 16);
}

@Override
public void paint(Graphics g){
    g.drawImage(sprite, 100, 100, null);
    repaint();

}

public static void main(String[] args) {
    Main Main = new Main();
}
}


此代码旨在在白色背景上显示静止图像,但是它会引发错误或显示透明背景。然后它臭虫出炉,使我烦恼。 :(
我究竟做错了什么????

最佳答案

你这样叫课

Main Main = new Main();


但是您需要向其传递参数,因为您将其定义为

public Main(Color white) { ... }


您可以将其更改为

Main Main = new Main(Color.WHITE);




public Main(Color c) {
   setSize(800, 600);
   setVisible(true);
   setResizable(true);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setLocationRelativeTo(null);
   Color = c;
   setBackground(Color);
   init();
}


编辑

您现在可以做两件事。像这样使您的Color变量static

private static Color Color;


或将您的Main构造函数更改为

public Main() {
   setSize(800, 600);
   setVisible(true);
   setResizable(true);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setLocationRelativeTo(null);
   Color = Color.WHITE;
   setBackground(Color);
   init();
}

关于java - 实际和正式论点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10860203/

10-13 03:42