【发布时间】:2015-09-19 20:30:53
【问题描述】:
对 Java 中的 public static void main 方法有点困惑,希望有人能提供帮助。我有两节课
public class theGame {
public static void main(String[] args) {
lineTest gameBoard = new lineTest();
}
和
public class lineTest extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
g2d.drawLine(100, 100, 100, 200);
}
public static void main(String[] args) {
lineTest points = new lineTest();
JFrame frame = new JFrame("Points");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(points);
frame.setSize(250, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
不幸的是,我的程序没有划清界限。我想弄清楚为什么 lineTest 类中的 main 方法没有启动?
虽然我可以通过将 main 方法更改为其他方法来使其工作,例如“go”,然后从“theGame”类运行该方法,但我对为什么 lineTest 类中的 main 方法不感兴趣'没用。
【问题讨论】:
-
您要执行哪个主要任务?为什么你有两种主要方法?