Lab01
Hello Text Box Square Root
Text Box:  4  import javax.swing.*;   5  import java.awt.*;   6  import java.awt.event.*;   7  public class Panel01 extends JPanel   8   {   9    private JLabel label1; 		 //field  10    private JTextField box;        //field  11    public Panel01()  12      {  13      setLayout(new FlowLayout());  14  15      textbox = new JTextField('0.0', 10);  16      box.setHorizontalAlignment(SwingConstants.RIGHT);  17      add(box);  18  19      JButton button = new JButton('SQRT');   //local  20      button.addActionListener(new Listener());  21      add(button);  22  23      label1 = new JLabel('0.0');  24      label1.setFont(new Font('Serif', Font.BOLD, 20));  25      label1.setForeground(Color.blue);  26      add(label1);  27      }  28    private class Listener implements ActionListener  29      {  30      public void actionPerformed(ActionEvent e)  31       {  32           //get the number  33           //take the square root  34           //display it   35        }  36      }  37   }Objective
Fields vs. local variables

Background

Lines 1-3 shoudl have comments that include your name and the date.

Lines 4-6 import requried libraries
Lines 9-10:  These 2 declarations create private fields because the fields need to be accessed later in the listener.
Line 13:  FlowLayout is the simplest GUI layout. The first component added to the panel is placed at the center top.  Additional components are placed to the right, or on the next line.   Be sure to drag the corner of the frame and watch how the flow works.
Line 15-31.  Notice the  different components are instantiated, formatted, and added to the panel.  Components appear in the order in which they are added.
Line 19:  This button is declared local because it does not need to be accessed in the listener, unlike the label and the textfield.
Specification
Filename Driver01.java.  Create an appropriate driver.  Make sure to add a panel object of type Panel01. 
Filename Panel01.java.  Implement the method actionPerformed() to find square roots. 

What happens if you type a negative number into the text box?
image3

 

Extension
Edit your program to handle negative numbers correctly, i.e., returns 5i.  You will need an if-statement.
Warning! A common error is to write Line 15 as     JTextField box = new JTextField("0.0", 10);  Write Line 15 with this mistake.  Compile and run.   Explain the error message.

panel