Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Swing (Java)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Hello World=== This example Swing application creates a single window with "Hello, world!" inside: <syntaxhighlight lang="java"> // Hello.java (Java SE 8) import javax.swing.*; public class Hello extends JFrame { public Hello() { super("Hello World"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); add(new JLabel("Hello, world!")); pack(); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(Hello::new); } } </syntaxhighlight> The first '''<code>import</code>''' includes all the public classes and interfaces from the '''{{Javadoc|module=java.desktop|package=javax.swing|monotype=y}}''' package. The <code>'''Hello'''</code> class <code>'''extends'''</code> the '''{{Javadoc|module=java.desktop|package=javax.swing|class=JFrame|text=JFrame|monotype=y}}''' class; the <code>JFrame</code> class implements a [[window (computing)|window]] with a [[title bar]] and a close [[Graphical control element|control]]. The <code>'''Hello()'''</code> [[constructor (object-oriented programming)|constructor]] initializes the frame by first calling the superclass constructor, passing the parameter <code>"Hello World"</code>, which is used as the window's title. It then calls the '''{{Javadoc|module=java.desktop|package=javax.swing|class=JFrame|member=setDefaultCloseOperation(int)|text=setDefaultCloseOperation(int)|monotype=y}}''' method inherited from <code>JFrame</code> to set the default operation when the close control on the title bar is selected to '''{{Javadoc|module=java.desktop|package=javax.swing|class=WindowConstants|member=EXIT_ON_CLOSE|text=WindowConstants.EXIT_ON_CLOSE|monotype=y}}'''{{snd}} this causes the <code>JFrame</code> to be disposed of when the frame is closed (as opposed to merely hidden), which allows the Java virtual machine to exit and the program to terminate. Next, a '''{{Javadoc|module=java.desktop|package=javax.swing|class=JLabel|text=JLabel|monotype=y}}''' is created for the string '''"Hello, world!"''' and the '''{{Javadoc|module=java.desktop|package=java.awt|class=Container|member=add(java.awt.Component)|text=add(Component)|monotype=y}}''' method inherited from the {{Javadoc|module=java.desktop|package=java.awt|class=Container|text=Container|monotype=y}} superclass is called to add the label to the frame. The '''{{Javadoc|module=java.desktop|package=java.awt|class=Window|text=pack()|member=pack()|monotype=y}}''' method inherited from the {{Javadoc|module=java.desktop|package=java.awt|class=Window|text=Window|monotype=y}} superclass is called to size the window and lay out its contents. The '''{{Javadoc|module=java.desktop|package=java.awt|class=Component|member=setVisible(boolean)|text=setVisible(boolean)|monotype=y}}''' method inherited from the {{Javadoc|module=java.desktop|package=java.awt|class=Component|text=Component|monotype=y}} superclass is called with the Boolean parameter <code>'''true'''</code>, which causes the frame to be displayed. The <code>'''main()'''</code> method is called by the Java virtual machine when the program starts. It [[Object (computer science)|instantiates]] a new '''<code>Hello</code>''' frame. The code uses the '''{{Javadoc|module=java.desktop|package=javax.swing|class=SwingUtilities|member=invokeLater(java.lang.Runnable)|text=invokeLater(Runnable)|monotype=y}}''' method to invoke the constructor from the AWT [[event dispatching thread]] in order to ensure the code is executed in a [[thread-safe]] manner. Once the frame is displayed, exiting the <code>main</code> method does not cause the program to terminate because the event dispatching thread remains active until all of the Swing top-level windows have been disposed.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)