Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Project 3 Write a java code that creates a GUI to accept an integer

ID: 3815847 • Letter: P

Question

Programming Project 3

Write a java code that creates a GUI to accept an integer from the user as the input, and creates all the divisor of the input value. Your frame should contain the following components:

JLabel: Enter an integer to find all it divisor

JTextField: to read the input from the user

JButton: to start the process

JLabel: to show the output

Your program should handle mismatch exception.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.lang.NumberFormatException;

public class DivisorException extends JFrame implements ActionListener{

       //data fields

       //Constructor

       public DivisorException(){

      

       //methods

       public void actionPerformed(ActionEvent e){

       }

      

       private String divisor(int num){

       }

}

See sample run in below page.

Divisor Enter a number to find all its divisors find Divisor Enter a number to find all its divisors find 12. Divisor Enter a number to find allits divisors find invalid input. Enter an integer. Divisor Enter a number to find all its divisors find abcd invalid input. Enter an integer. Divisor Enter a number to find all its divisors find invalid input. Enter an integer. Divisor Enter a number to find all its divisors find invalid input. Enter an integer. Divisor Enter a number to find all its divisors find 1, 2, 3, 4, 6, 8, 12, 24

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class AL extends JFrame implements ActionListener {
       JTextField t1;/*to take text input*/
       JLabel l1,l2;/*label used for display*/
        public static void main(String[] args) {
                AL myWindow = new AL("Divisor");
                myWindow.setSize(350,125);/*hight width of window*/
                myWindow.setVisible(true);
        }

        public AL(String title)/*Initialize the field*/
        {

                super(title);
                setLayout(new FlowLayout());
                l1 = new JLabel("Enter a Number to find all its divisors");
               l2 = new JLabel("",SwingConstants.CENTER);
               t1 = new JTextField(20);
               JButton b = new JButton("Find");
                b = new JButton("find");/*adding the components to window*/
                add(l1);
                add(t1);
                add(b);
                add(l2);
                b.addActionListener(this);/*call actionPerformed method*/
        }

        public void actionPerformed(ActionEvent e) {
              
           int i;
           String ans="1";
           String number = t1.getText();
           boolean isNumeric = number.chars().allMatch( Character::isDigit );/*Input Validated*/
           if(!isNumeric)
           {
               l2.setText("Invalid Input.Enter An Integer.");
           }
           else
           {
               int num=Integer.parseInt(number);
               for (i = 2; i <= num; i++)
               {
                   if (num % i == 0)
                   {
                       ans=ans+","+i;
                   }
                  }
               l2.setText(ans);/*print Divisors*/
           }  
        }

        public void windowClosing(WindowEvent e) {
                dispose();
                System.exit(0);
        }


}