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) {
}
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
public static void main(String[] args) {
}
Note: javax.swing.SwingUtilities.invokeLater() is a coverfor
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)
2)
private static final long serialVersionUID =1L;
3)
@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