JavaWorld

Tuesday, July 08, 2008

Créating sweet interfaces with Swing and Awt

Hi
 
I really enjoyed using AWT and using to create some nices user interfaces while training some trainers this week.
I came back to the old time coding style, with no graphic forms editor in Eclipse, just coding everything with your hands.
So I showed my students how to do nice things from out of nowhere.
We began by a quick tips on the Applets then we continued by presenting basic usage of Swing and AWT, and first of all the import of these namespaces
 
import javax.swing.*;
import java.awt.*;
 
Then we continued by looking at how I can create a window that will pop up on my screen: the steps are
 
1. Create a class with a static void main method in it (entry point)
 
2. déclare a JFrame variable in it and instanciate it and pass a form title parameter in it like it
    JFrame jf = new JFrame("My first frame");
 
3. Third I get the Panel that this frame uses to draw things in itself with
    Container myPanel = jf.getContentPane();
 
4. I set up the kind of Layou I am gonna use to insert things in this panel for exemple I can use a FlowLayout mode this way:
    myPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2,2)
 
5. Now I can insert things in this panel like this:
   myPanel.add (new JTextField (10));
   myPanel.add(new JButton("Validate"));
 
6. When I am done with the panel I pack the frame
    jf.pack()
 
7. I define the behavior of the application when the form is closed
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE_OPTION);
 
8. Finally I let the form show itself by calling
   jf.show();
 
Wasn't that nice ??

--
Alain Lompo
Excelta - Conseils et services informatiques
MCT
MCSD For Microsoft .Net
MVP Windows Systems Server / Biztalk Server
Certifié ITIL et Microsoft Biztalk Server

0 Comments:

Post a Comment

<< Home