How to debug this java code ? What is the error in this code?
import javax.swing.*; // For JPanel, etc.
import java.awt.*; // For Graphics, etc.
im开发者_开发知识库port 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?
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.
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.
精彩评论