Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Display a JFrame instance

The JFrame class has two methods that you call to display a JFrame instance, pack and setVisible. public void pack () public void setVisible (boolean visible) The pack method resizes the JFrame to fit the sizes and layout of its child components. You can invoke setVisible (true) to display the JFrame. import javax.swing.JFrame; import javax.swing.JLabel; public class JFramePack {   public static void main(String[] args) {     JFrame.setDefaultLookAndFeelDecorated(true);     JFrame frame = new JFrame();     frame.setTitle("My First Swing Application");     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     JLabel label = new JLabel("Welcome");     frame.add(label);     frame.pack();     frame.setVisible(true);   } }