How to create a simple Java JFrame Window

Java JFrame Window is a top level component that defines the Lookandfeel of your application.Its from this window that we can add other elements like JButton , JTextField, JLabel etc.

This tutorial introduces you to JFrame class and  some of the methods that it has.

Code to create simple Java JFrame Window

Run below code

Output

Simple Java JFrame Window

Adding swing components to Java JFrame Window

Its easy to add swing components to JFrame window using add() method.Below code adds JLabel,JTextField,JPasswordField and JButtons to JFrame window.

 

 Output

JFrame window with JButton and JTextFields

Setting Java JFrame Window DefaultCloseOperation

There are several options that can be take by JFrame.setDefaultCloseOperationMethod.These are

  • JFrame.EXIT_ON_CLOSE-This closes JFrame window completely.
  • JFrame.DISPOSE_ON_CLOSE-Exits windows and destroy its object releasing all resources back.
  • JFrame.DO_NOTHING_ON_CLOSE-when close button is clicked,nothing happens to the JFrame window.
  • JFrame.HIDE_ON_CLOSE-does not destroy JFrame object but hides it.

Example Code

 

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com