简体   繁体   中英

When main method is contained within a class, my program compiles but not when main method is separate

I've been working on a homework for class using JCreator and JCreator keeps giving me Error: Main method not found in class AddSubtract, please define main method I have two classes AddSubtract and AddSubtractViewer. AddSubtractViewer has the main method. When AddSubtractViewer has the main method, JCreator gives me the above error, but not when I copy and paste the exact main method into the AddSubtract class. Here are the codes:

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();
}*/
}

and the AddSubtractViewer is

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();      
}

}

why is this happening?

The problem happens because AddSubtract is configured as your start-up file in JCreator. You need to change the start-up file to tell JCreator that your main() is located in AddSubtractViewer , like this:

  • From the Project menu select Project Settings...
  • From the Run: window select AddSubtractViewer as the main file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM