- Swing Programming Examples
- Example - Home
- Example - Environment Setup
- Example - Borders
- Example - Buttons
- Example - CheckBoxes
- Example - Combo Boxes
- Example - Color Choosers
- Example - Dialogs
- Example - Editor Panes
- Example - File Choosers
- Example - Formatted TextFields
- Example - Frames
- Example - Lists
- Example - Layouts
- Example - Menus
- Example - Password Fields
- Example - Progress Bars
- Example - Scroll Panes
- Example - Sliders
- Example - Spinners
- Example - Tables
- Example - Toolbars
- Example - Tree
- Swing Useful Resources
- Swing - Quick Guide
- Swing - Useful Resources
- Swing - Discussion
Swing Examples - Creating Standard Window
Following example showcase how to create a standard window in Swing based application.
We are using the following APIs.
JFrame − To create a standard frame or window.
JFrame.getContentPane() − To get the content area of the frame.
JFrame.setSize() − To set the size of the frame.
JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); − To set the action when Frame is closed.
Example
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.LayoutManager; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingTester { public static void main(String[] args) { createWindow(); } private static void createWindow() { JFrame frame = new JFrame("Swing Tester"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); createUI(frame); frame.setSize(560, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } private static void createUI(JFrame frame){ JPanel panel = new JPanel(); LayoutManager layout = new FlowLayout(); panel.setLayout(layout); panel.add(new JLabel("Hello World!")); frame.getContentPane().add(panel, BorderLayout.CENTER); } }
Output
swingexamples_frames.htm
Advertisements
To Continue Learning Please Login
Login with Google