我有一些Java,从理论上讲,它从500x500窗口的左上角到右下角绘制一条线。代码:
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.geom.*;
@SuppressWarnings("serial")
public class graphix extends JFrame{
public static void main(String[] args){
new graphix();
}
public graphix(){
this.setSize(500, 500);
this.setTitle("Title Here");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new Draw(), BorderLayout.CENTER);
this.setVisible(true);
}
private class Draw extends JComponent{
public void paint(Graphics g){
Graphics2D graph2 = (Graphics2D)g;
graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape drawLine = new Line2D.Float(10, 10, 490, 490);
graph2.setPaint(Color.BLACK);
graph2.draw(drawLine);
}
}
}
当我运行此代码时,eclipse会给我错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at graphix.graphix.main(graphix.java:11)
通常在eclipse中,错误消息将显示在这两行之间的空白区域中,但它表示错误是在第11行触发的,这是
public static void main(String[] args){
我的项目名称是graphix,类文件名是graphix.java
修改
还有一个非致命错误:
The declared package "" does not match the expected package "graphix"
触发:
import javax.swing.JComponent;
答案 0 :(得分:0)
文件顶部缺少包声明,它必须与项目中的文件结构匹配:
package xxx.yyy.zzz;