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;
}
}
如何调试此代码?
我得到的错误就是这个
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
}
如何调试?
答案 0 :(得分:4)
你的课程中随机散布的代码。类中的所有非声明代码必须位于某种方法中。你的意思是代码进入paintComponent
方法吗?如果是这样,你就会得到一个无关的}
将该方法更改为:
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();
}
另请注意,我也在使用本地Button
实例。
编辑:您还需要定义动作侦听器a
,因为代码中的任何地方都没有名称a
,更不用说此功能的范围了
答案 1 :(得分:0)
主要的错误在于:
g.add(b = new Button("Aurevoir"), BorderLayout.SOUTH);
你应该使用:
g.add(new Button("Aurevoir"), BorderLayout.SOUTH);
或确认您的剩余代码:
Button b = new Button("Aurevoir")
g.add(b, BorderLayout.SOUTH);
这也可能解决其他标记错误。