HI! I want to write a constructor that will make a window (a frame). I want to be able to insert the height, width, position and window color. This is what I've written:

public Window(String name, int height, int width,String c,String isActive,int PointX, int PointY){


        setTitle(name);
        setSize(height,width);
        getContentPane().setBackground(Color.c);

        setLocation(PointX,PointY);

        setVisible(isActive.equals("active"));


        setDefaultCloseOperation(EXIT_ON_CLOSE);    

    }

My problem is that when compiling, I get a message "could not find symbol-variable c". So I can't figure out how to declare the color that I want my window to be. Does anybody know how to do this?

I see what you are trying to do. You are trying to access the static members of java.awt.Color such as Color.red or Color.blue except that you are trying to do it with a String that contains the words "red" or "blue". You cannot use the dot notation that way, and in general you cannot ever just substitute a String in place of the text that it contains in Java. Accessing fields using a String containing the name of the field would only be possible using reflection, but you would be far better to pass a Color into your constructor rather than a String.

If you must take a color name rather than a color, then I would use a java.util.Map<String,Color> to build up a library of as many colors as I liked and do the conversion that way. There is also Color.getColor(String name) which allows colors to be accessed by name from system properties.

Thanks for the response! I'll see what I can do with the Color.getColor(String name)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.