简体   繁体   中英

Java “cannot find Symbol” when accessing method from another class [on hold]

For a homework we are supposed to create an Object Color with an attribute rgb, certain getters and setters methods, and another method which outputs a color value in #FFFFFF form. Color should be used as a parameter to a method, from a Class which, was supplied to us. I got all of the getters, setters and attributes to work as intended, but when I try to use the method we are supposed to I get:

$javac -cp . Color.java

Color.java:161: error: cannot find symbol
        ColorVisualizer(foo);
        ^
  symbol:   method ColorVisualizer(Color)
  location: class Color
1 error

I have tried setting my classpath, copying the supplied class and method into my code, running it on different machines, compiling the supplied class along with my code and I always receive the same error. I have double checked my spelling, I have double checked that the files are in the right directory and tried searching for answers on the Internet.

My Code:

class Color{

    private int rgb;

    public Color(int color){
        this.rgb = color;

    }

    public void Color(int rgb){
        this.rgb=rgb;
    }

    public int getRgb(){
        return this.rgb;
    }

    public String getHex(){//this method needs to be implemented for the supplied class to work, afaik
        return  "#FFFFFF";
    }

    public static void main(String[] args){

        Color foo= new Color(2000);
        ColorVisualizer(foo);//<<for some reason this doesn't want to cooperate
    }
}

The Class we were supplied with

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class ColorVisualizer extends JFrame {

    private static final long serialVersionUID = -3144218759873864161L;

    public ColorVisualizer(Color color) {
        setSize(400, 200);
        setTitle("Color Visualizer");

        getContentPane().setBackground(java.awt.Color.decode(color.getHex()));
        JLabel label = new JLabel("<html><span style='font-size:20px'>" + color.getHex() + "</span></html>",
                SwingConstants.CENTER);
        add(label);

        setVisible(true);
        setFocusable(true);
        requestFocus();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

The question stated that the.getHex() method in my code needs to be implemented for the GUI to launch, which to my knowledge, I have done.

EDIT: I didn't call ColorVisualizer using the "new" keyword. Works like a charm now

Thanks to @rkosegi,

The problem was that I thought ColorVisualizer was a method not a Constructor.

Had to add "new" keyword to create the Object and everything worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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