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
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()
{
}
}
import javax.swing.*;
is importing swing, wich is GUI.
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:
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
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:
JPanel panel1 = new JPanel();
after the
this.setTitle("GUI");
so it looks like this:
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
JButton button1 = new JButton("Button");
so it looks like this.
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:
this.add(panel1);
now to add those buttons to the panel. add this after the line you just added:
panel1.add(button1);
that tells the panel to add your button.
now after that you can add this:
this.setVisible(true);
then your whole code should look like this:
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:
public class GUI extends JFrame
look like this
public class GUI extends JFrame implements action listener
then after your public void GUI()
after the
}
}
add this void,
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
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.URL.com");
or to make that button close the program, add this there
System.exit(0);