Note that there are some explanatory texts on larger screens.

plurals
  1. POBasic GUI: Computing for the area and perimeter of a rectangle, code works but "variables are never read"
    text
    copied!<p>I've already come up with a GUI code that computes for the area and perimeter of a rectangle when a user inputs both the length and the width:</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RectangleGUI extends JFrame { JLabel lengthL; JLabel widthL; JLabel areaL; JLabel perimeterL; JTextField lengthTF; JTextField widthTF; JTextField areaTF; JTextField perimeterTF; JButton CalculateB; JButton ExitB; CalculateButtonHandler cbHandler; ExitButtonHandler ebHandler; public RectangleGUI() { setTitle("Area and Perimeter of a Rectangle"); lengthL = new JLabel("Enter the length: ", JLabel.RIGHT); widthL = new JLabel ("Enter the width: ", JLabel.RIGHT); areaL = new JLabel ("Area: ", JLabel.RIGHT); perimeterL = new JLabel ("Perimeter: ", JLabel.RIGHT); lengthTF = new JTextField(10); widthTF = new JTextField (10); areaTF = new JTextField (10); perimeterTF = new JTextField (10); CalculateB = new JButton("Calculate"); cbHandler = new CalculateButtonHandler(); CalculateB.addActionListener(cbHandler); ExitB = new JButton("Exit"); ebHandler = new ExitButtonHandler(); ExitB.addActionListener(ebHandler); Container pane = getContentPane(); pane.setLayout(new GridLayout(5,2)); pane.add(lengthL); pane.add(lengthTF); pane.add(widthL); pane.add(widthTF); pane.add(areaL); pane.add(areaTF); pane.add(perimeterL); pane.add(perimeterTF); pane.add(CalculateB); pane.add(ExitB); setSize(400,300); setVisible(true); } public class CalculateButtonHandler implements ActionListener { public void actionPerformed (ActionEvent e) { double l, w, area, peri; l=Double.parseDouble(lengthTF.getText()); w=Double.parseDouble(widthTF.getText()); area=l*w; peri=2*(l+w); areaTF.setText("" + area); perimeterTF.setText("" + peri); } } public class ExitButtonHandler implements ActionListener { public void actionPerformed (ActionEvent e) { System.exit(0); } } public static void main(String[] args) { RectangleGUI rectObject = new RectangleGUI(); } } </code></pre> <p>This code should run on the console but it bugs me that on Eclipse, <code>RectangleGUI</code> in <code>public class RectangleGUI extends JFrame {</code> is underlined with a yellow squiggly line because "The serializable class RectangleGUI does not declare a static final serialVersionUID field of type long".</p> <p>In the latter part of the code, <code>rectObject</code> is also underlined because the variable is apparently "never read". I know I should use this variable at one point but when I try to, the code messes up and won't work again.</p> <p>I would like to understand why this is happening. I am only a beginner and I want to be able to avoid these mistakes in the future.</p> <p>ALSO: During run-time, I tried not to put a value in the "width" text field and the Eclipse console returned an error message. What can I do that instead of Eclipse alerting me that something is wrong, a GUI prompt will pop-up telling me to put a value in "width"? I've tried experimenting and failed time and time again.</p> <p>This is not homework, but I am discovering GUI for the first time, and a little insight on why the errors appear the way they are would be very much appreciated. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload