了解我的Prime数字计算和Prime数字JFrame发生了什么

时间:2013-04-08 06:26:52

标签: java swing primes prime-factoring

我想弄清楚我的代码出了什么问题,并帮助理解我的Prime数字计算和素数JFrame发生了什么。

  1. 由于某种原因,计算人员只说1-100的素数
    1. 我拿出了system.exit(0);,现在我需要在退出之前点击所有数字。我希望实现的是获得一个列出所有素数1-100的列表。
  2. 然后使用编码素数计算,我需要制作素数JFrame。我有太多错误,这是我的第一个问题,其次,由于错误,我无法运行项目来查看它。
  3. 我已将我的代码发布到素数计算和我的JFrame。任何有助于弄清楚发生了什么/错误的人都会非常感激。

    *********************************************************************************
    //Prime Numbers Java Swing JFrame
    **********************************************************************************
    
     import javax.swing.*;
     import java.awt.*;
     import java.awt.event.*;
    
    
     public class ProgrammingAssignment8 extends JFrame 
     implements ActionListener 
     {
    
    //DECLARE BUT DO NOT INSTANTIATE
    private JButton calculate;
    private JButton clear;
    private JButton exit;
    
    //PANELS
    private JPanel jpnlMain = new JPanel();
    
    private JPanel jpnlTop = new JPanel();
    private JPanel jpnlCenter = new JPanel();
    private JPanel jpnlBotton = new JPanel();
    
    private JScrollPane scrollingResult;
    
    
    //DECLARE JFRAME COMPONENTS
    private JLabel jlblName, jlblMaxTest;
    private JTextField jtfName, jtfMaxTest;
    private JTextArea jtaName, jtaMaxNumbers;
    private JButton jbutCalculate, jbutClear, jbutExit;
    
    private CalculateButtonHandler calculateHandler;
    private ClearButtonHandler clearHandler;
    private ExitButtonHandler exitHandler;
    
    
    
    
    
    /**
     * @param args
     */
    
    //CLASS CONSTRUCTOR 
    public ProgrammingAssignment8 (String sTitle) 
    {       
    
    //PREPARE THE JFRAME/WINDOW
        super(sTitle);
    //SET TITLE AND SIZE
    setTitle(sTitle);
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    //SET THE LAYOUT
    jpnlMain.setLayout(new GridLayout(3,3,5,5));
    
     //INSTANTIATE THE JBUTTONS WITH THE PASSED CAPTION AND COLOR AND ADD TO JPANEL
    //PUT INTO A METHOD FOR EFFICIENCY AND TO AVOID REPEATING ALL THE CODE
    jbtnCalculate = instantiateJButton("Calculate", Color.red);
    jbtnClear = instantiateJButton("Clear", Color.blue);
    jbtnExit = instantiateJButton("Exit", Color.black);
    
    //INSTANTIATE THE JLABEL COMPONENTS
    jlblName = new JLabel ("");
    
    //PREPARE THE CONTAINER
    Container ca = getContentPane();
    }
    
    set visible(true);
    
    
    //END CONSTRUCTOR
    
    
    private JButton instantiateJButton(String, sInCaption, Color inColor)
    {
    JButton jbtnButton = new JButton(sInCaption);
    jbtnButton.setActionCommand(sInCaption);
    jbtnButton.addActionListener(this);
    jbtnButton.setForeground(inColor);
    jbtnButton.setFont(fontChosenFont);
    jpnlMain.add(jbutnButton);
    return jbtnButton;
    
    //END INSTANTIATE JBUTTON
    
    //CLASS EVENT HANDLER   
    public void actionPerformed(java.awt.event.ActionEvent e)
    
    {
        //FIND OUT WHICH BUTTON WAS PRESSED BY USING THE ACTION COMMAND VALUE
        String sActionCommand = e.getActionCommand();
    
        //FROM THE ACTIONEVENT OBJECT, GET AN INSTANCE OF THE JBUTTON THAT WAS PRESSED
        JButton jbtnSource = (JButton) e.getSource();
    
        jbtnSource.setEnabled(false);
    
        //LET THE USER KNOW WHAT BUTTON WAS CLICKED
        JOptionPane.showMessageDialog(null,  "You pressed the "" sActionCommand + " button", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
    }
    
    //END ACTIONPERFORMED (java.awt.event.ActionEvent e)
    
    //EXECUTION STARTING POINT
    
    public static void main(String[] args)
    {
        ProgrammingAssignment8 = new ProgrammingAssignment8("Instantiates with a Method");
    
    }
    //END main(String[] args)
    
    
    }
    //END ProgrammingAssignment8 CLASS
    
    
    ***********************************************************************************
    PRIME NUMBERS CALCULATIONS
    ***********************************************************************************
    
    import javax.swing.JOptionPane;
    public class ProgrammingAssignment7 
    {
    
    /**
     * @param args
     * @return 
     */
    public static void main(String[] args) 
    {
        //Scanner Scan = new Scanner (System.in);
        // TODO Auto-generated method stub
    
        //DECLARE VARIABLES
        int x = 1;
        int i = 1;
        int iNumber = 1;
        boolean bNotPrime = false;
        boolean bIsPrime = true;
        int iNumberToTest;
        int iPrimeCheck;
        int iCounter;
        int iResult = 1;
        int iFact = 1;
        int iLimit = 100;
        String OutputStr = null;
    
    
    
    
    
        System.out.println("Prime numbers between 1 and " + iLimit);
    
        //loop through the numbers one by one
        for(i=1; i < 100; i++)
        {
    
                bIsPrime = true;
    
                //check to see if the number is prime
                for(int j = 2; j < i ; j++){
    
                        if(i % j == 0){
                                bIsPrime = false;
                                break;
                        }
                }
                // print the number
                if(bIsPrime)
    
                    OutputStr = "The Prime Numbers of 1 - 100 are: " + i + "\n";
    
                    JOptionPane.showMessageDialog(null, OutputStr, "PRIME NUMBERS", JOptionPane.INFORMATION_MESSAGE);
                       //System.out.print(i + "\n" );
    
    
    
    
    
    
        //LISTS NUMBERS 1 THROUGH 100, INITIALIZE INTEGER NAMED I, IF IT IS LESS THAN OR EQUAL TO 100 THEN ADD ONE TO THE LOOP
    
    
    
        /* for (int i=1; i<=100; i++)
        {
            System.out.println(i);
        }
    
    
    
        /* iNumber = 7;
    
        for (iNumber = 1; iNumber <= 100; iNumber++)
            iResult = iNumber / 1;
        System.out.println(iResult);
    
        /* for ( iNumber = 2; iNumber <= 100; iNumber++ )
        {
          if ( iNumber % iNumber == 0 )
          {
            bIsPrime = false;
        break;
    
          }
        }
    
        return bIsPrime;
    }
    
    */
    
    
    
    
    
        /* iNumber = 1; 
        while (iNumber < 100) iNumber++; 
        { 
            System.out.print(iNumber + " ");
            System.out.println();
    
    
            for (iNumber = iNumber - 1; iNumber > 2; iNumber++) 
            { //divides the number by each number less than the number itself and also greater than 1 (because 1 is a factor of every number, and every number is a factor of itself)
                if (iNumber % iNumber != 0) 
                    System.out.println(iNumber + " is a prime number.");
    
    
    
    
                { //else if the number is evenly divisible by a number other than itself and 1 (we weeded out those cases in the above for statement), then it sets the boolean to be true, and breaks the for loop
                    bNotPrime = false;
                    iNumber = 1;
                }
    
            }
    
            if (bIsPrime = true) 
            { //if the number is not not a prime, then it prints the number and the for loop moves on 
    
            }
    
            else if (bNotPrime = false)
            { //otherwise the for loop moves on
                System.out.println(iNumber + " is not a prime number.");
    
                */              
    
    
    }
    }
    }
    

2 个答案:

答案 0 :(得分:5)

  • set visible(true);不在构造函数的旁边,并且不是有效的方法名称,应该是setVisible(true);,向上移动以使其显示在当前位于其上方的}之前。 ..
  • private JButton instantiateJButton(String, sInCaption, Color inColor)不是有效的方法签名,您在,之后添加String,但不一定是private JButton instantiateJButton(String sInCaption, Color inColor)
  • JOptionPane.showMessageDialog(null, "You pressed the "" sActionCommand + " button", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);缺少+并且"太多了。我认为它应该是JOptionPane.showMessageDialog(null, "You pressed the " + sActionCommand + " button", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
  • ProgrammingAssignment8 = new ProgrammingAssignment8("Instantiates with a Method");不是有效的作业,您实际上并未将该值分配给任何内容...... ProgrammingAssignment8 assignment = new ProgrammingAssignment8("Instantiates with a Method");之类的内容会修复它。

以下不是有效评论......

***********************************************************************************
PRIME NUMBERS CALCULATIONS
***********************************************************************************

这是......

/***********************************************************************************
PRIME NUMBERS CALCULATIONS
***********************************************************************************/
  • 我对CalculateButtonHandlerClearButtonHandlerExitButtonHandlerjbtnCalculatejbtnClearjbtnExitfontChosenFont没有任何定义,所以我不能发表评论,不用说,在写完之后它才会起作用......

答案 1 :(得分:3)

您可以做的是将素数存储在集合中。查看示例ArrayList。它就像一个数组,但是动态增长,所以你不必事先指定大小。

您可以将列表显示给用户。

相关问题