2009年6月24日水曜日

Applet画面の見た目にかかわっている「LookAndFeel」

Applet画面の見た目にかかわっている「LookAndFeel」の説明:
一、LookAndFeelの種類:
1.CrossPlatformLookAndFeel(OSに関係なし):
これは、Java自分のデフォルトL&F:MetalLookAndFeel
(javax.swing.plaf.metal)

2.SystemLookAndFeel(OSに関係ある):
・Windows系:WindowsLookAndFeel
    (com.sun.java.swing.plaf.windows.WindowsLookAndFeel)
同じL&Fの名前があっても、実際Classic Windows、
Windows XP、Windows Vistaという実装があるから、見た目が
多少違う

・Solaris, Linux with GTK+ 2.2 or later:GTK
(com.sun.java.swing.plaf.gtk.GTKLookAndFeel)

・Other Solaris, Linux:Motif
(com.sun.java.swing.plaf.motif.MotifLookAndFeel)

・IBM UNIX:IBM
・HP UX:HP
・Macintosh:Macintosh

二、Appletの場合、LookAndFeelのロード優先順位:
1.ソースの中に直接LookAndFeelの定義がある場合、これは、Appletの
LookAndFeelとして使われる

2.ソースの中に定義されていない場合、自分が作った「swing.properties」
に定義されたLookAndFeelが使われる
例:
# Swing properties
swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

3.上記とも見つからない場合、Java自分のLookAndFellがデフォルトで使われる

三、自分なりの見た目を定義したい
LookAndFeelによって、定義されている見た目は、デフォルトのものですが、
自分がほしい色などがあったら、paintComponentメッソドをオーバーライドし、
親の該当メッソドを実行した後、自分のフォーマットを定義すればOK
例:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("This is my custom Panel!",10,20);
g.setColor(Color.RED);
g.fillRect(squareX,squareY,squareW,squareH);
g.setColor(Color.BLACK);
g.drawRect(squareX,squareY,squareW,squareH);
}

注:自分のフォーマットを定義する場合、このメッソドしかできない
また、裏側親クラスのpaint()を実行しないといけないから、ここにある
「super.paintComponent(g);」が不可欠

※SwingのGUIを作成することやSwing Componentとinteractionすることなど
の場合、処理スレッドは、event dispatching threadとして実行されることが
必要となる。
event dispatching threadとしての処理方法:
①Runnableの実装クラスを作成
②SwingUtilities.invokeLater(Runnable runObject)で上記クラスを実行

※要注意A:
Time-consuming tasks should not be run on the Event Dispatch Thread.
Otherwise the application becomes unresponsive.
Time-consuming tasks should be dealed with in worker thread.(SwingWorker)
※要注意B:
javax.swing.SwingWorkerは、JDK6から提供されるから、これまですでに存在している
SwingWorkerクラスとは、別物です。

0 件のコメント: