begining gui programming with java

Discussion in 'Programming General' started by paladen, Jun 11, 2007.

begining gui programming with java
  1. Unread #1 - Jun 11, 2007 at 12:50 AM
  2. paladen
    Joined:
    Jan 21, 2007
    Posts:
    1,633
    Referrals:
    1
    Sythe Gold:
    0

    paladen Guru
    Banned

    begining gui programming with java

    reposted, no idea who orignal maker was.

    Ok, we're really starting now.

    javax.swing is a package (meaning a collection of class files) that is used to create GUIs (Graphical User Interfaces). These are very useful. Without GUIs we would still be using DOS. Swing features a changable "Look and Feel". This basicly means you can change how things look.

    Firstly we're going to create a JFrame (this is like a window). It appears on the screen and hase close, minimize and restore/maximize buttons on the top right corner. We create on of these by doing this:

    Create a class file. Call it JFrameDemo.java (remember java is case sensitive). Open up the file and write this code:

    import javax.swing.*; // Telling java where to find the Swing class files

    public class JFrameDemo extends JFrame {The import javax.swing.*; line just tells the compiler where to find the files we're going to use.
    The public class JFrameDemo part tells us what the classfile (the compiled file) should be called.
    The extends JFrame part { tells us that it extends (adds onto) the classfile called JFrame in javax.swing (javax.swing.JFrame).

    Ok, now we're going to make the program do something when it is run. Add this code at the bottom of the file:

    public static void main(String args[]) {
    JFrameDemo view = new JFrameDemo();
    view.setVisible(true);
    }Ok, if you've read David's tutorial, you'll know what the public static void main part does, but you might not know what the JFrameDemo view = new JFrameDemo(); part means. It is simply a variable. It will make our JFrame appear on screen.

    The view.setVisible(true); part makes it so we can actually see our frame.

    Now, add this underneat the "main" method:

    public JFrameDemo() {
    this.setSize(100,100);
    this.setLocation(100,100);
    //...Now you might know what this method does. This is called a 'constructor'. It tells the java program how to make our JFrame and what to put in it. The 'this' means that that line of code is referring to the JFrame.
    I would think that the setSize and setLocation parts are obvious, but I'll explain anyway.

    The setSize part tells java how big our frame should be. The syntax is setSize(x,y); x being length and y being height.

    The setLocation part tell java where to place our JFrame. Once again, the syntax is setLocation(x,y); x being distance from the left of screen and y being distance from top of screen. Keep in mind that all measurements are in pixels.

    Moving on, we're going to add a JLabel to our frame. A JLabel is just basicly a peice of writing that will be displayed in the JFrame. They are relatively simple to add. Add this at the bottom of what we just added:

    JLabel label = new JLabel("z0mg! a fr4m3!");
    this.add(label);
    //...You might recognize that we have declared another variable (I really hope you have). And you probably have recognized that we have used 'this' again. What we have actually done is added the JLabel (a.k.a label) to our JFrame.

    We are almost finished now. Just a few more things to do. Add this:

    }
    }

    We have now closed up all the curly braces which means our .java file is complete. Compile this file and hopefull it should run.
     
  3. Unread #2 - Jun 11, 2007 at 8:59 AM
  4. WoW Sucks
    Joined:
    Jan 21, 2007
    Posts:
    3,708
    Referrals:
    3
    Sythe Gold:
    0

    WoW Sucks Global Moderator
    Banned

    begining gui programming with java

    Put the code in code boxes.
     
< java 2nd stage | Media player trial >

Users viewing this thread
1 guest


 
 
Adblock breaks this site