我一直在使用JCreator上课做作业,JCreator一直给我错误:在AddSubtract类中找不到主要方法,请定义main方法 我有两个类AddSubtract和AddSubtractViewer。 AddSubtractViewer有主要方法。当AddSubtractViewer具有main方法时,JCreator会给我上面的错误,但是当我将确切的main方法复制并粘贴到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();
}
}
为什么会这样?
答案 0 :(得分:2)
问题出现是因为AddSubtract
被配置为JCreator中的启动文件。您需要更改启动文件,告诉JCreator您的main()
位于AddSubtractViewer
,如下所示:
AddSubtractViewer
作为主文件。