From: Subject: GUIs part 2 Date: Wed, 30 Mar 2005 09:45:14 -0600 MIME-Version: 1.0 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Location: http://scitech.stisd.net/users/lois.kertesz/unit4/GUIpart2.htm X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 =EF=BB=BF GUIs part 2
More GUI commands
Layouts , JPanel,=20 ActionListener,=20 Fonts,=20 Colors,=20 Label=20 Background

Layouts

There = are 3=20 different kinds to use
1.   container.setLayout(new=20 FlowLayout()); 
   
 The Objects in the = container=20 will move as the window changes size

2.   container.setLayout(new=20 = GridLayout(3,2)); 
  &n= bsp;  This creates a grid in the = container=20 that is 3 rows by 2 columns - each cell is the same=20 size.
     If 6 Objects are not added = to the=20 container, space will be left for additional=20 Objects.
     If more that 6 Objects = are added=20 to the container, the number of columns will increase

3.  container.setLayout(new = BorderLayout()); 
     This=20 creates five cells of different sizes - top, bottom, right, left = and=20 middle.
     Objects are added using = one of=20 the constants NORTH, SOUTH, EAST, WEST,=20 CENTER
     Here is an example of an = add=20 line:
        container.add(b3,BorderLayout.SOUTH); =20
    If one fails to use the above command, all Objects = go to the=20 center cell.

 

JPanel

JPanel is a = subcontainer. It=20 is created the same as other Objects

JPanel p1,=20 p2;
    Declares the Object = variable

p1 =3D new JPanel(new=20 = GridLayout(3,1));
       = ;       or
p2 =3D new JPanel(new=20 BorderLayout());
   
Creates an instance of the = JPanel=20 Object

p1.add(b2);
    &= nbsp;Adds a JButton to JPanel=20 p1

p2.add(b3,=20 = BorderLayout.NORTH);
     &nb= sp;adds a JButton to p2 which = has a=20 borderlayout

To color the background of a = JLabel

If the JLabel is = added to=20 the container, then use a line like this in the container=20 location
  container.setBackground(Color.green);

= If=20 the JLabel is added to a JPanel, then use a line like this after = the=20 JPanel has been created
  p1.setBackground(Color.green); =


ActionListeners

1. The public = class line=20 changes from   public class Lab5c3 extends JFrame=20
to =   public class = Lab5c3=20 extends JFrame implements ActionListener=20

2. add an = ActionListner to=20 any JButton where some action must take place
    b1.addActionListener(this); =

3. add a method = before the=20 main method
public void = actionPerformed(ActionEvent e)=20 =
{
    if(e.getSource()=3D=3Db1)
  = ;   {
       //do=20 something like get the text from a=20 JTextfield
      =20 =  s1=3Dt1.getText();
       if(= s1.equals("cupcake"))=20 =
        {
  &nbs= p;      //do=20 = something
        }
 &= nbsp;     else
     =   {
         //= do=20 something else
         = }
 }

4. If you wish to = deactivite=20 the JButton
      = ;b1.setEnabled(false);=20 //turn off button  

 

Fonts

x.setFont(new = Font(font name as StringFont.style,  size));

x is an object like = L2, or b1 or=20 t3
font name as String 
is a = font name=20 like "Ariel"  or = "MS San Serif"
Font.style 
is usually  = Font.BOLD or Font.PLAIN  or  Font.ITALIC
size 
is an integer - larger = number means=20 larger size 

Samples:
t1.setFont(new = Font("Century", Font.BOLD,15));
b2.setFont(new=20 Font("Comic Sans MS", Font.PLAIN,20));
L1.setFont(new = Font("Ariel",=20 Font.ITALIC,25));

Colors

Use a = command like=20 this to change the background of a JButton, JTextField, = container or=20 JPanel

The numbers = represent=20 the amount of RED, GREEN and BLUE in the color

x.setBackground(new Color (=20 55,127,80));

This applet = on the=20 right will help you choose colors

=20