started but cant figure out STEP 3: OfficeAreaCalculator (10 points) Write a pro
ID: 3547081 • Letter: S
Question
started but cant figure out
STEP 3: OfficeAreaCalculator (10 points)
Write a program called OfficeAreaCalculator.java that displays the following prompts using two label components
Have your program accept the user input in two text fields. When a button is clicked, your program should calculate the area of the office and display the area in a text field with a label of Area. This display should be cleared whenever the input text fields receive the focus. A second button should be provided to terminate the application (Exit button).
The following tutorial shows you much of the code solution. Feel free to use the tutorial, but make changes so that you are not simply copying the tutorial code for your entire solution. To make this different from the tutorial, change the colors of the panel. Also, add this application to the same tabbed pane (see the JTabbedPane tutorial) as the application you built in Step 2, the DayGui application.
Grading Rubric
OfficeAreaCalculator
Points
Description
Standard header included
1
STEP 3: OfficeAreaCalculator (10 points)
An IGBT switch controls power to a 15 Ohm resistive load. The DC source voltage, Vs = 440V, switching frequency, fsw= 2 kHz, duty cycle, d = 0.6, tON = 20 ns, VCE(sat) = 1.5 V: find the IGBT minimum current rating on state power loss the turn on power loss Nothing to transcribeExplanation / Answer
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.*;
public class OfficeAreaCalculator extends JFrame implements ActionListener, FocusListener {
private JFrame mainFrameContainer;
private JButton btnCalculate;
private JButton btnExit;
private JTextField txtLength;
private JTextField txtWidth;
private JTextField txtArea;
private JLabel lblLength;
private JLabel lblWidth;
private JLabel lblArea;
public OfficeAreaCalculator() {
mainFrameContainer = new JFrame("Office Area Calculator");
btnCalculate = new JButton("Calculate Area");
btnExit = new JButton("Exit");
lblLength = new JLabel("Enter the length of the office:");
txtLength = new JTextField(10);
lblArea = new JLabel("Area: ");
txtArea = new JTextField(15);
txtWidth = new JTextField(10);
lblWidth = new JLabel("Enter the width of the office:");
Container container = mainFrameContainer.getContentPane();
container.setLayout(new GridLayout(0,2));
container.add(lblLength);
container.add(txtLength);
container.add(lblWidth);
container.add(txtWidth);
container.add(lblArea);
container.add(txtArea);
container.add(btnCalculate);
container.add(btnExit);
btnExit.addActionListener(this);
mainFrameContainer.setSize(400, 150);
mainFrameContainer.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
btnCalculate.addActionListener(this);
txtLength.addFocusListener(this);
txtWidth.addFocusListener(this);
txtArea.addFocusListener(this);
mainFrameContainer.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnExit)System.exit(1);
DecimalFormat num = new DecimalFormat(",###.##");
double width, length, area;
String stringText;
stringText = txtLength.getText();
if (stringText.equals("")) {
stringText = "0";
txtLength.setText("0");
}
length = Double.parseDouble(stringText);
stringText = txtWidth.getText();
if (stringText.equals("")) {
stringText = "0";
txtWidth.setText("0");
}
width = Double.parseDouble(stringText);
area = length * width;
txtArea.setText(num.format(area));
}
public void focusGained(FocusEvent e) {
if (e.getSource() == txtLength || e.getSource() == txtWidth) {
txtArea.setText("");
} else if (e.getSource() == txtArea) {
txtArea.setNextFocusableComponent(btnCalculate);
btnCalculate.grabFocus();
}
}
public void focusLost(FocusEvent e) {
if (e.getSource() == txtWidth) {
txtWidth.setNextFocusableComponent(btnCalculate);
}
}
public static void main(String args[]) {
new OfficeAreaCalculator();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.