KillianO92

PrimeNumbers JFrame

Dec 19th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class PrimeNumbersJ extends JFrame
  6. {
  7.     private static final int WIDTH=400;
  8.     private static final int HEIGHT=300;
  9.  
  10.     //JFrame Components
  11.     private JLabel jlblMaxNumber;
  12.  
  13.     private JTextArea  jtaOutput;
  14.  
  15.     private JTextField jtfMaxNumber;
  16.  
  17.     private JButton jbtnCalculate, jbtnClear, jbtnExit;
  18.  
  19.     private CalculateButtonHandler calculateHandler;
  20.     private ClearButtonHandler  clearHandler;
  21.     private ExitButtonHandler   exitHandler;
  22.  
  23.     private JScrollPane scrollingResult;
  24.  
  25.     private JPanel jpnlTop = new JPanel();
  26.     private JPanel jpnlCenter = new JPanel();
  27.     private JPanel jpnlBottom = new JPanel();
  28.  
  29.  
  30.     public PrimeNumbersJ()
  31.     {
  32.         // Set the title and Size:
  33.         setTitle("Prime Numbers with JFrame");
  34.         setSize(WIDTH, HEIGHT);
  35.         jpnlBottom.setLayout(new GridLayout(1, 3));
  36.  
  37.         // Instantiate the JLabel components:
  38.         jlblMaxNumber = new JLabel("Enter the Largest Number to test: ", SwingConstants.LEFT);
  39.  
  40.         // Instantiate the JTextFields:
  41.         jtfMaxNumber = new JTextField(10);
  42.  
  43.         // Make the JTextArea scrollable:
  44.         jtaOutput = new JTextArea(10,1);
  45.         scrollingResult = new JScrollPane(jtaOutput);
  46.  
  47.         // Instantiate and register the Calculate button for clicks events:
  48.         jbtnCalculate = new JButton("Calculate");
  49.         calculateHandler = new CalculateButtonHandler();
  50.         jbtnCalculate.addActionListener(calculateHandler);
  51.  
  52.         // Instantiate and register the Clear button for clicks events:
  53.         jbtnClear = new JButton("Clear");
  54.         clearHandler = new ClearButtonHandler();
  55.         jbtnClear.addActionListener(clearHandler);
  56.  
  57.         // Instantiate and register the Exit button for clicks events:
  58.         jbtnExit = new JButton("Exit");
  59.         exitHandler = new ExitButtonHandler();
  60.         jbtnExit.addActionListener(exitHandler);
  61.  
  62.         // Assemble the JPanels:
  63.         jpnlTop.setLayout(new GridLayout(1, 2));
  64.         jpnlTop.add(jlblMaxNumber);
  65.         jpnlTop.add(jtfMaxNumber);
  66.  
  67.         jpnlCenter.setLayout(new GridLayout(1, 1));
  68.         jpnlCenter.add(scrollingResult);
  69.  
  70.         jpnlBottom.setLayout(new GridLayout(1, 3));
  71.         jpnlBottom.add(jbtnCalculate);
  72.         jpnlBottom.add(jbtnClear);
  73.         jpnlBottom.add(jbtnExit);
  74.  
  75.         // Start to add the components to the JFrame:
  76.         Container pane = getContentPane();
  77.         pane.setLayout(new BorderLayout());
  78.  
  79.         pane.add(jpnlTop, BorderLayout.NORTH);
  80.         pane.add(jpnlCenter, BorderLayout.CENTER);
  81.         pane.add(jpnlBottom, BorderLayout.SOUTH);
  82.  
  83.  
  84.         // Show the JFrame and set code to respond to the user clicking on the X:
  85.         setVisible(true);
  86.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  87.  
  88.         jpnlTop.setLayout(new GridLayout(1, 3));
  89.         jpnlTop.add(jlblMaxNumber);
  90.         jpnlTop.add(jtfMaxNumber);
  91.  
  92.         jpnlCenter.setLayout(new GridLayout(1, 1));
  93.         jpnlCenter.add(scrollingResult);
  94.  
  95.         jpnlBottom.add(jbtnCalculate);
  96.         jpnlBottom.add(jbtnClear);
  97.         jpnlBottom.add(jbtnExit);
  98.  
  99.         // Show the JFrame and set code to respond to the user clicking on the X:
  100.         setVisible(true);
  101.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  102.     }//End Constructor
  103.  
  104.  
  105.     private class CalculateButtonHandler implements ActionListener
  106.     {
  107.         public void actionPerformed(ActionEvent e)
  108.         {
  109.             int iRemainder,iPrimeCheck;
  110.             int iNumbertoTest = 0;
  111.             boolean bValidInput = true;
  112.             String sPrime ="";
  113.  
  114.             try
  115.             {
  116.                 iNumbertoTest = Integer.parseInteger(jtfMaxNumber.getText());
  117.             }
  118.             catch (Exception aeRef)
  119.             {
  120.                 JOptionPane.showMessageDialog(null,"Enter the Max Number to Test.", getTitle(), JOptionPane.WARNING_MESSAGE);
  121.                 bValidInput = false;
  122.             }// end of catch
  123.  
  124.  
  125.             if ( bValidInput )
  126.             {
  127.  
  128.                 for(iNumberToTest = 1;iNumberToTest <= 100;iNumberToTest++){
  129.                     iRemainder = 0;
  130.                     for(iPrimeCheck = 1;iPrimeCheck <= iNumberToTest;iPrimeCheck++){
  131.                         if(iNumberToTest % iPrimeCheck == 0){
  132.                                 iRemainder++;
  133.                             }
  134.                         }
  135.                         if(iRemainder == 2 || iNumberToTest == 1)
  136.                         {
  137.                                 String sNumber = Integer.toString(iNumberToTest);
  138.                                 sPrime = sPrime + (sNumber + "\n");
  139.                         }
  140.  
  141.  
  142.             }
  143.                 // Populate the output by using the methods in the user defined class::
  144.  
  145.                 jtaOutput.append("The Prime Numbers Are: \n" + sPrime  + "\n");
  146.             } // end if
  147.         } //end ActionPerformed
  148.     }//End CalculateButtonHandler
  149.  
  150.     private class ExitButtonHandler implements ActionListener
  151.     {
  152.         public void actionPerformed(ActionEvent e)
  153.         {
  154.             System.exit(0);
  155.         }
  156.     }//end ExitButtonHandler
  157.  
  158.  
  159.     private class ClearButtonHandler implements ActionListener
  160.     {
  161.         public void actionPerformed(ActionEvent e)
  162.         {
  163.             jtfMaxNumber.setText("");
  164.             jtaOutput.setText("");
  165.          }
  166.     } // end ClearButtonHandler
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.     public static void main(String args[])
  175.     {
  176.         // The only thing this main does is Instantiate the JFrame:
  177.         // Note the name of the object is the name of the class from above.
  178.         PrimeNumbersJ primNumJ = new PrimeNumbersJ();
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment