วันศุกร์, ธันวาคม 09, 2548

Java : ว่าด้วยเรื่อง Inheritance, Superclass, Subclass

คำถามคาใจ:

ในคลาสลูก การเรียก Constructor super() ของคลาสพ่อแบบ explicit กับการไม่เรียก
ให้ผลต่างกันอย่างไร?

การใช้ this กับไม่ใช้ this นั้นมีผลต่อ inheritance อย่างไร คือพอถูก subclass ไปแล้ว this ที่ในคลาสพ่อก็ยังคง refer ถึงตัวคลาสพ่ออยู่ดีใช่ป่าว?

ทั้งหมดนี้น่าจะหาำคำตอบได้ใน Java Specification Book ไว้ว่างๆ ไปเช็คแล้วจะกลับมาตอบ

วันพฤหัสบดี, ธันวาคม 01, 2548

Java : Windows Font Look And Feel

สำหรับ application ที่เจาะจงว่าจะรันบนวินโด้ โดยเฉพาะนั้น เพื่อให้รูปแบบของฟอนต์สอดคล้องกับของวินโด้แต่ดั้งเดิม ควรเลือกใช้ Tahoma ขนาดตั้งแต่ 12 - 15

Java : เกี่ยวกับเรื่องของ ชิ้นส่วนภาชนะ Container Components

ใน Swing นั้น, เรามี Top-level Container อยู่คือ JFrame, JDialog และ JApplet.
สิ่งที่ควรสำเหนียก เวลาใช้พวก class เหล่านี้คือ
  • ทุก component ที่จะปรากฏบนจอ จะต้องอยู่ใน containment hierarchy---เป็นต้นไม้ของ component ซึ่งมี top-level container เป็นราก
  • ทุก GUI component ถูกยัดใส่ลงภาชนะ ได้แค่ครั้งเดียว. ถ้าเราเอา component ไหนยัดลงไปใน container หนึ่งแล้ว และจับมันยัดลงไปใน container อื่นอีก, ก็หมายความว่า มันต้องถูกเอาออกมาจาก container แรกก่อนจึงถูกนำไปยัดใส่ container ที่สองได้
  • ทุก top-level container มี content pane ซึ่งกล่าวอย่างคร่าวๆได้ว่า content pane นั้น บรรจุ visible components ใน GUI ของ top-level container ทั้งโดยตรงและโดยอ้อม.
  • เราสามารถเลือกที่จะใส่ menu bar ไปที่ top-level container หรือไม่ก็ได้. ซึ่ง menu bar นั้นจะถูกวางอยู่ภายใน top-level container, แต่อยู่นอก content pane. ในบาง look and feels, เช่น Mac OS look and feel, มีทางเลือกให้เราวางตำแหน่งของ menu bar ที่อื่นซึ่งเหมาะสมกว่าได้, เช่นที่ส่วนบนสุดของหน้าจอ เป็นต้น. ดังภาพข้างล่าง

ตัวอย่างของ containment hierarchy ลำดับชั้นของภาชนะ













Features ของ JComponent ที่น่านำไปใช้ในโปรแกรม
  1. Tool tips กำหนดข้อความโดย setToolTipText(), ช่วยบอกเวลา user เอาเมาส์มาวางไว้. ดูเพิ่มเติมได้ที่ How to Set the Look and Feel
  2. Support for layout ใน Component class แม้จะมี methods ซึ่งเอาไว้ดู layout hint เช่น getPreferredSize and getAlignmentX. ทางเลือกอื่นสำหรับ set layout hints, JComponent class เพิ่ม setter methods — setPreferredSize, setMinimumSize, setMaximumSize, setAlignmentX, and setAlignmentY. ขึ้นมา ดูเพิ่มที่ Laying Out Components Within a Container
  3. Key bindings ทำให้ components ตอบสนองเมื่อ user กดปุ่ม keyboard ที่กำหนดไว้. เช่นเวลาเคอร์เซอร์อยู่บนปุ่ม การกด space bar ก็เหมือนกับการกดคลิ๊กบนปุ่มนั้น. look and feel เซต bindings ของการกดและปล่อย ปุ่ม Space และส่งผลนั้นไปที่ปุ่ม. เพิ่มเติมเกี่ยวกับ key bindings ดูที่ How to Use Key Bindings (in the Creating a GUI with JFC/Swing trail).

การใช้ Text Components
Handling Events
(see
Writing Event Listeners (in the Creating a GUI with JFC/Swing trail) for details)
Method Purpose
void addMouseListener(MouseListener)
void removeMouseListener(MouseListener)
Add or remove a mouse listener (in the Creating a GUI with JFC/Swing trail) to or from the component. Mouse listeners are notified when the user uses the mouse to interact with the listened-to component.
void addMouseMotionListener(MouseMotionListener)
void removeMouseMotionListener(MouseMotionListener)
Add or remove a mouse motion listener (in the Creating a GUI with JFC/Swing trail) to or from the component. Mouse motion listeners are notified when the user moves the mouse within the listened-to component's bounds.
void addKeyListener(KeyListener)
void removeKeyListener(KeyListener)
Add or remove a key listener (in the Creating a GUI with JFC/Swing trail) to or from the component. Key listeners are notified when the user types at the keyboard and the listened-to component has the keyboard focus.
void addComponentListener(ComponentListener)
void removeComponentListener(ComponentListener)
Add or remove a component listener (in the Creating a GUI with JFC/Swing trail) to or from the component. Component listeners are notified when the listened-to component is hidden, shown, moved, or resized.
void setTransferHandler(TransferHandler)
TransferHandler getTransferHandler()
Set or remove the transferHandler property. The TransferHandler supports exchanging data via cut, copy, or paste to/from a clipboard as well a drag and drop. See Drag and Drop (in the Creating a GUI with JFC/Swing trail) for more details. Introduced in 1.4.
boolean contains(int, int)
boolean contains(Point)
Determine whether the specified point is within the component. The argument should be specified in terms of the component's coordinate system. The two int arguments specify x and y coordinates, respectively.
Component getComponentAt(int, int)
Component getComponentAt(Point)
Return the component that contains the specified x, y position. The top-most child component is returned in the case where components overlap. This is determined by finding the component closest to the index 0 that claims to contain the given point via Component.contains().

อ้างอิง
จาวา ติวโตเรี่ยล