简体   繁体   English

不兼容的类型错误Java

[英]Incompatible types error Java

So, I have this line of code 所以,我有这行代码

ScreenCapture.main(String[].class);

in file "1" and it is linking to this file "2" 在文件“ 1”中并链接到该文件“ 2”

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

class ScreenCapture
{
   public static void main (String args[]) throws
        AWTException, IOException
   {
      System.out.print(".");
      BufferedImage screencapture = new Robot().createScreenCapture(
            new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
      Expo.delay(2000);
      System.out.print(".");
      File file = new File("Agreement.jpg");
      ImageIO.write(screencapture, "jpg", file);
      Expo.delay(4000);
      System.out.print(".");
   }
}

and this is the error I get in file 1 这是我在文件1中得到的错误

WarandPeace.java:21: error: incompatible types: Class<String[]> cannot be
converted to String[]

My end goal is to take a screenshot of the screen when the user completes a specific action in file 1. I have the screenshot file working (file 2), but no matter what I do, that error keeps being annoying (file 1). 我的最终目标是在用户完成文件1中的特定操作后截取屏幕截图。我使屏幕快照文件(文件2)正常工作,但是无论我做什么,该错误始终令人讨厌(文件1)。 Any solutions? 有什么办法吗?

Your error is self-explanatory. 您的错误是不言自明的。 A String array class is not the same as a String array itself. String数组类与String数组本身不同。 But more importantly, that code should not be in a static main method if you want other code to use it since the main method should be used for starting a program, not for utility methods. 但更重要的是,如果您希望其他代码使用该代码,则该代码不应位于静态main方法中,因为应将main方法用于启动程序,而不是用于实用程序方法。 Learn proper OOPS concepts, create classes and call non-static methods of proper objects. 学习适当的OOPS概念,创建类并调用适当对象的非静态方法。 If this were my code, I'd create a method to capture the screen and have it return a BufferedImage. 如果这是我的代码,我将创建一个方法来捕获屏幕并让它返回BufferedImage。 Then other code can decide what to do with the BufferedImage. 然后其他代码可以决定如何处理BufferedImage。

The method in file 2 has this signature: 文件2中的方法具有以下签名:

public static void main (String args[]) throws
        AWTException, IOException

You need to match it. 您需要匹配它。 You can do eg this: 您可以这样做:

ScreenCapture.main(new String[] {"param1", "param2"});

or, since you are not using the arguments at all, you can just: 或者,由于您根本不使用参数,因此您可以:

ScreenCapture.main(new String[] {});

I agree with the other answer though... 我同意其他答案...

Modify "file 1". 修改“文件1”。

ScreenCapture.main(String[].class);

should be 应该

String[] args = { "one" }; // <- for one.

or 要么

String[] args = new String[0]; // <- for none.

and then 接着

ScreenCapture.main(args);

main() takes an array of String (s) (not the Class<String[]> which is the type of a String[] ). main()采用的阵列String (一个或多个)(未在Class<String[]>这是一个的类型String[]

Finally, on one line - 最后,在一行上-

ScreenCapture.main(new String[] {"one", "two"});

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

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