gpt4 book ai didi

java - 如何调试此 java 代码?这段代码有什么错误?

转载 作者:行者123 更新时间:2023-11-30 07:35:17 25 4
gpt4 key购买 nike

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
}

如何调试?

最佳答案

您的类(class)中随机散布着代码。类中的所有非声明代码都必须在某种方法中。您的意思是将代码放入 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 实例。

编辑:您还需要定义 Action 监听器a,因为代码中的任何地方都没有名为a 的内容,更不用说在此功能范围内。

关于java - 如何调试此 java 代码?这段代码有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4587807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com