我一直在使用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
作为主文件。