Question
IMP Timed 15 minutes Can you check if my answer is right or not? if it's not right, please modify the answer Solution: import java.awt.*;
IMP Timed 15 minutes Can you check if my answer is right or not? if it's not right, please modify the answer
Solution:
import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class GUIProgram extends JFrame implements ActionListener { final double SIZE = 44.3; Container con = getContentPane(); JButton button = new JButton("Show Results"); JLabel label = new JLabel("----"); public GUIProgram() { con.setLayout(new FlowLayout()); setSize(500, 500); //Adding action listener button.addActionListener(this); //Adding to pane con.add(button); con.add(label); //Set title setTitle("GUI Program"); pack(); setVisible(true); //allows the frame to be visible } @Override public void actionPerformed(ActionEvent e) { //Declaring variables int x=12, y=34, z=87; //Computing an average double average= (x+y+z)/3; //Writing to label label.setText("Average of these numbers is " + average); } public static void main(String[] args) { GUIProgram frame = new GUIProgram(); } }
Write a Java program to create a GUI window with a label and a button as shown in the following image. Consider three variables x, y and z with values as follows: X=12, y=34, z=87. When the user click the button, the program should display the output as follows: Average of these numbers is 44.3 The output should be displayed on the GUI window using a label. To display this output, you are required to use appropriate Java logic and other elements such as operators, loops, If-else etc. GUI Program Show Results show your results hereStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started