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

I am writing a program that calculates prime numbers and I want to make several

ID: 3678697 • Letter: I

Question

I am writing a program that calculates prime numbers and I want to make several adjustments (code below):

1. Add an output field that displays the results in the same GUI

2.Prints a dialog box with an error message if input is not a number

3. Getting the program to close when "cancel" is pressed.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.JMenuBar.*;

public class PrimeGui extends JFrame implements TextListener,ActionListener
{
  
JLabel l1,l2;
TextField t1;
JButton submit,reset;
String numstr;
int num,ctr = 0,n;
JColorChooser tcc;

public PrimeGui()

{
super(" Prime Numbers Verifier ");
setLayout(new FlowLayout());
getContentPane().setBackground(Color.WHITE);
l1 = new JLabel(" Enter A Number ");
add(l1);
t1 = new TextField(10);
add(t1);
t1.setForeground(Color.black);
submit = new JButton(" Submit ");
submit.setMnemonic(KeyEvent.VK_S);
submit.setFocusPainted(true);
add(submit);
  
reset = new JButton (" Reset ");
reset.setOpaque(true);
add(reset);
  
t1.addTextListener(this);
submit.addActionListener(this);
  
}
  
public void textValueChanged(TextEvent te)
  
{
if (te.getSource() == t1)
{
numstr = t1.getText();
num = Integer.parseInt(numstr);
}
}
  
public void actionPerformed(ActionEvent ae )

{
  
if(ae.getSource() == submit)

{
  
for (n=num;n>=1;n--)
{
if (num % n == 0)

{
ctr++;
}
}
if ( ctr <=2 )
{
JOptionPane.showMessageDialog(null,num+" is a prime number");
reset.setVisible(false);
remove(reset);

}
else
{
JOptionPane.showMessageDialog(null,num+" is not a prime number");
}
  
}
}
  
  
public static void main (String args[])

{

PrimeGui ob = new PrimeGui();
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(230,100);
  
ob.setLocation(350,300);
ob.setVisible(true);

}
  
  
  
}

Explanation / Answer

b)

c)

jFrame.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

        handleClosing();

    }

});

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