首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Factorial GUI

Java Factorial GUI
EN

Stack Overflow用户
提问于 2016-06-01 21:51:57
回答 2查看 1.5K关注 0票数 0

目标是创建一个利用GUI计算阶乘的程序。我需要创建一个jPanel或jFrame来显示程序的结果。然后,我需要从JTextField获取文本并将其解析为整数,以便可以将其传递给构造函数。第三,需要使用一个名为factorial的int类型的实例变量创建一个对象类。这个对象类需要一个名为Factorial的单参数构造函数,它接受int并将值赋给实例变量。然后它应该有一个方法,使用一个返回类型为int的for循环来计算阶乘。最后,它应该在调用计算阶乘的方法之后显示对象的值。这是我到目前为止的工作。

代码语言:javascript
复制
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);

     }

}

我的下一个文件是Object类:

代码语言:javascript
复制
 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));

            }

       }

 }

以下是我在尝试编译时得到的以下错误:

代码语言:javascript
复制
.\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

为什么不让我把字符串的阶乘解析成整数呢?

另外,如何以及在何处调用computeFactorial方法来显示对象的值?

谢谢!如有任何帮助,我们不胜感激!

EN

回答 2

Stack Overflow用户

发布于 2016-06-01 22:11:42

这里不使用factorial,它已经是一个int (因此出现错误)……

代码语言:javascript
复制
int text=Integer.parseInt(factorial);

..。我想你的意思是获取JTextField的值:

代码语言:javascript
复制
int text=Integer.parseInt(factorialText.getText());

另外,如果TempListener实现了ActionListener,那么您必须遵循ActionListener接口。该方法名为actionPerformed()...

代码语言:javascript
复制
 public int actionPerformed(ActionEvent event)

您不能像这里那样将其重命名为您想要的任何名称:

代码语言:javascript
复制
public int computeFactorial(ActionEvent event)

最后,根据你的评论,另一个问题是你错误地命名了你的构造函数。如果您的类被定义为:

代码语言:javascript
复制
public class Factorial extends JPanel

那么你的构造函数可能是:

代码语言:javascript
复制
public Factorial(int factorial)  // note: no void and same name as class
票数 1
EN

Stack Overflow用户

发布于 2016-06-01 22:26:28

听一听你的编译器:

错误: Factorial.TempListener不是抽象的,并且不重写ActionListener中的抽象方法actionPerformed(ActionEvent

对于初学者来说,很容易想到“哦,那是一堆胡言乱语,我会尝试一些东西,而不是试图理解它所说的话”--但你需要理解它们,因为它们确切地告诉你编译器在抱怨什么。

它告诉您,因为Factorial.TempListener实现了ActionListener,所以它希望它有一个actionPerformed(ActionEvent)方法。

如果ActionListenerabstract,则可以不使用此方法--但这样就无法实例化它,因此只添加abstract是没有帮助的。

所以你需要编写actionPerformed(ActionListener)

该方法应该做的事情超出了它的用途--阅读并理解一本关于Swing的书或教程。

毫无疑问,一旦你修复了这个问题,就会有更多的编译器错误。阅读它们;它们通常会告诉你问题所在。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37570720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档