0
import java.awt.*;
import javax.swing.JComponent;
public class HelloComponent {
  class HelloComponent extends JComponent{
    public void paintComponent( Graphics g) {
      g.drawString("Hello,Java!", 125, 95);
    }
}
  1. The error I get back is (The nested type "HelloComponent" cannot hide an enclosing type) I have tried changing the brackets around but I still get the same error back. I also tried to change my other class that contains the JFrame, from frame.add(text) ( text is my var for the label) to frame.add( new HelloComponent()); ( as the book Im reading instructed me to do, but I still get an error)) P.S. I am trying to make my JFrame text using drawString operation.

P.P.S. I started learning Java yesterday

P.P.P.S I already created a class for the actual Jframe

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
Ridhwaan
  • 57
  • 8
  • You declared two class of the name `public class HelloComponent { class HelloComponent extends JComponent {`...may try `public class HelloComponent extends JComponent {` instead – MadProgrammer Apr 26 '15 at 23:53
  • Before you get to far down the rabbit hole, you might consider having a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/), as you are breaking the paint call chain, which will come back to haunt you... – MadProgrammer Apr 26 '15 at 23:55
  • A great trick I I learned for checking brackets is to add 1 for every `{` and subtract 1 for every `}`. If you ever reach negative, you need to add some more `{` brackets. If at the end you are positive, you need to add some more `}`. – Obicere Apr 26 '15 at 23:55
  • Beautiful answers, thanks guys! much appreciated. Fixed the problem – Ridhwaan Apr 28 '15 at 01:07

2 Answers2

1

Seems like you are trying to redeclare HelloComponent class within a class by the same name!

Try:

import java.awt.*;
import javax.swing.JComponent;
public class HelloComponent extends JComponent {
    public void paintComponent( Graphics g) {
        g.drawString("Hello,Java!", 125, 95);
    }
}
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
0

You have 2 Class HelloComponent declarations, delete the class declaration that does not extend JComponent and the correspond bracket to get rid of the error.

AlecR
  • 57
  • 7