GUI Objects - JLabels, JTextFields, JButton, Font,
Color

JLabel       Declaration and methods

JLabel L1;       Declares the variable name of JLabels, JLabels can hold words or icons, used for output only.

L1 = new JLabel("Hello World");    or           L1 = new JLabel( );
                
Creates the JLabel object. The JLabel is not visible until it is added to the container. 
container.add ( L1 );          Adds the JLabel to the container (window).    
L1.setForeground(Color.red);        Sets the color of text
  Choices of Color are: Color.black, Color.white, Color.darkGray,  Color.gray, Color.lightGray,
 Color.blue, Color.cyan,Color.green, Color.magenta,  Color.orange, Color.pink, Color.red, , Color.yellow 
L1.setToolTipText("Tool Tip Message ");
                Enables a Tip Box which to appear when the mouse is run over the JLabel in the Container.
L1.setIcon(new ImageIcon("home.gif"));
             Display a picture on the JLabel..  The gif or jpeg must be stored in the same folder as the program code
L1.setHorizontalTextPosition(SwingConstants.CENTER);
         
Enables the Text of the JLabel to be justified horizontally  LEFT, RIGHT or CENTER of the Icon. 
L1.setVerticalTextPosition(SwingConstants.BOTTOM);
              
Enables the Text of the JLabel to be justified vertically  BOTTOM, TOP or CENTER of the Icon
To color the background of a JLabel, you must color the background of the container or the JPanel.
        container.setBackground(Color.green);
L1.setText("this will go in label");  - useful if you want to change a JLabel in a conditional
                
Prints text in a JLabel  - not used very often

top

JTextfields          Declaration and methods

JTextField t1; 
               Declares the variable name of JTextFields JLabels allows input data from the keyboard. ( can be used for output ) 
t1 = new JTextField( );            
 t1 = new JTextField( 9); 
                  Creates JTextField with column width of 9
t1 = new JTextField("Hello" );        Creates the JTextField object with the word Hello
t1.setColumns(10);              Sets the number of columns (letters) of the JTextField
container.add(t1);                Adds the JTextField to the container (window).   
t1.setForeground(Color.blue);           Sets the color of the Text in the JTextField. 
t1.setBackground(Color.yellow);      Sets the color of the JTextField background. 
t1.setToolTipText("Tool Tip Text"); 
     
       Enables a Tip Box which to appear when the mouse is run over the TextField in the Container. 
t1.setText("This is my text field ");   Prints text in a TextField. - very important when using conditionals
s1 = t1.getText( );     Retrieves whatever is typed in the JTextfield - s1 must be a String variable
t1.setHorizontalAlignment(JTextField.CENTER);  - We will not use this in class
            Enables the Text of the Label to be justified horizontally LEFT, RIGHT or CENTER in the TextField. 
            Other choices of justification are; 

                   t1.setHorizontalAlignment(JTextField.LEFT); 
           t1.setHorizontalAlignment(JTextField.CENTER); 
           t1.setHorizontalAlignment(JTextField.RIGHT);
 

top

 

JButton      Declaration and methods

JButton b1;    Declares the variable name of JButtons JButtons will be used for input and output.

b1 = new JButton( );        or             b1 = new JButton("click me");
          Use to create the JButton object with or without writing.

container.add(b1); Adds the JButton to the container (window).

b1.setForeground(Color.blue);           Changes color of the text
b1.setBackground(Color.green);        Changes color of the background.
b1.setToolTipText("Click here please");
          Enables a ToolTip Box which to appear when the mouse is run over the Button in the Container.
b1.setIcon(new ImageIcon("apple.gif"));       Adds an Icon to the Button.
b1.setHorizontalTextPosition(SwingConstants.LEFT);
            Enables the Text of the JButton to be justified vertically  LEFT, RIGHT or CENTER of the Icon
b1.setVerticalTextPosition(SwingConstants.BOTTOM);
           Enables the Text of the JButton to be justified vertically  BOTTOM, TOP or CENTER of the Icon 
b1.setRolloverIcon(new ImageIcon("star.gif");
          Adds a RollOver Icon to the Button. This icon will appear on top of the current icon when the mouse is moved over the Button.
 
b1.setEnabled(false);      turns off a button so it can no longer be clicked
b1.setEnabled(true);       turns on a button that was turned off

To listen to JButtons - 3 parts are necessary
(a)   need to add after the word JFrame
    implements ActionListener
(b)   to listen to JButton         b1.addActionListener(this); - necessary for action to occur
(c)   and you must have an actionPerformed method

public void actionPerformed(ActionEvent e)
{
 if(e.getSource()==b1)
   {

    }// end of b1

} // end of action performed

 

b1.setFocusPainted(false);     Optional - Removes the purple ring around the button
  b1.setBorder(BorderFactory.createLoweredBevelBorder( ));       Optional 
           Changes the border surrounding the button.  Other Border Options are; - use only 1
                  b1.setBorder(BorderFactory.createLineBorder(Color.orange));
          b1.setBorder(BorderFactory.createRaisedBevelBorder( ));
          b1.setBorder(BorderFactory.createEtchedBorder( ));
          b1.setBorder(BorderFactory.createEmptyBorder( ));

 

top