【发布时间】:2014-10-03 14:02:27
【问题描述】:
当我尝试以下错误时 令牌“(”,;预期的语法错误 标记 ")" 的语法错误,;预计
在 ButtonTest.actionPerformed(ButtonTest.java:58)
import java.awt.*;
import java.awt.event.*;//step-1
import java.applet.Applet;
public class ButtonTest extends Applet implements ActionListener//step-2
{
Button b1,b2,b3;
Font f;
Graphics gc;
public void init()
{
b1=new Button("Request");
b2=new Button("Grant");
b3=new Button("Accept");
f=new Font("Arial",Font.BOLD,12);
b1.setFont(f);
b2.setFont(f);
b3.setFont(f);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
add(b1);
add(b2);
add(b3);
}
public void paint(Graphics gc)
{
gc.drawLine(100, 150, 100, 400);
gc.drawLine(300, 150, 300, 400);
gc.drawOval(95, 155, 10, 10); //1.1
gc.drawOval(95, 225, 10, 10); //1.2
gc.drawOval(95, 295, 10, 10); //1.3
gc.drawOval(95, 365, 10, 10); //1.4
gc.drawOval(295, 155, 10, 10); //2.1
gc.drawOval(295, 225, 10, 10); //2.2
gc.drawOval(295, 295, 10, 10); //2.3
gc.drawOval(295, 365, 10, 10); //2.4
}
public void myPaint(Graphics gc) // this line is not working*******???????
{
gc.drawLine(95, 155, 295, 225); //1.1 to 2.2
gc.drawLine(95, 295, 295, 225); //1.3 to 2.2
gc.drawLine(95, 295, 295, 365); //1.3 to 2.4
gc.drawString(">>>", 260, 220);
gc.drawString(">>>", 218, 255);
gc.drawString(">>>", 267, 365);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
myPaint(gc); //this line is not working
setBackground(Color.red);
}
else if(ae.getSource()==b2)
{
setBackground(Color.green);
}
else{
setBackground(Color.blue);
}
}
}
/*<applet code="ButtonTest" width=300 height=300>
*/
错误是 令牌“(”,;预期的语法错误 标记 ")" 的语法错误,;预计
在 ButtonTest.actionPerformed(ButtonTest.java:58)
【问题讨论】:
-
您使用哪种 Java。在方法
actionPerformed(ActionEvent ae)中定义另一个方法public void paint(Graphics gc)。这是不可能的 -
1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 为什么使用 AWT?请参阅 this answer 了解放弃 AWT 使用支持 Swing 的组件的许多充分理由。