0

Here is my code.

import java.awt.*;
import java.awt.event.*;

public class Finals extends Frame implements  WindowListener,ActionListener{
public TextField tf1;
public Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnadd,btnminus,btndivide,btnmultiply,btnequals,btnbackspace;

public Finals(){

    Panel outputpanel = new Panel(new FlowLayout());
     tf1 = new TextField(" ",30);
     outputpanel.add(tf1);

    Panel btnpanel = new Panel(new GridLayout (5,5));

     btn0 = new Button ("0");
     btn1 = new Button ("1");
     btn2 = new Button ("2");
     btn3 = new Button ("3");
     btn4 = new Button ("4");
     btn5 = new Button ("5");
     btn6 = new Button ("6");
     btn7 = new Button ("7");
     btn8 = new Button ("8");
     btn9 = new Button ("9");

     btnadd= new Button("+");
     btnminus = new Button("-");
     btndivide = new Button ("/");
     btnmultiply = new Button ("*");
     btnequals = new Button ("=");
     btnbackspace= new Button("<-");

    btnpanel.add(btnadd);
    btnpanel.add(btnminus);
    btnpanel.add(btndivide);
    btnpanel.add(btnmultiply);
    btnpanel.add(btnequals);
    btnpanel.add(btnbackspace);

    btnpanel.add(btn1);
    btnpanel.add(btn2);
    btnpanel.add(btn3);
    btnpanel.add(btn4);
    btnpanel.add(btn5);
    btnpanel.add(btn6);
    btnpanel.add(btn7);
    btnpanel.add(btn8);
    btnpanel.add(btn9);
    btnpanel.add(btn0);

    setLayout(new BorderLayout());
    add(outputpanel,BorderLayout.NORTH);
    add(btnpanel,BorderLayout.SOUTH);
    setVisible (true);
    setSize (300,200);
    setTitle("Calculator");
    setLocationRelativeTo(null);
    setResizable(false);
    addWindowListener(this);
}

public static void mainr(String[]args){
        Finals awt = new Finals();    
}

    public void windowClosing(WindowEvent we){
            System.exit(0);
        }
     public void windowClosed(WindowEvent we){};
     public void windowOpened(WindowEvent we){};
     public void windowIconified(WindowEvent we){};
     public void windowDeiconified(WindowEvent we){};
     public void windowActivated(WindowEvent we){};
     public void windowDeactivated(WindowEvent we){};
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

2 Answers2

1

You have implemented interface ActionListener so need to override actionPerformed method from that interface otherwise declare your class as abstract.

    @Override
    public void actionPerformed(ActionEvent e) {
       //your code goes here
    }
eatSleepCode
  • 4,427
  • 7
  • 44
  • 93
1

If you're going to implements an interface (ActionListener), Then you need to @Override all of it's methods. In this case you need to implement the actionPerfomed method for your Finals class

@Override
public void actionPerformed(ActionEvent e) {
    // do something
}

  • Also, what is public static void mainr(String[]args){? mainr should be main

  • Also, make use of the @Override anotation, so you know you have successfully overridden a method, i.e.

    @Override
    public void windowClossing(WindowEvent e) {}
    
  • Also, Swing apps should be run on the Event Dispatch Thread (EDT). You can do so by wrapping the inside of your main method with SwingUtilities.invokeLater.... See more at Initial Threads

  • Also, you should setVisible(true) after setSize and setLocationXxx

  • Also, you should just pack() your frame, instead of setSize() so that all the components are sure to be visibly contained.

  • Also read up on Extends JFrame vs. creating it inside the the program

  • Also, why AWT and not Swing? Look into using Swing. See more at Creating GUI with Swing

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720