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


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.



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

Kenyan Developer and Blogger with Interests in Java,Javascript,Node.js, Wordpress, Android, PHP and Making Money Online. Solving world problems through programming is my passion. Hire me: Email :