SMF For Free Support Forum
Signup For Free Forum
October 08, 2008, 01:01:31 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to SMF For Free. The best free SMF Host
 
   Home   Help Search Arcade Gallery Login Register  

Pages: [1]
  Print  
Author Topic: Making a simple GUI. (My 3 hour tut)  (Read 1895 times)
0 Members and 1 Guest are viewing this topic.
† Java †
SMF For Free Full Member
*
Offline Offline

Posts: 183



View Profile WWW
« on: June 19, 2007, 07:03:54 pm »

this is my first java tut i put alot of work into this tell me what you think.


SIMPLE GUI

here is what we will be making today.


I will show you how to add simple buttons and simple actions for the buttons.

what you will need first is eclipse, its what i use but i guess anythings fine you can get eclipse from here - http://www.eclipse.org/downloads/  download the SDK.

when you open eclipse you will see the following -


to run a program go to run > run as > java application

now, go to file, new, project.

when the window comes up click java project in the tree below the first text field.

then in the column to the right you will see your project.now go to file, new, class.

when it comes up set the following to the following:

package: JavaAIO

Name:GUI

and select public static void main string.

now in the middle of eclipse its like notepad. paste this in there

Code:

package JavaAIO

import javax.swing.*;
import java.awt.FlowLayout;
import java.io.*;

public class GUI extends JFrame
{
public static void main(String[] args) throws IOException
{
new GUI();
}

public GUI()
{
        }

}

Code:
import javax.swing.*;
is importing swing, wich is GUI.

Code:
import java.awt.FlowLayout;

is importing the layout type, for this tut we will be using flow.

now we will add a window to the GUI.

make public GUI() look like this:
Code:
public GUI()
{
this.setSize(330,200);
this.setResizable(false);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
this.setTitle("GUI");

}

now the setsize is self explanatory, sets the size of the window.
if you set it resizable as true you can maximize and minimize your program.
and this.setTile sets the title of your program.

dont try to run it now because we need to add more code, but if you wish to run it, add a
Code:
this.setVisible(true);
to the bottom of your code.

now to add a button.

to add a button you ned a panel to add it on, to create a panel add this:
Code:
JPanel panel1 = new JPanel();
after the
Code:
this.setTitle("GUI");

so it looks like this:
Code:
public GUI()
{
this.setSize(330,200);
this.setResizable(false);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
this.setTitle("GUI");

JPanel panel1 = new JPanel();
}
then to add a button add this right after the JPanel line
Code:
JButton button1 = new JButton("Button");

so it looks like this.
Code:
public GUI()
{
this.setSize(330,200);
this.setResizable(false);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
this.setTitle("GUI");

JPanel panel1 = new JPanel();
JButton button1 = new JButton("Button");
what you have done is added a button under the name "button1". the text in the (" "); at the end of the button line is what the text will be on the button, so mine is simply 'button'.
now to add the panel,
press enter after the button1 line and put this:
Code:
this.add(panel1);
now to add those buttons to the panel. add this after the line you just added:
Code:
panel1.add(button1);
that tells the panel to add your button.
now after that you can add this:
Code:
this.setVisible(true);

then your whole code should look like this:
Code:

package JavaAIO

import javax.swing.*;
import java.awt.FlowLayout;
import java.io.*;

public class GUI extends JFrame
{
public static void main(String[] args) throws IOException
{
new GUI();
}

public GUI()
{
this.setSize(330,200);
this.setResizable(false);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
this.setTitle("GUI");

JPanel panel1 = new JPanel();
JButton button1 = new JButton("Button");
this.add(panel1);
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
panel1.add(button1);
this.setVisible(true);


}

}

congratulations, you have made your first gui!
Adding action to your button

this one wont be as long, we will be continuing from the other code we just used.

make your:
Code:
public class GUI extends JFrame
look like this
Code:
public class GUI extends JFrame implements action listener
then after your public void GUI()
after the
Code:
    }

}

add this void,
Code:
public void actionPerformed(ActionEvent evt) {
        String cmd = evt.getActionCommand();
        if(cmd.equals("BUTTON NAME")) {
        try {
        //what the button does (will be explained)
        } catch (Exception e) {}
        }       

}

were it says button name add your buttons name which is the txt on the button.

now to open a webpage with that button add this were the //'s are. after you delete them
Code:
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.URL.com");
or to make that button close the program, add this there
Code:
System.exit(0);



« Last Edit: June 19, 2007, 08:43:36 pm by † Frell † » Logged

Crasy
Global Moderator
*
Offline Offline

Posts: 3309


I Approve of this Message


View Profile WWW
« Reply #1 on: June 19, 2007, 07:18:41 pm »

Is that Javascript? Or Java?
Logged

† Java †
SMF For Free Full Member
*
Offline Offline

Posts: 183



View Profile WWW
« Reply #2 on: June 19, 2007, 08:07:59 pm »

well, i think there the same, im not sure.
Logged

Crasy
Global Moderator
*
Offline Offline

Posts: 3309


I Approve of this Message


View Profile WWW
« Reply #3 on: June 19, 2007, 08:19:20 pm »

well, i think there the same, im not sure.

Javascript is scripting like AgentMoose is using. Java is a programming language.
They both just have a similar syntax...but Javascript is a scripting language. Totally unrelated besides the name.
« Last Edit: June 19, 2007, 08:22:04 pm by crasyandconfused » Logged

† Java †
SMF For Free Full Member
*
Offline Offline

Posts: 183



View Profile WWW
« Reply #4 on: June 19, 2007, 08:44:06 pm »

ohh ok. im glad my 3 hour tut didnt get deleted.. worked hard.
Logged

Mod Quack
SMF For Free Member
*
Offline Offline

Posts: 74



View Profile
« Reply #5 on: June 22, 2007, 11:18:49 pm »

good job frell
Logged

99Bullets
SMF For Free Full Member
*
Offline Offline

Posts: 180


Join Massive Gaming Site Forums!!!


View Profile WWW
« Reply #6 on: July 06, 2007, 04:08:21 pm »

can u explain this part better, i dont get this part

to run a program go to run > run as > java application

now, go to file, new, project.

when the window comes up click java project in the tree below the first text field.

then in the column to the right you will see your project.now go to file, new, class.
Logged

Thanks Hurrican47 For Making This

you want to apply for staff, please pm me!!!
mr_css
SMF For Free Sr. Member
*
Offline Offline

Posts: 259



View Profile
« Reply #7 on: October 17, 2007, 08:35:32 am »

Nicely done but I dont really understand. lol
Logged


            WOLFSCAPE FORUMS


Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.6 | SMF © 2006-2008, Simple Machines LLC
ServerBeach Coupon
Page created in 0.424 seconds with 18 queries.