Java Interface Programming (1), java Interface Programming
This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020
I recently want to learn java interface programming, which is recorded here.
Most Swing applications are built inside the basic JFrame, and JFrame can create Windows applications in any operating system you are working on.
See the following simple JFrame example:
JFrame frame = new JFrame("Hello Swing");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 100);frame.setVisible(true);
In this way, a window with a title is created, where setdefaclocloseoperation () tells the JFrame what to do when it is executed with a record. The EXIT_ON_CLOSE constant tells it to exit the program. Without this call, the default action is to do nothing, so the application will not be closed. SetSize () sets the size of the window in pixels. JFrame is not displayed by default. Therefore, you must set setVisible (true) to display it on the screen. Shows the effect:
Next, 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");
Here, TimeUnit. SECONDS. sleep (1) indicates that the following operation is performed after one second, that is, the text displayed in JLabel is reset. The effect is as follows:
The above is to directly execute the UI event update screen in the Main () thread, but this is not a good idea. Swing has its own dedicated thread to receive UI events and update the screen, if you start operations on the screen from other threads, conflicts and deadlocks may occur. Therefore, other threads include the main () thread, which should submit the task to be executed through the Swing event distribution thread. You can submit the task to SwingUtilities. invokeLater.
If we apply this method to the following 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");}});
For example, you do not need to directly operate JLabel. Instead, submit a runnable, so there will be no conflict.
Since Swing has its own dedicated thread to receive UI events, we should not directly operate JFrame in the Main () thread. Therefore, the JFrame operation is also implemented by using a dedicated Swing thread. The above example is changed:
public class test {private static JLabel label;private static JFrame frame;private static void InitJFrame(){frame = new JFrame("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, there are two ways to create a JFrame: one is new JFrame (), and the other is to directly inherit JFrame. The complete code above uses the first method, the complete code for the second method is as follows:
public class SwingTest extends JFrame{private static JLabel label;private static SwingTest st;public SwingTest() {super("Hello Swing");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(300, 100);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 SwingTest();}});TimeUnit.SECONDS.sleep(1);SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {label.setText("Hi,this is a different");}});}}
Note that the call to sleep () cannot be within the constructor. If it is placed inside, the initial JLabel text will never appear. This is mainly because the constructor will not end until sleep () is called and the new tag is inserted.