Java interface Programming (1)------Swing Foundation

Source: Internet
Author: User

This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020

recently want to learn the Java interface programming, in this record.

Most swing applications are built within the base jframe, and JFrame can create Windows apps in any operating system you work with.

Look at one of the simplest jframe examples below:

JFrame frame = new JFrame ("Hello Swing"); Frame.setdefaultcloseoperation (jframe.exit_on_close); Frame.setsize (300, 100 ); Frame.setvisible (true);
This creates a window with a caption, where Setdefaultcloseoperation () tells JFrame what to do when the close operation is performed in mind. The Exit_on_close constant tells it to exit the program. Without this call, the default behavior is to do nothing, so the app will not close. SetSize () Sets the size of the window in pixels. JFrame is not displayed by default, so you need to set setvisible (true) to display on the screen. The effect is as follows:


Below, add a label to the window, the main code is as follows:

JLabel label = new JLabel ("A label"); Frame.add (label); try {TimeUnit.SECONDS.sleep (1);} catch (Interruptedexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();} Label.settext ("Hi,this is a different");
This TimeUnit.SECONDS.sleep (1) refers to the following action after 1 seconds, which is to reset the text displayed by JLabel. The effect is as follows:

The above is a UI event update screen that executes directly on the main () thread, but this is not a good idea, and swing has its own dedicated thread to receive UI events and update the screen, which can create conflicts and deadlocks if you start working on the screen from another thread. So other threads include the main () thread, which should submit the task to be performed through the Swing event distribution thread. This can be done by handing the task over to Swingutilities.invokelater ().

If we apply this approach to a polygon example, the code is as follows:

Final JLabel label = new JLabel ("A label"); Frame.add (label); TimeUnit.SECONDS.sleep (1); Swingutilities.invokelater (New Runnable () {@Overridepublic void run () {Label.settext ("Hi,this is a Different");});

As the above code does not directly manipulate JLabel, instead of submitting a runnable, there is no conflict.

Since swing has its own dedicated thread to receive UI events, then we should not operate jframe directly in the main () thread, so we will use JFrame's operations uniformly with swing dedicated threads, and the above example becomes:

public class Test {private static JLabel label;private static JFrame frame;private static void Initjframe () {frame = new JF Rame ("Hello Swing"); Frame.setdefaultcloseoperation (jframe.exit_on_close); Frame.setsize (300, 100); Frame.setvisible (true); label = new JLabel ("A label"); Frame.add (label);} /** * @param args * @throws interruptedexception  */public static void Main (string[] args) throws Interruptedexception {Swingutilities.invokelater (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubinitjframe ();}} ); TimeUnit.SECONDS.sleep (1); Swingutilities.invokelater (New Runnable () {@Overridepublic void run () {Label.settext ("Hi,this is a Different"),}});}}
Of course, because there are two ways to create JFrame, one is new JFrame (), the other is to directly inherit JFrame, the complete code above is the first method, the second method complete code is as follows:

public class Swingtest extends jframe{private static JLabel label;private static swingtest st;public swingtest () {Super ("H Ello Swing "); Setdefaultcloseoperation (jframe.exit_on_close); setSize (+); setvisible (true); label = new JLabel (" A label "); Add (label);} /** * @param args * @throws interruptedexception  */public static void Main (string[] args) throws Interruptedexception {Swingutilities.invokelater (new Runnable () {@Overridepublic void Run () {//TODO auto-generated Method Stubst = new Swingte St ();}}); TimeUnit.SECONDS.sleep (1); Swingutilities.invokelater (New Runnable () {@Overridepublic void run () {Label.settext ("Hi,this is a Different"),}});}}
It is important to note that the call to sleep () cannot be inside the constructor, and if placed internally, the initial text of the JLabel will never appear. This is mainly because the constructor does not end until the sleep () call is complete and the new label is inserted.


 


Java interface Programming (1)------Swing Foundation

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.