2013/05/08 学习笔记

来源:互联网 发布:php后台密码破解工具 编辑:程序博客网 时间:2024/04/29 20:56

1.Cannot refer to a non-final variable inside an innerclass defined in a different method. 

 

2.Inside the inner class, "this" refers to the inner class.To refer to the "this" of the outer class, use"OuterClassName.this". But you can reference outer class's membersdirectly without this clumpysyntax.  

 

3.Anadapter class called WindowAdapter implements the WindowListenerinterface and provides default implementations to all the 7abstract methods. You can then derive a subclass from WindowAdapterand override only methods of interest and leave the rest to theirdefault implementation.

 

4.the JComponents shall not be added onto the top-levelcontainer (e.g., JFrame, JApplet) directly because they arelightweight components. The JComponents must be added onto theso-called content-pane of the top-level container. Content-pane isin fact a java.awt.Container that can be used to group and layoutcomponents.

 

Notes: If a component is added directly into a JFrame, itis added into the content-pane of JFrame instead 

5.

Running the GUIConstruction Codes on the Event-Dispatching Thread

Inthe previous examples, we invoke the constructor directly in theentry main() method to setup the GUI components. Forexample,

 

// The entry main method

public static void main(String[] args) {

  // Invoke the constructor (byallocating an instance) to setup the GUI

   newSwingCounter();

}

Theconstructor will be executed in the so-called "Main-Program"thread. This may cause multi-threading issues (such as unresponsiveuser-interface and deadlock).

 

Itis recommended to execute the GUI setup codes in the so-called"Event-Dispatching" thread, instead of "Main-Program" thread, forthread-safe opearations. Event-dispatching thread, which processesevents, should be used when the codes updates the GUI.

 

Torun the construstor on the event-dispatching thread,invoke static method SwingUtilities.invokeLater() to asynchronously queue theconstructor on the event-dispatching thread. The codes will be runafter all pending events have been processed. Forexample,

 

public static void main(String[] args) {

  // Run the GUI codes in theEvent-dispatching thread for thread-safety

  SwingUtilities.invokeLater(new Runnable() {

     @Override

     public void run() {

        new SwingCounter(); // Let the constructor do thejob

     }

  });

}

Note: javax.swing.SwingUtilities.invokeLater() is a coverfor java.awt.EventQueue.invokeLater() ( which is used in the NetBeans'Visual GUI Builder).

 

Attimes, for example in game programming, the constructor or themain() may contains non-GUI codes. Hence, it is a common practiceto create a dedicated method called initComponents() (used inNetBeans visual GUI builder) or createAndShowGUI() (used in Swingtutorial) to handle all the GUI codes (and another method calldinitGame() to handle initialization of the game's objects). ThisGUI init method shall be run in the event-dispatchingthread.

 

 

 

6.Warning Message "Theserialization class does not declare a static finalserialVersionUID field of type long" (Advanced)

Thiswarning message is triggered because java.awt.Frame (via itssuperclass java.awt.Component) implements the java.io.Serializableinterface. This interface enables the object to be written out toan output stream serially (via method writeObject()); and read backinto the program (via method readObject()). The serializationruntime uses a number (called serialVersionUID) to ensure that theobject read into the program is compatible with the classdefinition, and not belonging to another version.

 

Youhave these options:

 

1)   Simplyignore this warning message. If a serializable class does notexplicitly declare a serialVersionUID, then the serializationruntime will calculate a default serialVersionUID value for thatclass based on various aspects of the class.

2)   Add aserialVersionUID (Recommended), e.g.

private static final long serialVersionUID =1L; // verion 1

3)   Suppress this particular warning via annotation@SuppressWarmomgs (in package java.lang) (JDK 1.5):

@SuppressWarnings("serial")

public class MyFrame extends JFrame { ...... }

 

以上学习笔记摘自:http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html

这里顺便介绍个很好的java学习网站:

http://docs.oracle.com/javase/tutorial/uiswing/index.html





0 0