当main方法包含在一个类中时,我的程序可以编译,但是当main方法是单独的时候就不能编译



我一直在使用JCreator和JCreator为课堂做作业,JCreator一直给我错误:在类AddSubtract中没有找到主方法,请定义主方法我有两个类AddSubtract和AddSubtractViewer。AddSubtractViewer有main方法。当AddSubtractViewer有主方法时,JCreator给了我上面的错误,但当我将确切的主方法复制并粘贴到AddSubtract类中时却没有。代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AddSubtract
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 100;
private static final int INCREMENT = 5;
private int value;
private JButton buttonAdd, buttonSubtract;
private JLabel label;
private JFrame frame;
private JPanel panel;
//Sets up the GUI
public AddSubtract()
{
    frame = new JFrame ("Add and Subtract");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    value = 50;
    label = new JLabel("Value: " + Integer.toString(value));
    buttonAdd = new JButton("Add");
    buttonSubtract = new JButton("Subtract");
    buttonAdd.addActionListener(new AddButtonListener());
    buttonSubtract.addActionListener(new SubtractButtonListener());     
    panel = new JPanel();
    panel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
    panel.setBackground(Color.lightGray);
    panel.add(label);
    panel.add(buttonAdd);
    panel.add(buttonSubtract);
    frame.add(panel);
}
//displays the primary application frame
public void display()
{
    frame.pack();
    frame.setVisible(true);
}
private class AddButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        value = value + INCREMENT;
        label.setText("Vaue: " + Integer.toString(value));
    }
}
private class SubtractButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        value = value - INCREMENT;
        label.setText("Value : " + Integer.toString(value));
    }
}   
/*public static void main(String[] args) 
{
    AddSubtract application = new AddSubtract();
    application.display();
}*/
}

和AddSubtractViewer

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
public class AddSubtractViewer
{
//execute application
public static void main(String[] args) 
{
    AddSubtract application = new AddSubtract();
    application.display();      
}
}

为什么会发生这种情况?

出现问题是因为在JCreator中将AddSubtract配置为启动文件。您需要更改启动文件,告诉JCreator您的main()位于AddSubtractViewer中,如下所示:

  • 从项目菜单中选择项目设置…
  • 在Run:窗口中选择AddSubtractViewer作为主文件。

相关内容

  • 没有找到相关文章

最新更新