java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIExample implements ActionListener

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GUIExample implements ActionListener

Post by answerhappygod »

java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIExample implements ActionListener{
JTextField width, length;
JButton enter;
JLabel answer;
JCheckBox perimeter,area;
public GUIExample(){
//Create and set up the window.
JFrame frame = new JFrame("GUI - Geometry");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
width = new JTextField("Width");
length = new JTextField("Length");
enter = new JButton("Enter");
answer = new JLabel();
Color purple = new Color(122, 0, 122);
width.setBackground(purple);
width.setForeground(Color.WHITE);
length.setBackground(Color.RED);
//answer.setForeground(Color.CYAN);
width.setBounds(50, 50, 150, 20);
length.setBounds(50, 100, 150, 20);
enter.setBounds(120, 150, 50, 50);
answer.setBounds(80, 200, 150, 20);
enter.addActionListener(this);
perimeter = new JCheckBox("Perimeter");
area = new JCheckBox("Area");
area.setBounds(50,20, 100,20);
perimeter.setBounds(110,20, 100,20);
frame.add(perimeter);
frame.add(area);
frame.add(width);
frame.add(length);
frame.add(enter);
frame.add(answer);
frame.setSize(300,300);
frame.setLayout(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
int a=Integer.parseInt(width.getText());
int b=Integer.parseInt(length.getText());
String result = "";
if (area.isSelected()){
int areaResult = a * b;
result += "Area: ";
result += String.valueOf(areaResult);
result += "\n";
}
if (perimeter.isSelected()) {
int perimeterResult = 2*a + 2*b;
result += "Perimeter: ";
result += String.valueOf(perimeterResult);
result += "\n";
}
answer.setText(result);
} catch (Exception ex) {
answer.setText("Invalid Input");
}
}
public static void main(String[] args) {
GUIExample ex = new GUIExample();
}
Update the GUI to add a third JTextField that will be for the
height. Set the default value to "Height".
Remove the Perimeter Checkbox. Add a JCheckBox for Volume. (Make
sure to name variables in a logical way.)
Change the Area Checkbox to now calculate Surface Area of a
rectangular prism. (Surface area = 2*len*wid + 2*len*h +
2*wid*h)
Update the actionPerformed method to correctly calculate and
display the results based on the input. (Volume = len*wid*h)
Note: Feel free to change any colors! (for fun only- no extra
credit)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply