java - 为什么它不交出我的东西?

标签 java swing oop object jframe

所以我试图交出一个物体,但它告诉我:

The method drawCommand(Launcher) in the type Command is not applicable for the arguments ()

and

a cannot be resolved to a variable.

import java.awt.Color;
import java.awt.Graphics;
import java.util.Scanner;

import javax.swing.JFrame;

public class Launcher extends JFrame {
    Launcher() {
        setSize(300, 400);
        setTitle("An Empty Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private int width = 1000;
    private int hight = 750;

    public static void main(String[] args) {

        Launcher a = new Launcher();
        a.repaint();
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        a.setTitle("Grafik");
        a.setSize(1000, 750);
        a.setVisible(true);
        a.operand();
    }

    public void paint(Graphics stift) {

        stift.drawString("A A A A A A A A A A ", 50, 50);
        stift.setColor(Color.LIGHT_GRAY);

        for (int i = 0; i < width; i = i + 10)
            stift.drawLine(i, 0, i, hight);

        for (int i = 0; i < hight; i = i + 10)
            stift.drawLine(0, i, width, i);
    }

    public void operand() {

        System.out.println("Bitte geben sie etwas ein: ");
        Scanner eingabe1 = new Scanner(System.in);
        String command = eingabe1.nextLine();
        // if(eingabe1!=null)
        // eingabe1.close();
        switch (command) {
        case "A":
            Command c = new Command();
            c.drawCommand(a);             //  here are the error messages
            System.out.println("draw fertig");
            // fenster.repaint();
            System.out.println("repaint fertig");
        }
    }
}

这是 a 的来源:

import java.awt.Color;
import java.awt.Graphics;
import java.util.Scanner;

public class Command extends Commands {

    private String text = "";


    public Command() {
        super();
        text = "";
    }


    public Command(int width, int hight, int[] cornerLocation, String text) {
        super(width, hight, cornerLocation);
        this.text = text;
    }

    public void drawCommand(Launcher a) {

        System.out.println("Bitte geben sie den Text ein: ");
        Scanner eingabe2 = new Scanner(System.in);
        text = eingabe2.nextLine();
//      if(eingabe2!=null)
//          eingabe2.close();

    }

    public void paint(Graphics stift) {

        stift.setColor(Color.RED);
        stift.drawString("Hasebraten ",150,150);
        stift.drawRect(cornerLocation[0], cornerLocation[1], width, hight);
    }

}

由于我对 JFrames 和 Graphics 还很陌生,所以我经常遇到错误,但我无法解决这个问题

最佳答案

在发生错误的代码中,作用域内没有变量 a,因此编译器会告诉您这一点。

  switch (command) {
    case "A":
        Command c = new Command();
        c.drawCommand(a);             //  a is not in scope here so this does not compile..
        System.out.println("draw fertig");
        // fenster.repaint();
        System.out.println("repaint fertig");
    }

但是,此时您确实有一个 Launcher 可用,因为相关类本身就是一个 Launcher

如果将代码更改为:

c.drawCommand(this);

然后,调用 Command 对象的 drawCommand 方法的 Launcher 对象会将其自身传递给 drawCommand code> 方法作为 Launcher

这将满足语法预期并消除此特定错误。

我不完全确定您打算让代码做什么,因此我不确定这是否能真正解决您的所有问题,但它肯定会让您克服该特定错误。

关于java - 为什么它不交出我的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30688611/

相关文章:

java - 在Java中,如何在切换到新的JTab之前捕获事件?

java - 处理java泛型处理的更好方法?

java - 根据循环中的位置替换字符

java - Jtable获取列标题组件

java - 为什么 JScrollpane 没有添加到我的文本编辑器中的 JTextArea 中?

java - 将行分割为单词数组

java - Weblogic,setDomainEnv.sh 类路径和 java 参数是否继承到托管服务器

javascript - 使用商店在 Svelte 中使类实例具有反应性

c# - 除了创建助手之外,创建静态方法的理想情况(现实生活中的例子)是什么?

java - 跟踪父类(super class)中子类的所有实例