为什么主要方法不运行? [英] Why doesn't the main method run?

查看:127
本文介绍了为什么主要方法不运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Java中的public static void main方法有点困惑,希望有人可以提供帮助。我有两个班级

A bit confused about the public static void main method in Java and was hoping someone could help. I have two classes

    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方法没有启动?

My program doesn't draw the line unfortunately. I am trying to figure out why the main method in the lineTest class doesn't kick in?

虽然我可以通过将main方法更改为其他方法来使其工作,比如'go'然后从'theGame'类运行那个方法,我很感兴趣为什么lineTest类中的main方法不起作用。

While I can make it work by changing the main method to something else, such as 'go' and then running that method from the 'theGame' class, I am intrigued as to why the main method in the lineTest class doesn't work.

推荐答案

您的应用程序有一个入口点,该入口点是执行的单个主要方法。如果您的入口点是 theGame 类,则只会执行该类的main方法(除非您手动执行其他类的主要方法)。

Your application has one entry point, and that entry point is the single main method that gets executed. If your entry point is the theGame class, only the main method of that class will be executed (unless you manually execute main methods of other classes).

创建 lineTest 类的实例不会导致其主方法被执行。

Creating an instance of lineTest class doesn't cause its main method to be executed.

这篇关于为什么主要方法不运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆