0

beginner coder here.

wrote a simple GUI to calculate the area and perimeter after inputting the information. The issue is that the labels disappear after the action event. I can't figure out what is wrong with the code that might cause it.

I put the line "setVisible(true);" at the end of the object.

import java.awt.*;
import java.awt.event.*;

public class RectangleCalc extends Frame implements ActionListener {
    private Label widthLblInput;    //label input for width
    private Label heightLblInput;   //label input for height
    private TextField tfWidth;      //text field for width
    private TextField tfHeight;     //text field for height
    private TextField tfArea;       //text field for area
    private TextField tfPerimeter;  //text field for Perimeter
    private Button btnArea;
    private Button btnPerimeter;
    private int width;
    private int height;
    private int area = 0;
    private int perimeter = 0;

    /** time for GUI setup */
    public RectangleCalc() {
        setLayout(new FlowLayout());

        // WIDTH GUI
        widthLblInput = new Label("Enter the Width: ");     // Construct label for widthInput
        add(widthLblInput);

        tfWidth = new TextField(10);                        // Add Textfield for width
        add(tfWidth);


        // HEIGHT GUI
        heightLblInput = new Label("Enter the Height: ");   // Construct label for heightInput
        add(heightLblInput);

        tfHeight = new TextField(10);                       // Add Textfield for height
        add(tfHeight);


        // AREA GUI 
        tfArea = new TextField("0", 10);                            // text field for area calculation
        tfArea.setEditable(false);
        add(tfArea);

        btnArea = new Button("Area");
        add(btnArea);
        btnArea.addActionListener(this);    


        // PERIMETER GUI
        tfPerimeter = new TextField("0", 10);                   // text field for Perimeter calculation
        tfPerimeter.setEditable(false);
        add(tfPerimeter);

        btnPerimeter = new Button("Perimeter");
        add(btnPerimeter);

        btnPerimeter.addActionListener(this);


        // WINDOW LAYOUT
        setTitle("Rectangle Calculator");
        setSize(300, 180);
        setVisible(true);

        }

    /** The entry main() method */
    public static void main(String[] args) {
        new RectangleCalc();
    }

    @Override
    /** Action Event */
    public void actionPerformed(ActionEvent evt) {

        width = Integer.parseInt(tfWidth.getText());
        height = Integer.parseInt(tfHeight.getText());

        area = (width * height);
        perimeter = ((width*2) + (height*2));

        widthLblInput.setText("");
        heightLblInput.setText("");

        tfPerimeter.setText(perimeter + "");
        tfArea.setText(area + "");

    }
}
  • `widthLblInput.setText("");` ... what do you think this might do? Also, AWT, seriously? AWT was replaced by Swing back in 2000 – MadProgrammer Oct 23 '15 at 06:43
  • Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Oct 23 '15 at 15:46

1 Answers1

0

You explicitly set your label fields to empty in your actionPerformed method:

widthLblInput.setText("");
heightLblInput.setText("");

Which will make it seem like they have disappeared.

JayVeeInCorp
  • 144
  • 1
  • 3
  • 12