1 Attachment(s)
Creating a Simple GUI JFrame
I'm new this java program community, although i'm also new to java i'll share some of my knowledge to beginners. Just Copy & Pasting the code wouldn't do anyone any good. I'll start off simple and explain the commands. Lets get started. Whole source code at the end of the thread.
For this tutorial im using Eclipse
Lets start off by creating a new .java class if you haven't done so already. I've named mines gui.java. If your using a regular text editor make sure your java class looks like this code shown below:
If you have some basic knownledge of Java lets enter the public void code,
Code java:
public class gui {
public static void main(String args[]) {
}
}
After we've gotten this covered lets import the JFrame function. Above the code were going to type import javax.swing.*;. This import makes the java class understand what the JFrame codes are. Without this you will get many errors within you class. So now your code should look something similar as:
Code java:
import javax.swing.*;
public class gui {
public static void main(String args[]) {
}
}
Alright now that we've gotten all the boring stuff out of the way let's begin coding the real fun lines. JFrame or any other function will need a name, you can name it anything such as frame, bobby, anything you want it doesn't matter. I'm going to name mine tobi I just like the name.
JFrame tobi = new JFrame("youwillneverfigurethisout");
Now let me explain what i just wrote, the JFrame as you know is the Gui also the tobi variable we discussed earlier. The new eh...it explains itself there. ("youwillneverfigurethisout"); You can always type in anything you want here. dose not matter. make sure your code looks like:
Code java:
import javax.swing.*;
public class gui {
public static void main(String args[]) {
JFrame tobi = new JFrame("youwillneverfigurethisout");
}
}
I will not explain many of these functions because it dose it themselves.
Where going to start off by making the gui visible simple by typing
tobi.setVisible(true);
Next this comes in handy the title of the gui
tobi.setTitle("Xbatz Gui Example")
Set the size. . .
tobi.setSize(400, 200);
Making the gui close-able when the X is clicked
tobi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Well almost there! Make sure your class looks like this:
Code java:
import javax.swing.*;
public class gui {
public static void main(String args[]) {
JFrame tobi = new JFrame("youwillneverfigurethisout");
tobi.setVisible(true);
tobi.setTitle("Xbatz GUI Example");
tobi.setSize(400, 200);
tobi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
If you have no errors lets run it! :o
For users who are using a text editor, sorry i don't know how to run a class from that :\ i'm sure there is a tutorial on how within the site. For Eclipse user, NetBeans, etc. Go to the Run tab and select run. if you get no errors then your gui should look something like this:
NOTE:: Well thanks guys i know it isn't much but i'm a beginner and will learn more soon ;))
Re: Creating a Simple GUI JFrame
Remember to adhere to the Java Naming Conventions, especially with your class name. That would mean gui -> Gui [And that isn't a very good name either]
Some possible class names:
- FrameTest
- XbatzFrame
- FirstFrame
- FrameExample
Also, that code should be called on the EDT, because it is very gui-based. Finally, you should comment your code! This would make it look a bit more like:
Code Java:
import javax.swing.*;
import java.awt.FlowLayout;
/**
* This class demonstrates how to create a basic
* JFrame. For example purposes, this frame will
* have a label saying "Hello World!"
*
* @see javax.swing.JFrame
* @author Timothy Moore
*/
public class FrameExample extends JFrame
{
/**
* Creates the frame example and makes it visible
*
* @throws AssertionError if not called on the EDT
*/
protected FrameExample()
{
setTitle("Frame Example");
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Middle of the screen
prepareComponents(); // Throws the assertion error
setVisible(true); // This will paint the entire frame
}
/**
* Prepares the components for display.
*
* @throws AssertionError if not called on the EDT
*/
private void prepareComponents()
{
assert SwingUtilities.isEventDispatchThread();
setLayout(new FlowLayout(FlowLayout.CENTER));
add(new JLabel("Hello World!"));
}
/**
* @param args not used
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
FrameExample example = new FrameExample();
}
});
}
}
Hope you get the idea, I added the example because so few people put code on the Event Dispatch Thread these days. Note that I subclass JFrame, instead of create one using its constructor. This is a much more versatile technique.
Some links: