为什么要把它交给我的对象?

时间:2015-06-06 23:34:04

标签: java swing oop object jframe

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

  

类型Command中的方法drawCommand(Launcher)不是   适用于arguments()

     

     

a无法解析为变量。

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");
        }
    }
}

这是来自的地方:

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的新手,我经常会遇到错误,但我无法解决这个问题

2 个答案:

答案 0 :(得分:3)

在这里发生错误的代码中,范围内没有变量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);

然后,调用Launcher对象的drawCommand方法的Command对象将自己作为drawCommand传递给Launcher方法。< / p>

这将符合语法预期并超越此特定错误。

我不完全确定您打算如何编写代码,因此我不确定这是否会真正解决您的所有问题,但它肯定会让您超越该特定错误。

答案 1 :(得分:0)

Launcher a超出了operand()功能的范围。您需要向Launcher添加operand()参数并传递启动器。

相关问题