Skip to main content

Java Swing GUI (Tutorial)

                        Java Swing GUI Tutorial : #JFrame
 Hello, welcome to my first java swing tutorial in JFrame. Let's start from creating a new project and throw the java project.




Select java project and then I put the project name as "Swing"


Then create a new class and I put the class name as "Frame"



And also I'm going to select the "public static void main(String[] args)"


To create any swing component will have to import our swing library or type like this.


Here, I'm going to make my objects like this.

"JFrame frame = new JFrame();"

and I'll pass the title of my frame in constructive JFrame class and title will be JFrame demo,

"JFrame frame = new JFrame( "JFrame demo");" 

 

Here also I have to call few methods. First method will be,

"frame.setDefaultCloseOperation();"
"setDefaultCloseOperation()" is a method which defines how our application ends. So it defines what happens when we click on exit button.

 

And  then I'm going to code like this.
" frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);"

So some of your friends which have some experiences are probably wondering why I'm calling "DISPOSE_ON_CLOSE" instead of exiter close. Calling exiter close will end our application violently.

Next method I'm going to call is,
"frame.setSize();"
It's of course self-explanatory but it defines size for free in pixels and I'll set the size like this.
"frame.setSize(320,240);"

Last time that I'm going to call is, "frame.setVisible()" and of course it will make our frame visible. And as an argument I'll pass boolean "true".

"frame.setVisible(true);"


And then save it. Let's run the program and here it is.


Our JFrame with title "JFrame demo" and with its size of 320 with 240 and of course it's respond. In next tutorial I'm going to show you how to use JPanel.




Comments

Post a Comment