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