简体   繁体   中英

Java Factorial GUI

The goal is to create a program which utilizes a GUI to calculate a factorial. I need to create a jPanel or jFrame to display the results of the program. Then I need to get the text from JTextField and parse it to an Integer so it can be passed to a constructor. Thirdly, an object class needs to be created with an instance variable of type int called factorial. This object class needs a single argument constructor named Factorial that accepts an int and assigns the value to the instance variable. Then it should have a method that calculates the factorial using a for loop with a return type of int. Finally, it should display the value of the object after calling the method that computes the factorial. Here is my work so far.

import javax.swing.JFrame;

public class factorialCalc

{

     public static void main (String[] args)

     {

          JFrame frame=new JFrame ("Factorial Calculator");

          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

          Factorial panel=new Factorial();

          frame.getContentPane().add(panel);

          frame.pack();

          frame.setVisible(true);

     }

}

And my next file is the Object class:

 import java.awt.*;

 import java.awt.event.*;

 import javax.swing.*;



 public class Factorial extends JPanel

 {

          private int factorial; 

          private JLabel inputLabel,resultLabel;

          private JTextField factorialText;

          private JButton factButton;



           //Constructor: sets up main GUI components

           public void FactorialPanel(int factorial)

           {

                this.factorial=factorial;

                inputLabel= new JLabel ("Please enter an integer:");

                factButton = new JButton("Compute");

                TempListener listener=new TempListener();

                factButton.addActionListener(listener);

                factorialText = new JTextField();

                factorialText.addActionListener (new TempListener());

                add(inputLabel);

                add(factorialText);

                add(resultLabel);

           }

           //represents a listener for the button

           private class TempListener implements ActionListener

           {

            //performs factorial operation when the 'Compute' button is  pressed

            public int computeFactorial(ActionEvent event)

            {

                 int text=Integer.parseInt(factorial);

                 int f =1;

                 for(int i=text;i>=1;i--)

                 {      

                      f = f*i;

                 }  

                  resultLabel.setText(Integer.toString(f));

            }

       }

 }

These are the following errors I get when I try to compile:

.\Factorial.java:31: error: Factorial.TempListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
    private class TempListener implements ActionListener
            ^
  .\Factorial.java:37: error: incompatible types: int cannot be converted to String
                    int text=Integer.parseInt(factorial);
                                              ^
  Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
  2 errors

Why does it not let me parse the String factorial into and integer?

Also, how and where can I call the computeFactorial method to display the value of the object?

Thank you! Any help is appreciated!

Instead of using factorial here, which is already an int (hence the error)...

int text=Integer.parseInt(factorial);

... I think you meant to get the JTextField value:

int text=Integer.parseInt(factorialText.getText());

Also, if TempListener implements ActionListener , then you must follow the ActionListener interface. The method is called actionPerformed() ...

 public int actionPerformed(ActionEvent event)

You can't just rename it to whatever you want, like you did here:

public int computeFactorial(ActionEvent event)

Finally, based on your comment, another issue is you misnamed your constructor. If your class is defined as:

public class Factorial extends JPanel

Then your constructor might be:

public Factorial(int factorial)  // note: no void and same name as class

Listen to your compiler:

error: Factorial.TempListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener

It's easy for a beginner to think "Oh, that's a load of babble, I'll try stuff instead of trying to understand what it says" -- but you need to understand them because they tell you exactly what the compiler is complaining about.

Here's it's telling you that because Factorial.TempListener implements ActionListener , it expects it to have a method actionPerformed(ActionEvent) .

If ActionListener was abstract , it would be allowed to not have this method -- but then you wouldn't be able to instantiate it, so just adding abstract won't help.

So you need to write actionPerformed(ActionListener) .

What that method should do is beyond what SO is for -- read and understand a book or tutorial on Swing.

No doubt once you fix this there will be more compiler errors. Read them; they usually tell you where the problem is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM