Java Swing simplest example (1) an empty JFrame

Source: Internet
Author: User

I'm going to write a tutorial on a series of Java graphical interfaces. Each program tries to write only the most streamlined code needed to keep it running, so everyone can understand it.

Using the Java graphical interface requires only jdk,eclipse. If the layout abstraction is not powerful enough, it is recommended to install Jigloo First and then copy the code. (Jigloo automatically generated code redundancy is too large, put the position is not reasonable, just to see the effect, then still need to ctrl+c,ctrl+v)

To put everything in place, we need to have a jframe first. So let's talk about how to create an empty jframe.

Here is a simple example, first we need to extends JFrame, so the current class itself is a JFrame. Then create an object of the current class in the function, which is to create a jframe. The code reads from top to bottom in order.

Package Basiccompoment;import Javax.swing.jframe;import Javax.swing.windowconstants;public class EmptyJFrame extends Jframe{public static void Main (string[] args) {//now creates an object, but nothing is shown emptyjframe f = new Emptyjframe ();// With this sentence you can display a frame with only a close, minimized, maximized button, f.setvisible (TRUE);//Add this sentence to show a frame with a specified size in the upper-left corner f.setsize (300,400);// With this sentence you can put the frame in the middle of the f.setlocationrelativeto (null);//If you don't have this, the program is still in the execution state when you click Close Frame. Plus this sentence is really the release of Resources F.setdefaultcloseoperation (Windowconstants.dispose_on_close);}}


The creation of JFrame is usually placed in the Swingutilities.invokelater, as follows

Package Basiccompoment;import Javax.swing.jframe;import Javax.swing.swingutilities;import Javax.swing.windowconstants;public class EmptyJFrame2 extends Jframe{emptyjframe2 () {Initgui ();} private void Initgui () {setvisible (true); SetSize (300,400); Setlocationrelativeto (null); Setdefaultcloseoperation ( Windowconstants.dispose_on_close);}  public static void Main (string[] args) {Swingutilities.invokelater (new Runnable () {public void run () {EmptyJFrame2 F = new EmptyJFrame2 ();}});}}


Java Swing simplest example (1) an empty JFrame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.