【发布时间】:2014-11-29 14:28:11
【问题描述】:
我从讲师那里得到了 3 节课作为作业,在我完全编辑它们之前,我尝试编译它们。它们不会在引用“DrawPanel panel = new DrawPanel();”时引用“cannot find symbol error”的命令提示符进行编译。然后我尝试在 Eclipse 中运行它,它运行良好,任何关于为什么甚至如何让它在我的命令提示符下工作的想法
TestDraw.java:
package shapessimple;
import javax.swing.JFrame;
public class TestDraw
{
public static void main (String[]args)
{
DrawPanel panel = new DrawPanel();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300,300);
application.setVisible(true);
}
}
DrawPanel.java:
package shapessimple;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
private Random randomNumbers = new Random();
private MyLine[] lines;
public DrawPanel() {
setBackground(Color.white);
lines = new MyLine[5 + randomNumbers.nextInt(5)];
for (int count = 0; count < lines.length; count++) {
int x1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);
Color color = new Color(randomNumbers.nextInt(256),
randomNumbers.nextInt(256), randomNumbers.nextInt(256));
lines[count] = new MyLine(x1, y1, x2, y2, color);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (MyLine line : lines) {
line.draw(g);
}
}
}
MyLine.java:
package shapessimple;
import java.awt.Color;
import java.awt.Graphics;
public class MyLine {
private int x1;
private int y1;
private int x2;
private int y2;
private Color myColor;
public MyLine (int x1, int y1, int x2, int y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
myColor = color;
}
public void draw (Graphics g) {
g.setColor(myColor);
g.drawLine(x1,y1,x2,x2);
}
}
编辑:
现在正在编译,但我收到了NoClassDefFoundError: TestDraw error message
【问题讨论】:
-
你尝试运行它的完整命令是什么?
-
@StephenHogarty 用于编译。 glad3dr 询问您用于运行的命令。
-
为什么不
javac *.java? -
它不会在命令提示符下编译。在eclipse中我按下了运行
-
@StephenHogarty 您必须在命令中包含所有文件才能编译它:
javac TestDraw.java DrawPanel.java MyLine.java
标签: java eclipse command prompt symbols