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

Write a java code that creates a GUI to accept an integer from the user as the i

ID: 3693720 • Letter: W

Question

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){

       }

}

Explanation / Answer

package appu;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class DivisorException extends JFrame {
   /**
       *
       */
   private static final long serialVersionUID = 1L;
   private JTextField number;
   private JLabel numberLabel = new JLabel("Enter number");;
   private JLabel outputLabel;
   private JButton run;
   private JPanel mainPanel = new JPanel();

   public DivisorException() {

       setLayout(new FlowLayout());

       mainPanel.add(numberLabel);
       number = new JTextField(10);
       mainPanel.add(number);
       outputLabel = new JLabel("");
       mainPanel.add(outputLabel);
       run = new JButton("Run");
       mainPanel.add(run);
       run.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent e) {
               // TODO Auto-generated method stub
               outputLabel.setText(divisor(Integer.parseInt(number.getText())));
           }
       });
   }

   private String divisor(int num) {
       String divisors = "";
       for (int i = 2; i <= num / 2; i++) {
           if (num % i == 0) {
               divisors = divisors.concat(i + " , ");
           }
       }
       return divisors;

   }

   public JComponent getMainComponent() {
       return mainPanel;
   }

   private static void createAndShowGui() {
       DivisorException e = new DivisorException();

       // creating my JFrame only when I need it and where I need it
       JFrame frame = new JFrame("Divisor");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.getContentPane().add(e.getMainComponent());
       frame.pack();
       frame.setLocationByPlatform(true);
       frame.setVisible(true);
   }

   public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGui();
           }
       });
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote