Hello, men, I'm gonna begin the java game development series. First of all, I will create posts on basic topics such as double buffering, transformation and so on. After completing it we'll be able to move forward to make a game.
can navigate to next and previous posts thru the button at the bottom of the web page. Or you can see the contents of this series here.
Clearing the basics will take a protracted time thus stick with me. I'll keep updating my blog with regular game programming posts.
The first necessity to a game is a GUI window thus this post is on the way to build GUI Window in java by dealing with the swing library.
Making a GUI window with swing's JFrame
Creating a GUI window is the very first thing that you just know. If you don't know just continue otherwise go to the next post.
To make a GUI in java we are allowed to leverage the java's SWING library.
What is Swing in java?
Swing is one in each of the libraries that comes with java JDK and could be a Graphical program (GUI) toolkit that features a chic set of widgets.
It is a portion of Java Foundation Classes(JFC), that is an API for Java programs that offer a GUI. Swing includes packages that permit you to create a complicated set of GUI elements for your Java applications and it's platform-independent.
The swing library is made on the top of AWT (Abstract window toolkit), an older, platform-dependent GUI toolkit
You are able to make use of Java GUI components like button, textbox, etc. from the swing library and don't have to make every component from scratch.
Most of the elements in swing begin with 'J' like JText, JTextArea, JPanel, JButton etc.
How to construct a GUI window with swing
In swing, there's a class referred to as JFrame and is employed to make a GUI window. Our main class will extend JFrame or it will contain JFrame objects as a member.
// don't forget to import JFrame
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
}
To make the window visible invoke the setVisible() within the constructor and pass in true as a parameter
// do not forget to import JFrame
import javax.swing.JFrame;
private Main() {
setVisible(true);
}
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
}
Now run the program, you will see a tiny window at the top left corner of the screen.
Still, It has issues that the program doesn't stop itself after closing the window (if we tend to use ide, we can stop it easily) to solve that problem invoke the setDefaultCloseOperation(EXIT_ON_CLOSE) method.
import javax.swing.JFrame;
private Main() {
// call it to properly to stop the execution
//after closing the window.
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("java swing");
// to set the size call setSize()
// and to show gui invoke setVisible(true)
setSize(800, 600);
setVisible(true);
}
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
}
Run it and you'll see a frame of size 800 by 600 at a similar position as before with the title. if you close up the window the program won't be in progress.
No comments:
Post a Comment