2

I am a newbie to programming, specially with Java. Got a book for self study and now I am little stuck. I know the issue but not sure how to resolve it. Code below -

    import javax.swing.JFrame;

public class RectangleViewer 
{
    public static void main(String args[]) 
    {
        JFrame frame = new JFrame();
        
        frame.setSize(300,400);
        frame.setTitle("Two Rectengle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        RectangleComponent component = new RectangleComponent(); /** <--- Error here
        frame.add(component);
        
        frame.setVisible(true);
    }
}

I am using Netbeans IDE 12. Error code pasted below...

C:\Users\marks\RectangleViewer.java:27: error: cannot find symbol
        RectangleComponent component = new RectangleComponent();
        ^
  symbol:   class RectangleComponent
  location: class RectangleViewer
C:\Users\marks\RectangleViewer.java:27: error: cannot find symbol
        RectangleComponent component = new RectangleComponent();
                                           ^
  symbol:   class RectangleComponent
  location: class RectangleViewer
2 errors
error: compilation failed

Thank you for your time and assistance.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
MarkSardar
  • 29
  • 2
  • 5
    There is no class `RectangleComponent` in the Java API. Probably the book defines that class somewhere, and you were supposed to type it in as well. – markspace Aug 11 '20 at 18:02
  • Also, Swing (Java's GUI classes, like `JFrame`) is not thread safe, and you have to use special ways of accessing them. Books that don't tell you this are imo not good books. Check the official tutorials: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html – markspace Aug 11 '20 at 18:05
  • 5
    Honestly asking someone new to programming to start reading up on concurrency is like throwing someone into the deep end of a pool when they've never even seen swimming pool before. Yes, it's important -- but not just yet. – Roddy of the Frozen Peas Aug 11 '20 at 18:06
  • start here https://docs.oracle.com/javase/specs/ – bananas Aug 11 '20 at 18:08
  • 1
    They should get used to it. They can copy paste code that is correct, and learn why they are doing it as they go along. The amount of boilerplate is minimal, esp. with lambdas and method references from Java 8. – markspace Aug 11 '20 at 18:09
  • Thank you for the tips, I haven't finished the entire book yet, not sure if the book talks about 'Class' later on. – MarkSardar Aug 11 '20 at 18:36

0 Answers0