Java Tutorial
In java programming, we can also use JFrame to create a Graphical user interface (GUI) using JFrame class.
Need to create a instance for JFrame class to have GUI in java code.
JFrame frame = new JFrame("JFrame Title")
//Testing Frame using JFrame class in java code //importing java swing package for GUI import javax.swing.*; public class FrameTest { public static void main(String[] args){ // Creating a instance 'frame' for java JFrame class // and Frame title is 'JFrame Test Java Code'. JFrame frame = new JFrame("JFrame Test Java Code"); //Setting Frame size width 500 and height 200 frame.setSize(500,200); // Setting Frame visibility frame.setVisible(true); // Adding close operation as default in Frame. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }Output:
Once executing this java program, Empty Frame will be popped up as above and closes if we click close button in the frame title.
Below Java code is used to create JFrame GUI inheriting the JFrame class.
//Testing Frame using JFrame class in java code //importing java swing package for GUI import javax.swing.*; public class FrameTest extends JFrame { public FrameTest (){ // Setting Title setTitle("JFrame Test Java Code"); } public static void main(String[] args){ // Creating a instance 'frame' for java JFrame class // and Frame title is 'JFrame Test Java Code'. FrameTest frame = new FrameTest (); //Setting Frame size width 500 and height 200 frame.setSize(500,200); // Setting Frame visibility frame.setVisible(true); // Adding close operation as default in Frame. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page