0

好的,所以我的程序正在获取用户的输入以找出质数(直到用户输入的最大值),然后在可滚动的 JFrame 中显示这些结果。我已经完成了所有这些(至少我相信如此),但是当我尝试编译它时总是出现一个错误。另外,如果您发现我遗漏的任何其他错误,请随时告诉我!

代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PrimeNumbersJ extends JFrame
{
    private static final int WIDTH=400;
    private static final int HEIGHT=300;

    //JFrame Components
    private JLabel jlblMaxNumber;

    private JTextArea  jtaOutput;

    private JTextField jtfMaxNumber;

    private JButton jbtnCalculate, jbtnClear, jbtnExit;

    private CalculateButtonHandler calculateHandler;
    private ClearButtonHandler  clearHandler;
    private ExitButtonHandler   exitHandler;

    private JScrollPane scrollingResult;

    private JPanel jpnlTop = new JPanel();
    private JPanel jpnlCenter = new JPanel();
    private JPanel jpnlBottom = new JPanel();


    public PrimeNumbersJ()
    {
        // Set the title and Size:
        setTitle("Prime Numbers with JFrame");
        setSize(WIDTH, HEIGHT);
        jpnlBottom.setLayout(new GridLayout(1, 3));

        // Instantiate the JLabel components:
        jlblMaxNumber = new JLabel("Enter the Largest Number to test: ", SwingConstants.LEFT);

        // Instantiate the JTextFields:
        jtfMaxNumber = new JTextField(10);

        // Make the JTextArea scrollable:
        jtaOutput = new JTextArea(10,1);
        scrollingResult = new JScrollPane(jtaOutput);

        // Instantiate and register the Calculate button for clicks events:
        jbtnCalculate = new JButton("Calculate");
        calculateHandler = new CalculateButtonHandler();
        jbtnCalculate.addActionListener(calculateHandler);

        // Instantiate and register the Clear button for clicks events:
        jbtnClear = new JButton("Clear");
        clearHandler = new ClearButtonHandler();
        jbtnClear.addActionListener(clearHandler);

        // Instantiate and register the Exit button for clicks events:
        jbtnExit = new JButton("Exit");
        exitHandler = new ExitButtonHandler();
        jbtnExit.addActionListener(exitHandler);

        // Assemble the JPanels:
        jpnlTop.setLayout(new GridLayout(1, 2));
        jpnlTop.add(jlblMaxNumber);
        jpnlTop.add(jtfMaxNumber);

        jpnlCenter.setLayout(new GridLayout(1, 1));
        jpnlCenter.add(scrollingResult);

        jpnlBottom.setLayout(new GridLayout(1, 3));
        jpnlBottom.add(jbtnCalculate);
        jpnlBottom.add(jbtnClear);
        jpnlBottom.add(jbtnExit);

        // Start to add the components to the JFrame:
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());

        pane.add(jpnlTop, BorderLayout.NORTH);
        pane.add(jpnlCenter, BorderLayout.CENTER);
        pane.add(jpnlBottom, BorderLayout.SOUTH);


        // Show the JFrame and set code to respond to the user clicking on the X:
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        jpnlTop.setLayout(new GridLayout(1, 3));
        jpnlTop.add(jlblMaxNumber);
        jpnlTop.add(jtfMaxNumber);

        jpnlCenter.setLayout(new GridLayout(1, 1));
        jpnlCenter.add(scrollingResult);

        jpnlBottom.add(jbtnCalculate);
        jpnlBottom.add(jbtnClear);
        jpnlBottom.add(jbtnExit);

        // Show the JFrame and set code to respond to the user clicking on the X:
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }//End Constructor


    private class CalculateButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            int iRemainder,iPrimeCheck;
            int iNumbertoTest = 0;
            boolean bValidInput = true;
            String sPrime ="";

            try
            {
                iNumbertoTest = Integer.parseInteger(jtfMaxNumber.getText());
        }
            catch (Exception aeRef)
            {
                JOptionPane.showMessageDialog(null,"Enter the Max Number to Test.", getTitle(), JOptionPane.WARNING_MESSAGE);
                bValidInput = false;
            }// end of catch


            if ( bValidInput )
            {

                for(iNumberToTest = 1;iNumberToTest <= 100;iNumberToTest++)    {
                    iRemainder = 0;
                    for(iPrimeCheck = 1;iPrimeCheck <= iNumberToTest;iPrimeCheck++){
                        if(iNumberToTest % iPrimeCheck == 0){
                                iRemainder++;
                            }
                        }
                        if(iRemainder == 2 || iNumberToTest == 1)
                        {
                                String sNumber = Integer.toString(iNumberToTest);
                                sPrime = sPrime + (sNumber + "\n");
                        }


            }
                // Populate the output by using the methods in the user defined class::

                jtaOutput.append("The Prime Numbers Are: \n" + sPrime  + "\n");
            } // end if
        } //end ActionPerformed
    }//End CalculateButtonHandler

    private class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }//end ExitButtonHandler


    private class ClearButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            jtfMaxNumber.setText("");
            jtaOutput.setText("");
         }
    } // end ClearButtonHandler






    public static void main(String args[])
    {
        PrimeNumbersJ primNumJ = new PrimeNumbersJ();
    }
}

错误

java:120: cannot find symbol
symbol  : method parseInteger(java.lang.String)
location: class java.lang.Integer
            iMaxNumber = Integer.parseInteger(jtfMaxNumber.getText());
                                ^
4

3 回答 3

6

Integer.parseInteger()

不存在。

你在找吗Integer.parseInt()???

于 2012-12-19T08:45:11.610 回答
4

更改 Integer.parseInteger()

Integer.parseInt()

在类中也声明int iNumberToTest为类变量CalculateButtonHandler

于 2012-12-19T08:46:38.147 回答
2

该类Integer不包含名为 的方法parseInteger。改为使用parseInt

于 2012-12-19T08:46:09.780 回答