Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can anyone help me with this java problem? [Java]

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Location
    Italy
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Can anyone help me with this java problem? [Java]

    Exception in thread "main" java.lang.ClassCastException: calculator.Calculator cannot be cast to java.awt.event.ActionListener
    at calculator.Calculator.<init>(Calculator.java:142)
    at calculator.Main_calculator.main(Main_calculator.ja va:7)

    Code:
    package calculator;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.border.Border;

    public class Calculator {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextArea textarea = new JTextArea(2,10);
    JButton button1 = new JButton ();
    JButton button2 = new JButton ();
    JButton button3 = new JButton ();
    JButton button4 = new JButton ();
    JButton button5 = new JButton ();
    JButton button6 = new JButton ();
    JButton button7 = new JButton ();
    JButton button8 = new JButton ();
    JButton button9 = new JButton ();
    JButton button0 = new JButton ();

    JButton buttondot = new JButton ();
    JButton buttonequal = new JButton ();
    JButton buttonadd = new JButton ();
    JButton buttonsub = new JButton ();
    JButton buttonmul = new JButton ();
    JButton buttondiv = new JButton ();
    JButton buttonclear = new JButton ();

    double number1,number2,resoult;
    int addc=0,mulc=0,divc=0,subc=0;

    public Calculator () {

    frame.setSize(340,450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    frame.setTitle("Calcolatrice di Paolo");

    //frame.setResizable(false);

    frame.add(panel);
    panel.setBackground(Color.white);
    Border border = BorderFactory.createLineBorder(Color.RED,4);

    panel.add(textarea);

    textarea.setBackground(Color.black);
    Border tborder = BorderFactory.createLineBorder(Color.blue,3);
    textarea.setBorder(tborder);
    Font font = new Font("Arial",Font.BOLD,33);
    textarea.setFont(font);
    textarea.setForeground(Color.WHITE);

    textarea.setPreferredSize(new Dimension (2,10));
    textarea.setLineWrap(true);

    //lista numeri
    button1.setPreferredSize(new Dimension(100,50));
    button1.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton1.png"));

    button2.setPreferredSize(new Dimension(50,50));
    button2.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton2.png"));

    button3.setPreferredSize(new Dimension(50,50));
    button3.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton3.png"));

    button4.setPreferredSize(new Dimension(50,50));
    button4.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton4.png"));

    button5.setPreferredSize(new Dimension(50,50));
    button5.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton5.png"));

    button6.setPreferredSize(new Dimension(50,50));
    button6.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton6.png"));

    button7.setPreferredSize(new Dimension(50,50));
    button7.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton7.png"));

    button8.setPreferredSize(new Dimension(50,50));
    button8.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton8.png"));

    button9.setPreferredSize(new Dimension(50,50));
    button9.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton9.png"));

    button0.setPreferredSize(new Dimension(50,50));
    button0.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\b utton0.png"));

    //lista segni

    buttondot.setPreferredSize(new Dimension(50,50));
    buttondot.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\d ot.png"));

    buttonmul.setPreferredSize(new Dimension(50,50));
    buttonmul.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\m ul.png"));

    buttondiv.setPreferredSize(new Dimension(50,50));
    buttondiv.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\d iv.png"));

    buttonadd.setPreferredSize(new Dimension(50,50));
    buttonadd.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\p lus.png"));

    buttonsub.setPreferredSize(new Dimension(50,50));
    buttonsub.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\s ub.png"));

    buttonequal.setPreferredSize(new Dimension(50,50));
    buttonequal.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\e qual.png"));

    buttonclear.setPreferredSize(new Dimension(50,50));
    buttonclear.setIcon(new ImageIcon("C:\\Users\\Utente\\Desktop\\immagini\\c lear.png"));


    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    panel.add(button5);
    panel.add(button6);
    panel.add(button7);
    panel.add(button8);
    panel.add(button9);
    panel.add(button0);
    panel.add(buttondot);
    panel.add(buttondiv);
    panel.add(buttonsub);
    panel.add(buttonmul);
    panel.add(buttonadd);
    panel.add(buttonequal);
    panel.add(buttonclear);

    button1.addActionListener((ActionListener) this);
    button2.addActionListener((ActionListener) this);
    button3.addActionListener((ActionListener) this);
    button4.addActionListener((ActionListener) this);
    button5.addActionListener((ActionListener) this);
    button6.addActionListener((ActionListener) this);
    button7.addActionListener((ActionListener) this);
    button8.addActionListener((ActionListener) this);
    button9.addActionListener((ActionListener) this);
    button0.addActionListener((ActionListener) this);
    buttondot.addActionListener((ActionListener) this);
    buttonadd.addActionListener((ActionListener) this);
    buttonsub.addActionListener((ActionListener) this);
    buttondiv.addActionListener((ActionListener) this);
    buttonmul.addActionListener((ActionListener) this);
    buttonclear.addActionListener((ActionListener) this);
    buttonequal.addActionListener((ActionListener) this);

    }




    public void actionPerformed (ActionEvent e) {

    Object source = e.getSource();
    number1=0.0;
    number2=0.0;
    textarea.setText("");


    if (source==button1) {
    textarea.append("1");
    }
    if(source==button2){
    textarea.append("2");
    }
    if(source==button2){
    textarea.append("2");
    }
    if(source==button3){
    textarea.append("3");
    }
    if(source==button4){
    textarea.append("4");
    }
    if(source==button5){
    textarea.append("5");
    }
    if(source==button6){
    textarea.append("6");
    }
    if(source==button7){
    textarea.append("7");
    }
    if(source==button8){
    textarea.append("8");
    }
    if(source==button9){
    textarea.append("9");
    }
    if(source==button0){
    textarea.append("0");
    }
    if(source==buttondot){
    textarea.append(".");
    }


    if(source==buttonadd){
    number1=number_reader();
    textarea.setText("+");
    addc=1;
    divc=0;
    mulc=0;
    subc=0;
    }
    if(source==buttondiv){
    number1=number_reader();
    textarea.setText("/");
    addc=0;
    divc=1;
    mulc=0;
    subc=0;

    }
    if(source==buttonmul){
    number1=number_reader();
    textarea.setText("*");
    addc=0;
    divc=0;
    mulc=1;
    subc=0;

    }
    if(source==buttonsub){
    number1=number_reader();
    textarea.setText("-");
    addc=0;
    divc=0;
    mulc=0;
    subc=1;

    }
    if(source==buttonequal){
    number1=number_reader();
    if(addc>0) {
    resoult=number1+number2;
    textarea.setText(Double.toString(resoult));
    }
    if(subc>0) {
    resoult=number1-number2;
    textarea.setText(Double.toString(resoult));
    }
    if(mulc>0) {
    resoult=number1*number2;
    textarea.setText(Double.toString(resoult));
    }
    if(divc>0) {
    resoult=number1/number2;
    textarea.setText(Double.toString(resoult));
    }

    }





    }

    public double number_reader() {
    double num1;
    String s;
    s=textarea.getText();
    num1=Double.valueOf(s);
    return num1;
    }
    }

    -------------------------Main:
    package calculator;

    public class Main_calculator {

    public static void main(String[] args) {

    Calculator cal = new Calculator ();

    }

    }

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    92
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Can anyone help me with this java problem? [Java]

    It's because your Calculator class does not implement ActionListener. If you add the @Override annotation to the actionPerformed function (a good habit for all overridden functions), you'll get a compiler error letting you know it's not overriding anything:

    @Override  // <- Error: does not override or implement a method from a supertype
    public void actionPerformed (ActionEvent e) {
     
    }

    So tag your class as implementing ActionListener, then actionPerformed is an override as expected:
    public class Calculator implements ActionListener {
     
    }
    With this, the error on the @Override annotation, and the ClassCastException, should both go away.

Similar Threads

  1. Java 7 or java 8 problem syntax
    By glprobot in forum Java Theory & Questions
    Replies: 0
    Last Post: August 26th, 2019, 03:24 AM
  2. Java Problem
    By HypeIsReal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 25th, 2014, 05:20 AM
  3. Java problem
    By destinedtale in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 7th, 2014, 11:02 AM
  4. HELP,java problem *java.lang.NullPointerException*
    By Agung_Rianto in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 24th, 2014, 11:19 AM
  5. java problem. please help
    By jimmy_crews in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 31st, 2013, 10:33 AM

Tags for this Thread