2013/05/08 学习笔记

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值