This paper presents his own study notes. Welcome reprint, but please specify 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. JFrame can create Windows apps in whatever operating system you work with.
See for example one of the simplest jframe examples:
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 in it. Setdefaultcloseoperation () tells JFrame what to do when you run the close operation with Kee.
The Exit_on_close constant tells it to exit the program.
And assuming that there is no such call, the default behavior is to do nothing. So the app will not shut down. SetSize () Sets the size of the window in pixels. JFrame is not displayed by default. So you need to set setvisible (true). Talent is displayed on the screen.
Effects such as those seen in:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvamvzc29umjaxmjewmja=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/center ">
Following. Add a label to the window, the main code such as the following:
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 operation after 1 seconds, which sets the text to be displayed again JLabel.
Effects such as the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvamvzc29umjaxmjewmja=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/center ">
The above is the UI event update screen that runs directly on the main () thread, but this is not a good idea. Swing has its own dedicated thread to receive UI events and update the screen. Assume that you are working on the screen from another thread. Then there may be conflicts and deadlocks.
So other threads contain the main () thread. The task to run should be submitted through the Swing event distribution thread.
Can be achieved by handing over tasks to Swingutilities.invokelater ().
Assuming that we apply this approach to a polygon sample, 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");});
such as the above code. Do not operate the JLabel directly. Instead, a runnable is submitted, so there is no conflict.
Now that swing has its own dedicated thread to receive UI events. Then, we should not operate jframe directly in the main () thread, so. We will also unify the operation of JFrame with swing dedicated thread, then 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 (). There is also the direct inheritance of JFrame, the above complete code with the first method. The second method is complete code such as the following:
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"),}});}}
One thing to be aware of here. The call to sleep () cannot be inside the constructor, assuming that the initial text of the JLabel will never appear in the interior. This is mainly because the constructor does not end before the sleep () call finishes and the new label is inserted.
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Java Programming Interface (1)------Swing Foundation