Java 如何正确导入stddraw?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29136723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:25:50  来源:igfitidea点击:

How to import stddraw correctly?

javaintellij-ideaimportstddraw

提问by Morgantuan

Editor: IntelliJ CE

编辑:IntelliJ CE

What I want: Be able to write

我想要的是:会写

setCanvas(500,500);

Instead of

代替

StdDraw.setcanvas(500,500);

Problem: I can't figure out how to correctly import the Stddraw library. If i simply do

问题:我不知道如何正确导入 Stddraw 库。如果我只是这样做

import StdDraw;

IntelliJ tells me "StdDraw" symbol cannot be resolved. If I comment it out I can call methods from StdDraw but I have to write StdDraw.setcanvas(500,500);

IntelliJ 告诉我无法解析“StdDraw”符号。如果我将其注释掉,我可以从 StdDraw 调用方法,但我必须编写 StdDraw.setcanvas(500,500);

StdDraw.java is in the same directory as Solver.java.

StdDraw.java 与 Solver.java 位于同一目录中。

Code:

代码:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
//    import StdDraw;//StdDraw is in the same directory as Solver

public class Solver {

    public static void main(String[] args) {
        System.out.println("Solver main is running.");

        StdDraw.setCanvasSize(500, 500);
        StdDraw.setPenColor(StdDraw.RED);
        StdDraw.filledRectangle(0,0,10,10);
     }
}

I've already tried: - Making sure Stddraw.java is in the same directory as the file I'm compiling and running - Looking at http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html- Searching for COMPLETE code examples, ie. code that shows how to import the library - Searching YouTube tutorials - Reading https://www.jetbrains.com/idea/help/library.html- Fiddling around with adding stuff in front of StdDraw, eg. stblib.StdDraw

我已经尝试过: - 确保 Stddraw.java 与我正在编译和运行的文件在同一目录中 - 查看http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html- 搜索完整的代码示例,即。显示如何导入库的代码 - 搜索 YouTube 教程 - 阅读https://www.jetbrains.com/idea/help/library.html- 在 StdDraw 前面摆弄添加东西,例如。stblib.StdDraw

回答by Ron

I use StdDraw all the time

我一直使用 StdDraw

Under your package declaration, type:

在您的包声明下,键入:

import stddraw.StdDraw;

then all the stuff you need to do should work, also make sure the actual class is inside of your file correctly

那么你需要做的所有事情都应该工作,还要确保实际的类正确地在你的文件中

回答by Amegar

Add this import to your class.

将此导入添加到您的课程中。

import static StdDraw.*;

What it means is that all static methods of the StdDrawclass can be used without prefixing them with StdDraw.It also assumes that StdDrawclass is in the default package, which is generally frowned upon but appears to be what that library has done.

这意味着StdDraw可以使用该类的所有静态方法,而无需为它们添加前缀StdDraw.它还假定StdDraw该类位于默认包中,这通常令人不悦,但似乎是该库所做的。

回答by Ph?m D??ng

You can download the library stdlib.jar here: http://introcs.cs.princeton.edu/java/stdlib/

您可以在此处下载库 stdlib.jar:http://introcs.cs.princeton.edu/java/stdlib/

Then import it follow this tutorial:https://stackoverflow.com/a/32853178/2048865

然后按照本教程导入:https: //stackoverflow.com/a/32853178/2048865

回答by William Hou

You said:

你说:



 What I want: Be able to write

     setCanvas(500,500);

 Instead of

     StdDraw.setcanvas(500,500);


Isn't that against the basic rules of Java?

这不是违反Java的基本规则吗?

You cannot write

你不能写

    setCanvas(500,500);

unless you are inside the "StdDraw" class where other methods of the "StdDraw" class call the "setCanvas" method.

除非您在“StdDraw”类中,“StdDraw”类的其他方法调用“setCanvas”方法。

Otherwise, either you have to create an instance of the "StdDraw" class first:

否则,您必须首先创建“StdDraw”类的实例:

    e.g. StdDraw stdDraw = new StdDraw();

and then use that instance to call the method:

然后使用该实例调用该方法:

    e.g. stdDraw.setCanvas(500,500); 

or you call the method this way:

或者你这样调用方法:

    StdDraw.setcanvas(500,500);

This is the basic knowledge of Java, right?

这是Java的基础知识吧?

By the way, if the "StdDraw" class is in the same directory as class "Solver", you don't have to import it to use it.

顺便说一句,如果“StdDraw”类与“Solver”类在同一目录中,则不必导入它即可使用它。

I use eclipse. I put class "StdDraw" in the same package with other classes. This way, I don't have to use the "import" key word to import "StdDraw". I just use the methods of "StdDraw" the static way. You import it only when it is not in the same package.

我使用日食。我将类“StdDraw”与其他类放在同一个包中。这样,我不必使用“导入”关键字来导入“StdDraw”。我只是以静态方式使用“StdDraw”的方法。仅当它不在同一个包中时才导入它。

FYI: I'm reading Robert Sedgewick's "Algorithms", in which I've never seen any direct calls to methods like the way you want:

仅供参考:我正在阅读 Robert Sedgewick 的“算法”,其中我从未见过像您想要的那样直接调用方法:

 uniform(N-i); or 
 printf("%.2f\n", x); or
 point(x0, y0); or
 line(x0, y0, x1, y1); or
 circle(x, y, r); or
 square(x, y, r); or
 polygon(x, y); etc. etc....

Instead, it's always:

相反,它总是:

 StdRandom.uniform(N-i); or 
 StdOut.printf("%.2f\n", x); or
 StdDraw.point(x0, y0); or
 StdDraw.line(x0, y0, x1, y1); or
 StdDraw.circle(x, y, r); or
 StdDraw.square(x, y, r); or
 StdDraw.polygon(x, y); etc. etc....

I hope this helps.

我希望这有帮助。

回答by Jozott

You need to add Stdlib to your local libraries of your java project. StdDraw is part of this Stdlib library.

您需要将 Stdlib 添加到您的 Java 项目的本地库中。StdDraw 是这个 Stdlib 库的一部分。

  1. First you need to downloadthe stdlib.jar file
  2. Then you create a folder in your java project (name it "lib")
  3. Copy & Paste the stdlib.jar inside the lib folder
  4. Open your java project with IntelliJ.
  5. Click File -> Project Structure -> Modules -> Dependencies
  6. Click on the + sign and choose Library -> Java
  7. Then you need to select your stdlib.jar inside your lib folder
  1. 首先你需要下载stdlib.jar文件
  2. 然后你在你的java项目中创建一个文件夹(命名为“lib”)
  3. 将 stdlib.jar 复制并粘贴到 lib 文件夹中
  4. 使用 IntelliJ 打开您的 Java 项目。
  5. 单击文件 -> 项目结构 -> 模块 -> 依赖项
  6. 单击 + 号并选择 Library -> Java
  7. 然后你需要在你的 lib 文件夹中选择你的 stdlib.jar

Now you can use the StdDraw class. You don't need to import the class at the top of the file.

现在您可以使用 StdDraw 类。您不需要在文件顶部导入类。