GUI for Math Operations ( In JAVA using WidgetView , in simple beginner java) A
ID: 3887765 • Letter: G
Question
GUI for Math Operations ( In JAVA using WidgetView , in simple beginner java) A couple people posted this same question and I also posted this earlier today but got an answer that did not show the whole code and did not use WidgetView, could someone please read carefully and show me the correct code USING WIDGETVIEW. thanks in advance.
Write a program that asks the user for 2 integer numbers and displays those numbers added, subtracted, multiplied, divided and modulo.
Use a WidgetView object.
In your GUI, display a label containing instructions to the user such as "Enter two ints and I'll perform math operations on them."
Create two text fields for the user to enter the numbers.
Use a button for the user to indicate that he has entered the numbers.
When the user clicks the button, display labels showing the results of each operation with an indication of the operation performed, such as "adding 7 and 8 is 15."
Explanation / Answer
import java.awt.*; //importing all awt packages and event clases also
import java.awt.event.*;
import javaX.swing.JFrame
import javaX.swing.JButton
import javaX.swing.JLabel
import javaX.swing.JOptionPane
public class Arithmetic extends JFrame{
private JTextField n11TextField;
private JTextField n22TextField;
public static voi main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
Arithmetic fr=new Arithmetic(); //object creation
fr.setVisible(true);
}});
}
public Arithmetic(){
setDefaultCloseOperation(JFrame.exit_on_close);
setBounds(150,150,300,300);
setTitle(“Arithmetic operations”);
getContentPane().setLayout(null);
//labels
JLabel inst=new Jlabel(“enter two instructions and perform operations on them”);
inst.setBounds(80,40,100,20);
getContentPane().add(inst);
//first number label
JLabel n1=new JLabel(“n1”);
n1.setBounds(30,40,20,30);
getContentPane().add(n1);
//for second number
JLabel n2=new JLabel("n2");
n2.setBounds(30,40,20,30);
getContentPane().add(n2);
n1TextField=new JTextField();
n1TextField.setBounds(200,80,50,120);
getContentPane().add(n1TextField);
n2TextField=new JTextField();
n2TextField.setBounds(200,80,50,120);
getContentPane().add(n2TextField);
JButton submit=new JButton(“enter”);
Submit,addActionListener(new ActionListener(){
Public void actionPerformed(ActionEvent ag){
PerformOperations();
}});
Submit.setBounds(200,80,50,120);
getContentPane().add(submit);
}
Public void PerformOperations()
{
String n1=n1.TextField.getText();
String n2=n2TextField.getText();
If(n1.equals(“”))
{ //conditional checking for entering number
JOptionPane.shoeMessage(null,please enter n1”);
n1TextField.requestFocusInWindows();
} If(n2.equals(“”))
{ //conditional checking for entering number
JOptionPane.shoeMessage(null,please enter n2”);
n2TextField.requestFocusInWindows();
}
If(!n1.equalsIgnoreCase(“”)&&!n2.equalsIgnoreCase(“”))
{
int n11=Integer.parseInt(n1); //converting into integers
int n22=Integer.parseInt(n2);
Int add=n11+n22; //adding
Jlabel addition=new JLabel(“ “+n11+”and”+n22”+add);
Addition.setBounds(150,70,50,120);
getContentPane().add(addition);
int sub=n11-n22; //sutraction
Jlabel subtraction=new JLabel(“ “+n11+”and”+n22”+sub);
subtraction.setBounds(150,70,50,120);
getContentPane().add(subtraction);
int mul=n11*n22; //multiplication
Jlabel multi=new JLabel(“ “+n11+”and”+n22”+mul);
multi.setBounds(150,70,50,120);
getContentPane().add(multi);
float div=(float)n11/n22; //division
Jlabel division=new JLabel(“ “+n11+”and”+n22”+div);
division.setBounds(150,70,50,120);
getContentPane().add(division);
}}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.