f you cant do it dont waste my question please. thank you. JAVA Design your GUI
ID: 3813733 • Letter: F
Question
f 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
package com.chegg.programs;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class DivideByZeroGUIDemo {
private static final String FIRST_PANEL = "INPUT PANEL";
private static final String SECOND_PANEL = "RESULT PANEL";
private static final String THIRD_PANEL = "EXCEPTION PANEL";
private static final String EMPTY_PANEL = "EMPTY PANEL";
final JFrame f = new JFrame("DIVIDE BY ZERO GUI");
private CardLayout cardLayout = new CardLayout();
private JPanel content;
Double numerator=0D;
Double denominator=0D;
Double quotient=0D;
//creating textFields for user input
JTextField textBox1 = new JTextField("", 15);
JTextField textBox2 = new JTextField("", 15);
public void Starter() {
f.setSize(400, 100);
f.setLocationRelativeTo(null);
// creating labels
JLabel num1 = new JLabel("Enter Numerator: ");
JLabel num2 = new JLabel("Enter Denominator: ");
JButton calc = new JButton("Calculate");
content = (JPanel) f.getContentPane();
content.setLayout(cardLayout);
JPanel firstPanel = new JPanel();
firstPanel.setBackground(Color.white);
firstPanel.setLayout(new FlowLayout());
firstPanel.add(num1);
firstPanel.add(textBox1);
firstPanel.add(num2);
firstPanel.add(textBox2);
firstPanel.add(calc);
content.add(firstPanel, FIRST_PANEL);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (textBox2.getText().equals("") || textBox1.getText().equals("")) {
textBox1.setText("");
textBox2.setText("");
cardLayout.show(content, EMPTY_PANEL);
} else if (Integer.parseInt(textBox2.getText()) == 0) {
textBox1.setText("");
textBox2.setText("");
cardLayout.show(content, THIRD_PANEL);
} else {
numerator = Double.parseDouble(textBox1.getText());
denominator = Double.parseDouble(textBox2.getText());
content.add(createSecondPanel(numerator,denominator), SECOND_PANEL);
cardLayout.show(content, SECOND_PANEL);
}
}
});
content.add(createThirdPanel(), THIRD_PANEL);
content.add(createFourthPanel(), EMPTY_PANEL);
}
private JPanel createSecondPanel(Double num1,Double num2) {
JPanel resultPanel = new JPanel();
resultPanel.setBackground(Color.white);
resultPanel.setLayout(new FlowLayout());
quotient = num1/num2;
resultPanel.add(new JLabel("Quotient of " + num1 + " / " + num2 + " = " + quotient));
return resultPanel;
}
private JPanel createThirdPanel() {
JPanel resultPanel = new JPanel();
resultPanel.setBackground(Color.white);
resultPanel.setLayout(new FlowLayout());
resultPanel.add(new JLabel("Denominator Cant be Zero"));
resultPanel.add(new JButton(new AbstractAction("Second Chance") {
public void actionPerformed(ActionEvent e) {
cardLayout.show(content, FIRST_PANEL);
}
}));
return resultPanel;
}
private JPanel createFourthPanel() {
JPanel resultPanel = new JPanel();
resultPanel.setBackground(Color.white);
resultPanel.setLayout(new FlowLayout());
resultPanel.add(new JLabel("Values Cant be empty"));
resultPanel.add(new JButton(new AbstractAction("Second Chance") {
public void actionPerformed(ActionEvent e) {
cardLayout.show(content, FIRST_PANEL);
}
}));
return resultPanel;
}
public static void main(String[] args) {
//starting point of Application
DivideByZeroGUIDemo gf = new DivideByZeroGUIDemo();
gf.Starter();
}
}
Please provide your suggestions as comments if any.
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.