Java GUI - by Unknown

Discussion in 'Archives' started by Six, May 12, 2007.

Java GUI - by Unknown
  1. Unread #1 - May 12, 2007 at 11:37 AM
  2. Six
    Joined:
    Jan 21, 2007
    Posts:
    1,482
    Referrals:
    4
    Sythe Gold:
    12

    Six Guru

    Java GUI - by Unknown

    Java GUI

    Making a Little Java Program

    Sec. 1: Imports and starting it off
    Sec. 2: Variables
    Sec. 3: Frame and Stuff
    Sec. 4: Declaring buttons
    Sec. 5: Adding buttons
    Sec. 6: Action Listening
    Sec. 7: Using this for a learning experience

    Section 1

    Now, let's think. What imports do we need? We obviously need GUI imports. We also need the action Listener. So,
    let's declare this at the very top:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    That's all we need to get all our supplies. Now to start us off.
    Skip a couple lines and add:

    Code:
    public class Tutorial extends JFrame implements ActionListener
          {

    Section 2

    Let's declare our variables.
    Right below the class, add

    Code:
            private static Cool frame;
           private static JTextField text;
    I will tell you about this later.

    Section 3

    This is in all java programs. To start it and load everything, you must add:

    Code:
    public static main(String[] args)
    {

    We must now add the frame, so skip a 2 lines and add:

    Code:
        frame = new Tutorial();
              frame.setTitle("Tutorial of GUI");
              frame.setSize(400, 100);
              frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
              frame.setVisible(true);
    Ending the statement, add one last "}"

    Section 4

    Now add:

    Code:
    public Tutorial()
       {
    Next add:

    Code:
               setLayout(new BorderLayout());
    
                  JTextField text = new JTextField(10);
                  JButton button = new JButton("JButton1");
                  JButton button1 = new JButton("JButton2");
                  JButton button2 = new JButton("JButton3");
                  JButton button3 = new JButton("JButton4");
    
    
                  JPanel panel = new JPanel();
    That adds the button vars.

    Section 5

    Let's add the buttons now. Add right below that, add:

    Code:
                  panel.setLayout(new FlowLayout());
    
    
                  panel.add(button);
                  panel.add(button1);
                  panel.add(button2);
                  panel.add(button3);
    
    
    
           add(panel, BorderLayout.NORTH);
              button.addActionListener(this);
               button1.addActionListener(this);
                button2.addActionListener(this);
                 button3.addActionListener(this);
             }

    That adds the buttons on.

    Section 6

    Let's make the outcome of clicking the button. Add:

    Code:
             public void actionPerformed(ActionEvent e)
              {
    
               JOptionPane.showMessageDialog(frame, " was selected." );
    
    
              }
         }
    That tells you what button was selected.

    Now save and compile. Now run the program and see what the outcome is.

    Section 7

    You can view other things like this at:
    http://java.sun.com/developer/onlineTraining/awt/contents.html
    This will help you understand all the functions of java GUI.
     
< Renting out a pure | Games and videos! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site