Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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[])
- {
- // The only thing this main does is Instantiate the JFrame:
- // Note the name of the object is the name of the class from above.
- PrimeNumbersJ primNumJ = new PrimeNumbersJ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment