2
import javax.swing.*; // For JPanel, etc.
import java.awt.*;           // For Graphics, etc.
import java.awt.geom.*;      // For Ellipse2D, etc.
import java.awt.event.*; 


public class ShapeExample extends JPanel {
  private Rectangle2D.Double square =
    new Rectangle2D.Double(50, 50, 100, 100);
  private Rectangle2D.Double square1 =
    new Rectangle2D.Double(10, 10, 200, 200);

  public void paintComponent(Graphics g) {
    clear(g);
    Graphics2D g2d = (Graphics2D)g;
    g2d.fill(square);
    g2d.draw(square1);
  }
 g.add(b = new Button("Bonjour"), BorderLayout.NORTH); 
    b.setActionCommand("Good Morning"); 
    b.addActionListener(a); 
    g.add(b = new Button("Good Day"), BorderLayout.CENTER); 
    b.addActionListener(a); 
    g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 
    b.setActionCommand("Exit"); 
    b.addActionListener(a); 
    g.pack(); 
    g.show(); 
} 
  // super.paintComponent clears offscreen pixmap,
  // since we're using double buffering by default.

  protected void clear(Graphics g) {
    super.paintComponent(g);
  }

  protected Rectangle2D.Double getsquare() {
    return(square);
  }

  public static void main(String[] args) {
    WindowUtilities.openInJFrame(new ShapeExample(), 100, 100);
    Button b; 

  }
}

How to debug this code?

The error I get is this

ShapeExample.java:19: <identifier> expected
 g.add(b = new Button("Bonjour"), BorderLayout.NORTH); 
      ^
ShapeExample.java:19: <identifier> expected
 g.add(b = new Button("Bonjour"), BorderLayout.NORTH); 
        ^
ShapeExample.java:19: ';' expected
 g.add(b = new Button("Bonjour"), BorderLayout.NORTH); 
          ^
ShapeExample.java:19: invalid method declaration; return type required
 g.add(b = new Button("Bonjour"), BorderLayout.NORTH); 
               ^
ShapeExample.java:19: illegal start of type
 g.add(b = new Button("Bonjour"), BorderLayout.NORTH); 
                      ^
ShapeExample.java:19: ';' expected
 g.add(b = new Button("Bonjour"), BorderLayout.NORTH); 
                                ^
ShapeExample.java:20: <identifier> expected
    b.setActionCommand("Good Morning"); 
                      ^
ShapeExample.java:20: illegal start of type
    b.setActionCommand("Good Morning"); 
                       ^
ShapeExample.java:21: <identifier> expected
    b.addActionListener(a); 
                       ^
ShapeExample.java:21: <identifier> expected
    b.addActionListener(a); 
                         ^
ShapeExample.java:22: <identifier> expected
    g.add(b = new Button("Good Day"), BorderLayout.CENTER); 
         ^
ShapeExample.java:22: <identifier> expected
    g.add(b = new Button("Good Day"), BorderLayout.CENTER); 
           ^
ShapeExample.java:22: ';' expected
    g.add(b = new Button("Good Day"), BorderLayout.CENTER); 
             ^
ShapeExample.java:22: invalid method declaration; return type required
    g.add(b = new Button("Good Day"), BorderLayout.CENTER); 
                  ^
ShapeExample.java:22: illegal start of type
    g.add(b = new Button("Good Day"), BorderLayout.CENTER); 
                         ^
ShapeExample.java:22: ';' expected
    g.add(b = new Button("Good Day"), BorderLayout.CENTER); 
                                    ^
ShapeExample.java:23: <identifier> expected
    b.addActionListener(a); 
                       ^
ShapeExample.java:23: <identifier> expected
    b.addActionListener(a); 
                         ^
ShapeExample.java:24: <identifier> expected
    g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 
         ^
ShapeExample.java:24: <identifier> expected
    g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 
           ^
ShapeExample.java:24: ';' expected
    g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 
             ^
ShapeExample.java:24: invalid method declaration; return type required
    g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 
                  ^
ShapeExample.java:24: illegal start of type
    g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 
                         ^
ShapeExample.java:24: ';' expected
    g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 
                                    ^
ShapeExample.java:25: <identifier> expected
    b.setActionCommand("Exit"); 
                      ^
ShapeExample.java:25: illegal start of type
    b.setActionCommand("Exit"); 
                       ^
ShapeExample.java:26: <identifier> expected
    b.addActionListener(a); 
                       ^
ShapeExample.java:26: <identifier> expected
    b.addActionListener(a); 
                         ^
ShapeExample.java:27: <identifier> expected
    g.pack(); 
          ^
ShapeExample.java:28: <identifier> expected
    g.show(); 
          ^
ShapeExample.java:33: class, interface, or enum expected
  protected void clear(Graphics g) {
            ^
ShapeExample.java:35: class, interface, or enum expected
  }
  ^
ShapeExample.java:39: class, interface, or enum expected
  }
  ^
ShapeExample.java:41: class, interface, or enum expected
  public static void main(String[] args) {
                ^
ShapeExample.java:43: class, interface, or enum expected
    Button b; 
    ^
ShapeExample.java:45: class, interface, or enum expected
  }

how to debug this?

3
  • Remember that for Java if an instruction isn't defining a constant, it needs to live inside of a method or the compiler can't understand you -- it's not like Ruby where you can sort of do anything anywhere (though of course that's not quite the case even in Ruby.) Please note that there are several errors, of which the compiler is handily enumerating several. I might consider working through to try to reorganize your code -- perhaps conceptually, on paper -- into a group of well-defined methods to accomplish the goal. Commented Jan 3, 2011 at 19:58
  • 2
    I suggest you use an IDE and use its code formatting, it would make this error really obvious and easy to fix. Commented Jan 3, 2011 at 19:59
  • I would suggest you try building your GUI using your IDE's GUI builder which allows you to create it graphically before you work out how to create the code by hand. i.e. you can create the whole thing with just a few lines of code. Commented Jan 3, 2011 at 20:01

2 Answers 2

4

You have code randomly strewn about inside your class. All non-declaration code within a class must be within a method of some sort. Do you mean that code to go inside the paintComponent method? if so, you've got an extraneous }

Change that method to:

public void paintComponent(Graphics g) {
    clear(g);
    Graphics2D g2d = (Graphics2D)g;
    g2d.fill(square);
    g2d.draw(square1);
    Button bonjourButton = new Button("Bonjour");
    g.add(bonjourButton, BorderLayout.NORTH); 
    b.setActionCommand("Good Morning"); 
    b.addActionListener(a);
    Button goodDayButton = new Button("Good Day"); 
    g.add(goodDayButton, BorderLayout.CENTER); 
    b.addActionListener(a); 
    Button aurevoirButton = new Button("Aurevoir");
    g.add(aurevoirButton, BorderLayout.SOUTH); 
    b.setActionCommand("Exit"); 
    b.addActionListener(a); 
    g.pack(); 
    g.show(); 
} 

And also note that I am using local Button instances instead here as well.

edit: and also you'll need to define the action listener a as there's nothing named a anywhere in the code, much less in the scope of this function.

Sign up to request clarification or add additional context in comments.

3 Comments

I did all the necessary changes . And The error still remains .
If it's the exact same error, then either you've done the changes improperly or you didn't paste the original code correctly or something. Take the recommendations of some commenters and use a proper IDE which will help you.
I am using gedit . I just have a simple doubt. I have a window which displays a particular shape , here it is a circle . How do i show a few buttons on it ? Like radio-button and check-box .
0

The main mistake is here:

g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH); 

either you should use:

 g.add(new Button("Aurevoir"), BorderLayout.SOUTH);

or confirming to your remaing code:

Button b = new Button("Aurevoir")
 g.add(b, BorderLayout.SOUTH); 

This will maybe also solve the other marked errors.

1 Comment

The main mistake is the extraneous } in the paintComponent method, the out of scope Button would be the next thing to trigger a compile error once he fixed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.