Вопрос

I am having a little trouble with a bluej lab I'm doing for AP Computer Science.

I am supposed to make an American Flag using ASCII characters.
I have a skeleton code my teacher gave us, but we are supposed to fill it in. I managed to make the program, but I cant test it because the runner (which was given to us fully filled out) is protesting my getContentPane().add(new StarsAndStripesLab()); It says:

No suitable method found for add(StarsAndStripesLab)

method java.awt.Container.add(java.awt.Component) is not applicable (actual argument StarsAndStripesLab cannot be converted to java.awp.Component by method invocation conversion)

method java.awp.Component.add(java.awp.PopupMenu) is not applicable (actual argument StarsAndStripesLab cannot be converted to java.awt.PopupMenu by method invocation conversion)

Here is the whole code for my runner and lab:

Lab:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Canvas;
import java.awt.Font;
import static java.lang.System.*;

public class StarsAndStripesLab
{
   public StarsAndStripesLab()
   {
       out.println("StarsAndStripesLab");
       printTwoBlankLines();
   }

   public void printTwentyStars()
   {
      out.println("* * * * * *");
      out.println(" * * * * * ");
      out.println("* * * * * *");
      out.println(" * * * * * ");
      out.println("* * * * * *");
      out.println(" * * * * * ");
      out.println("* * * * * *");
      out.println(" * * * * * ");
      out.println("* * * * * *");
   }

   public void printTwentyDashes()
   {
       out.println("--------------------");
   }

   public void printTwoBlankLines()
   {
       out.println("\n\n");
   }

   public void printASmallBox()
   {    
       out.println("|--------------|");
       out.println("|              |");
       out.println("|              |");
       out.println("|              |");
       out.println("|--------------|");
   }

   public void printABigBox()
   {    
       out.println("|-----------------------------|");
       out.println("|                             |");
       out.println("|                             |");
       out.println("|                             |");
       out.println("|                             |");
       out.println("|                             |");
       out.println("|                             |");
       out.println("|                             |");
       out.println("|                             |");
       out.println("|                             |");
       out.println("|-----------------------------|");
   }   
}

Here is the runner (I put asterisks around where its giving me trouble since I cant highlight):

import javax.swing.JFrame;



public class StarsAndStripesLabRunner extends JFrame

{

 private static final int WIDTH = 800;

 private static final int HEIGHT = 600;

    public StarsAndStripesLabRunner()
    {
        super("Keelen Berkenkotter");
        setSize(WIDTH,HEIGHT);

        ***getContentPane().add(new StarsAndStripesLab());***

        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

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

Any help is appreciated. Thanks in advance.

-Keelen Berkenkotter

Это было полезно?

Решение

I think you are either using the wrong runner code or you misunderstood the task. Your code is printing the characters on the terminal, but the runner code is a graphical program that sets up a window.

You either have to use a runner code that calls all your methods, such as this:

public class StarsAndStripesLabRunner {
    public static void main( String args[] )
    {
        StarsAndStripesLab stars = new StarsAndStripesLab();
        stars.printTwentyStars(); // and all the other methods in correct order
    }
}

Or you have to write a program that displays the flag graphically. But I think it's the former, because you said you have to use ASCII characters.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top