2

So I found a grid layout of a simple calculator somewhere that I thought could be fun to see if I could get working.

Problem is though, that after I started messing with it to add actionlisteners and such to the buttons, my console spewed following at me:

Error: Main method not found in class CalcMain.CalculatorMain, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

I'm still fairly new in coding, so I'm not entirely sure as to what this means, and how I should fix it.

The error popped up after I implemented ActionListener, and switched out

Calc.add(new JButton(buttons[i]));

with

test[i] = new JButton(buttons[i]);
test[i].addActionListener(this);
test[i].setActionCommand(buttons[i]);

What did I screw up, and what should I do to fix it? Any help is appreciated!

Thanks in advance!

public class CalculatorMain extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

public CalculatorMain() {

    initUI();
}   

public final void initUI() {

    JPanel Calc = new JPanel();

    Calc.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    Calc.setLayout(new GridLayout(5, 4, 5, 5));

    String[] buttons = {
        "Cls", "Bck", "", "Close", "7", "8", "9", "/", "4",
        "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"
    };

    JButton[] test = new JButton[19];

    for (int i = 0; i < buttons.length; i++) {
        if (i == 2)
            Calc.add(new JLabel(buttons[i]));
        else
            test[i] = new JButton(buttons[i]);
            test[i].addActionListener(this);
            test[i].setActionCommand(buttons[i]);
    }

    add(Calc);

    setTitle("CalculatorDemo");
    setSize(350, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            CalculatorMain demo = new CalculatorMain();
            demo.setVisible(true);
        }
    });
}

public void actionPerformed(ActionEvent e) {
    String name = e.getActionCommand();

    if(name.equals("1")) {
        System.out.println("No");
    }
    else if(name.equals("2")) {
        System.out.println("Yes");
    }
}
}
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
Nilley
  • 21
  • 1
  • You can make any other class which will have the main method and then make an object of your **CalculatorMain** class and call the method of this class using the object which you have created. All this stuff you have to do inside the main method. – Rish Jan 04 '16 at 12:29
  • I'll try that, thanks! – Nilley Jan 04 '16 at 12:30
  • *"..found a grid layout of a simple calculator.."* See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Jan 04 '16 at 12:40
  • Have a look at [A Closer Look at the "Hello World!" Application](https://docs.oracle.com/javase/tutorial/getStarted/application/) – MadProgrammer Jan 04 '16 at 22:42

0 Answers0