0

Hi im still a student learning Java. Im supposed to make this calculator but im having an error and I don't know how to fix it.

here are my codes:

**import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Task1 extends JPanel implements ActionListener
{
   JLabel label1,label2;
   JTextField textField1,textField2; 
   JButton button1,button2,button3,button4,button5,button6;
   double addResult=0;
   double subResult=0;
   double mulResult=0;
   double divResult=0;

   public void Task1()    
   {
    setLayout(new GridLayout(5,2,5,5)); 
      
    label1=new JLabel("First Value: "); 
    add(label1);
    textField1=new JTextField(10); 
    add(textField1);
    label2=new JLabel("Second Value"); 
    add(label2);
    textField2=new JTextField(10); 
    add(textField2);
    button1=new JButton("ADD"); 
    add(button1);
    button1.setMnemonic(KeyEvent.VK_A);
    button1.addActionListener(this); 
    button2=new JButton("SUBSTRACT"); 
    add(button2);
    button2.addActionListener(this);
    button3=new JButton("MULTIPLY"); 
    add(button3);
    button3.addActionListener(this);
    button4=new JButton("DIVIDE"); 
    add(button4);
    button4.addActionListener(this);
    button5=new JButton("COMPUTE"); 
    add(button5);
    button5.addActionListener(this);
    button6=new JButton("RESET"); 
    add(button6);
    button6.addActionListener(this);
      
      
  }
 @Override
 public void actionPerformed(ActionEvent evt)
 {
   try{
    if(evt.getSource()==button1) 
    {
    String text=textField1.getText(); 
    String text2=textField2.getText();
    if(text.equals("")||text2.equals("")) 
  {
    JOptionPane.showMessageDialog(null,"Please Enter Values"); 
  }
    addResult=addResult+Integer.parseInt(text)+Integer.parseInt(text2); 
  }
    if(evt.getSource()==button2) 
  {
    String text=textField1.getText(); 
    String text2=textField2.getText();
    if(text.equals("")||text2.equals(""))
  {
    JOptionPane.showMessageDialog(null,"Please Enter Values"); 
  }
    subResult=subResult+(Integer.parseInt(text)-Integer.parseInt(text2)); 
  }
    if(evt.getSource()==button3) 
  {
    String text=textField1.getText();
    String text2=textField2.getText(); 
    if(text.equals("")||text2.equals("")) 
  {
     JOptionPane.showMessageDialog(null,"Please Enter Values"); 
  }
     mulResult=mulResult+(Integer.parseInt(text)*Integer.parseInt(text2)); 
  }
     if(evt.getSource()==button4) 
  {
     String text=textField1.getText(); 
     String text2=textField2.getText();
     if(text.equals("")||text2.equals("")) 
  {
     JOptionPane.showMessageDialog(null,"Please Enter Values"); 
  }
     divResult=divResult+(Integer.parseInt(text)/Integer.parseInt(text2)); 
  }
     if(evt.getSource()==button5) 
  {
     JOptionPane.showMessageDialog(null,"Result: "+ 
  (addResult+subResult+mulResult+divResult));
  }
     if(evt.getSource()==button6) 
 {
     textField1.setText(""); 
     textField2.setText("");
  }
  
  }
     catch(Exception e)
 {
     JOptionPane.showMessageDialog(null,"Exception: "+e.getMessage()); 
 }
}
}**

and here's the error:

Error: Main method not found in class Task1, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application codio@border-florida:~/workspace/swing$

  • 1
    *"I don't know how to fix it."* Well, what do you understand from the error reported? Do some searching, SO is not a place to come to have (the meaning of) basic errors explained. – Andrew Thompson Jul 15 '21 at 20:19
  • 1
    Read the [Swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for Swing basics. All section have working demos you can download and they all show how to write a main() method for your class. – camickr Jul 15 '21 at 20:59

2 Answers2

0

As stated in the error text you will need to specify a main method. A main method is the starting point for all java applications. You should have gotten some material from school where this is explained. Search for a method named main there or look it up in Oracles basic Java tutorials.

Moreover you will have to do some more work to actually see this JPanel named Task1, but your school material or the basic swing tutorials linked by other replies explain these things.

peq
  • 329
  • 1
  • 2
  • 9
-4

First, You should put you class in a package. Second, you have to override the method start like this :

@Override
    public void start(Stage primaryStage) {
         //put your starting code here
    }

MAKAPUSH
  • 7
  • 2