If you cant do it dont waste my question please. thank you. JAVA Design your GUI
ID: 3813089 • Letter: I
Question
If you cant do it dont waste my question please. thank you.
JAVA
Design your GUI to follow the functionality of the the original program
Above you should see DivideByZeroDemo, DivideByZeroException. The two(2) DivideByZero files work together as one application. You are to create GUIs for this program. It must allow user input and program output. Design your GUI to follow the functionality of the the original programs. In other words, familiarize yourself with how each of the programs work before attempting your GUI version. This time try to make your GUIs user friendly. Tell the user what they must do and what outcome to expect.
Please add comments to code
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Myframe extends JFrame implements ActionListener
{
JLabel l1,l2,l3,msg;
JButton b1;
JTextField t1,t2,t3;
int chance=0; //it counts the chances
public Myframe() //constructor
{
l1=new JLabel("ENTER NUMERATOR");
l2=new JLabel("ENTER DENOMINATOR");
l3=new JLabel("QUOTIENT :");
b1=new JButton("DIVIDE");
msg=new JLabel();
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(msg);
b1.addActionListener(this);
setSize(300,200);
setLayout(new FlowLayout());
setTitle("Divide By Zero");
}//end of Myframe
public void giveSecondChance( ) //Second chance method
{
t1.setText("");
t2.setText("");
msg.setText("Be sure the denominator is not zero.");
msg.repaint();
}
public void actionPerformed(ActionEvent ae)
{
float a,b,c;
if(ae.getSource()==b1)
{
try
{
a=Float.parseFloat(t1.getText());
b=Float.parseFloat(t2.getText());
if (b== 0)
{
chance+=1; //increment the chance
throw new DivideByZeroException( ); //throwing exeception
}
c=a/b;
t3.setText(String.valueOf(c));
}
catch(DivideByZeroException e) //catch the thrown exception
{
if(chance==1)
{
msg.setText(e.getMessage());
msg.repaint();
giveSecondChance( );
}
if(chance==2)
{
String s="<html><font color=red size=3>"+ "<ul><li>I cannot do division by
zero.</li>"+"<li>Since I cannot do what you want,</li>"+"<li>the program will
now end.</li>"+"</ul></font></html>";
msg.setText(s);
msg.repaint();
}
}
}
}
}
// Divide By Zero Test class
public class DivideByZeroTest
{
public static void main(String args[])
{
Myframe f=new Myframe(); //creating object of the Myframe class
f.setVisible(true);
f.setLocation(250,200);
}
}
//DivideByZeroException
class DivideByZeroException extends Exception
{
public DivideByZeroException( )
{
super("Dividing by Zero!");
}
public DivideByZeroException(String message)
{
super(message);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.