- Joined
- Mar 22, 2008
- Messages
- 139
- Reaction score
- 5
I've noticed alot of people don't know much about Java GUI. There are programs that build GUI code for you, but who needs them.
I will be using JCreator to create my GUI.
To Start off:
- Create a new blank workspace called GUIGoodness.
- Create a new EMPTY project, and Name it GUIGoodness.
- Add a new Java Class File to that project and name it GUI.
1. Setting your code up:
You should just have something similar to this in your file -
You need to add these imports to the top -
Now we need to change the line -
to
This allows us to use JFrame and ActionListener.
2. Adding JFrame and a Container:
Add these variables into your class -
Now we need to make the Container, which will basically create a window with no buttons inside it.
Add this inside public GUI{ -
Now we need to set some attributes for the window.
Add this under what you just added -
You can change up the Size if you want.
3. Main Method:
Add this after the closing brace of public GUI -
And this -
This will be used later when we give a button an action.
Your program should look like this -
4. Creating an Object(JButton, JLabel):
To create a Button, TextField or Label, you first need to declare a variable, like this -
Now you need to create the button, like this -
Place that code in public GUI.
Now Run your project. It should look like this if you did not change anything.
Now lets add a JLabel, Do the same you did for the button, but instead of JButton, use JLabel.
Here's my variable -
Now this is how you create a JLabel, it's slightly different than a JButton.
After you add that, your program should look like this.
We're done with making objects now. Let's go into giving the Button an action.
5. Giving an Action:
Giving an action is actually quite easy, It just requires one if statement.
In this method, you need to add an if statement under -
This is how you would do that...
This checks if the JButton btn was clicked, if so, it changes the text in our JLabel lbl to "YAY YOU CLICKED IT".
In the end your entire program should look like this -
Well, thats the basics on how to make a GUI and give it funcionality.
If you experience any problems compiling, just post the error you get and I'll tell you what you did wrong, and how to fix it.
I will be using JCreator to create my GUI.
To Start off:
- Create a new blank workspace called GUIGoodness.
- Create a new EMPTY project, and Name it GUIGoodness.
- Add a new Java Class File to that project and name it GUI.
1. Setting your code up:
You should just have something similar to this in your file -
Code:
/**
* @(#)GUI.java
*
*
* @author
* @version 1.00 2009/4/15
*/
public class GUI {
public GUI() {
}
}
You need to add these imports to the top -
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Now we need to change the line -
Code:
public class GUI {
Code:
public class GUI extends JFrame implements ActionListener {
2. Adding JFrame and a Container:
Add these variables into your class -
Code:
private JFrame displayWindow;
private Container cp;
Now we need to make the Container, which will basically create a window with no buttons inside it.
Add this inside public GUI{ -
Code:
cp = getContentPane();
cp.setLayout(null);
cp.setBackground(Color.white);
Now we need to set some attributes for the window.
Add this under what you just added -
Code:
setTitle("GUIGoodness");
setLocation(150,250);
setSize(200,200);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
You can change up the Size if you want.
3. Main Method:
Add this after the closing brace of public GUI -
Code:
public static void main(String args[])
{
GUI frame = new GUI();
frame.setVisible(true);
}
And this -
Code:
public void actionPerformed(ActionEvent e)
{
//JButton actions
if(e.getSource() instanceof JButton)
{
JButton clicked = (JButton)e.getSource();
}
}
Your program should look like this -
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener {
private JFrame displayWindow;
private Container cp;
public GUI() {
cp = getContentPane();
cp.setLayout(null);
cp.setBackground(Color.white);
//main gui
setTitle("GUIGoodness");
setLocation(150,250);
setSize(200,200);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String args[])
{
GUI frame = new GUI();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//JButton actions
if(e.getSource() instanceof JButton)
{
JButton clicked = (JButton)e.getSource();
}
}
}
4. Creating an Object(JButton, JLabel):
To create a Button, TextField or Label, you first need to declare a variable, like this -
Code:
private JButton btn;
Now you need to create the button, like this -
Code:
btn = new JButton("Click Me!"); //creates the object
btn.setBounds(0,0,120,50); //sets the size and position (225,290 is the position, and 120,50 is the size)
btn.addActionListener(this);
cp.add(btn); //this shows the button on the actual window.
Place that code in public GUI.
Now Run your project. It should look like this if you did not change anything.
![Vintage - [Java] How to use GUI - RaGEZONE Forums Vintage - [Java] How to use GUI - RaGEZONE Forums](http://i29.photobucket.com/albums/c287/Tanner9636/window.jpg)
Now lets add a JLabel, Do the same you did for the button, but instead of JButton, use JLabel.
Here's my variable -
Code:
private JLabel lbl;
Code:
lbl = new JLabel("CLICK THE BUTTON!!");
lbl.setBounds(0,60,120,20);
cp.add(lbl);
![Vintage - [Java] How to use GUI - RaGEZONE Forums Vintage - [Java] How to use GUI - RaGEZONE Forums](http://i29.photobucket.com/albums/c287/Tanner9636/window2.jpg)
We're done with making objects now. Let's go into giving the Button an action.
5. Giving an Action:
Giving an action is actually quite easy, It just requires one if statement.
![Vintage - [Java] How to use GUI - RaGEZONE Forums Vintage - [Java] How to use GUI - RaGEZONE Forums](http://i29.photobucket.com/albums/c287/Tanner9636/action.jpg)
In this method, you need to add an if statement under -
Code:
JButton clicked = (JButton)e.getSource();
This is how you would do that...
Code:
if(clicked == btn) //btn is the name of my button, NOT the text inside it.
{
lbl.setText("YAY YOU CLICKED IT"); //changes the text in our label.
}
This checks if the JButton btn was clicked, if so, it changes the text in our JLabel lbl to "YAY YOU CLICKED IT".
In the end your entire program should look like this -
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener {
private JFrame displayWindow;
private Container cp;
private JButton btn;
private JLabel lbl;
public GUI() {
cp = getContentPane();
cp.setLayout(null);
cp.setBackground(Color.white);
//main gui
setTitle("GUIGoodness");
setLocation(150,250);
setSize(200,200);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
btn = new JButton("Click Me!"); //creates the object
btn.setBounds(0,0,120,50); //sets the size and position (225,290 is the position, and 120,50 is the size)
btn.addActionListener(this);
cp.add(btn); //this shows the button on the actual window.
lbl = new JLabel("CLICK THE BUTTON!!");
lbl.setBounds(0,60,120,20);
cp.add(lbl);
}
public static void main(String args[])
{
GUI frame = new GUI();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//JButton actions
if(e.getSource() instanceof JButton)
{
JButton clicked = (JButton)e.getSource();
if(clicked == btn)
{
lbl.setText("YAY YOU CLICKED IT");
}
}
}
}
If you experience any problems compiling, just post the error you get and I'll tell you what you did wrong, and how to fix it.