CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: error code help

  1. #1
    Join Date
    Sep 2011
    Posts
    8

    Thumbs up error code help

    Hello everybody.
    I have met a problem ,the following java code can't be compiled successfully in one of my computer but can be succeed in another one.Does anyone could tell me what is the problem?

    I list the Code and Error message as below:
    thank you in advance!


    Code:
    //////////////////////////////////////Code://///////////////////////////////////////////////
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Win extends JFrame implements ItemListener
    { JComboBox list; 
      JTextArea text;
      Win()
       { list=new JComboBox(); 
         text=new JTextArea();
         Button b=new Button();
         b.setBackground(Color.yellow);
         text.setForeground(new Color(12,250,34));
         list.setForeground(Color.blue);
          list.setBackground(Color.cyan);
         GraphicsEnvironment ge=
         GraphicsEnvironment.getLocalGraphicsEnvironment();
         String fontName[]=ge.getAvailableFontFamilyNames();
         for(int i=0;i<fontName.length;i++)
         { list.addItem(fontName[i]);
         }
         add(list,BorderLayout.NORTH);
         add(new JScrollPane(text),BorderLayout.CENTER);
         list.addItemListener(this);
         setVisible(true); 
         setBounds(100,120,300,300);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
         validate();
       }
      public void itemStateChanged(ItemEvent e)
      {   String name=(String)list.getSelectedItem();
          Font f=new Font(name,Font.BOLD,32);
          text.setFont(f);
          text.setText("\n2008bj");
      }
    }
    public class Example8_18
    {   public static void main(String args[])
        {  Win win=new Win();
        }
    }
    ///////////////////////////////////Error Message:////////////////////////////////////////////

    Example8_18.java:11: setBackground(java.awt.Color) in java.awt.Component cannot
    be applied to (Color)
    b.setBackground(Color.yellow);
    ^
    Example8_18.java:12: enum types may not be instantiated
    text.setForeground(new Color(12,250,34));
    ^
    Example8_18.java:13: cannot find symbol
    symbol : method setForeground(Color)
    location: class javax.swing.JComboBox
    list.setForeground(Color.blue);
    ^
    Example8_18.java:14: cannot find symbol
    symbol : variable cyan
    location: class Color
    list.setBackground(Color.cyan);
    ^
    4 errors

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: error code help

    It would appear you have a Enum called Color on the classpath that the compiler is finding before it is finding java.awt.Color.

    You could either:

    1. Remove the Color Enum from the classpath (but only if it has nothing to do with this class).
    2. Add an import statement to explicitly import java.awt.Color ie
      Code:
      import java.awt.Color;
    3. Use the fully specified name wherever you use Color ie
      Code:
      list.setForeground(java.awt.Color.blue);
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured