代码是这样的:
import javax.swing.*;
import java.awt.*;
class NoHelloWorldPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("not a hello world program",MESSAGE_X,MESSAGE_Y);
public static final int MESSAGE_X = 75;
public static final int MESSAGE_Y = 100;
}
}
class NoHelloWorldFrame extends JFrame
{
public NoHelloWorldFrame()
{
setTitle("Not Hello World!!");
setSize(WIDTH,HEIGHT);
NoHelloWorldPanel panel = new NoHelloWorldPanel();
Container contentpane = getContentPane();
contentpane.add(panel);
}
public static final int WIDTH= 300;
public static final int HEIGHT = 200;
}
public class NoHelloWorld
{
public static void main(String [] args)
{
NoHelloWorldFrame frame = new NoHelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
以上代码在编译的时候提示如下错误:
illegal start of expression public static final int MESSAGE_X = 75;
illegal start of expression public static final int MESSAGE_Y= 100
但是如果把代码中划线的两行注释了。改为 g.drawString("not a hello world program",70,100);
是可以通过编译并运行的。
这是怎么回事啊??