1 / 23

Component

Component. องค์ประกอบของ GUI. GUI (Graphical User Interface). คือการเขียนโปรแกรมที่ใช้รูปภาพกราฟิกในการโต้ตอบกับผู้ใช้ ซึ่งในจาวาเราสามารถเลือกงานได้หลายแบบ เช่น Java Swing Java AWT Java Applet Java Script. AWT (Abstract Windowing Toolkit).

Télécharger la présentation

Component

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Component องค์ประกอบของ GUI

  2. GUI (Graphical User Interface) • คือการเขียนโปรแกรมที่ใช้รูปภาพกราฟิกในการโต้ตอบกับผู้ใช้ ซึ่งในจาวาเราสามารถเลือกงานได้หลายแบบ เช่น • Java Swing • Java AWT • Java Applet • Java Script

  3. AWT (Abstract Windowing Toolkit) • คือเครื่องมือพื้นฐานในการเขียนโปรแกรมด้วย GUI ประกอบด้วย • AWT component • AWT Container จัดเป็น component อย่างหนึ่งที่สามารถนำเอา component อื่นๆ มาใส่ได้ • Layout Manager ใช้สำหรับจัดแสดง component ออกทางจอภาพ

  4. Swing Component • เนื่องจากการที่ AWT เองมีปัญหาต่าง ๆ เกิดขึ้นในการใช้งาน เช่น เรื่องของความจำ ทำให้จาวาพัฒนา class ขึ้นมาใหม่ที่มีความสามารถในด้าน GUI แต่ปัญหาน้อยกว่า ซึ่ง Swing ไม่ได้ใช้งานแทน AWT เพราะ AWT ยังมีอยู่ ในขณะเดียวกัน Swing ก็ยังคงต้อง extends บางคลาสจาก AWT มาใช้งาน

  5. ตารางเปรียบเทียบ component ของ AWT และ Swing

  6. import javax.swing.*; • class ExDialog { • public static void main(String []args) { • String firstNumber,seNumber; • int number1,number2, sum; • firstNumber = JOptionPane.showInputDialog("First num. is"); • seNumber = JOptionPane.showInputDialog ("And second num."); • number1 = Integer.parseInt(firstNumber); • number2 = Integer.parseInt(seNumber); • sum = number1+number2; • JOptionPane.showMessageDialog (null," Sum of two numbers is :" • +sum," Result", JOptionPane.PLAIN_MESSAGE); • System.exit(0); • } • }

  7. Swing Component

  8. ประเภทของ Component • Compound Components คือ คลาสที่ใช้ในการสร้าง GUI ที่มี Component อื่นเป็นส่วนประกอบเช่น เมนู

  9. Menu Menu Bar MenuItem

  10. ประเภทของ Component • Container Components คือ คลาสที่ใช้ในการสร้าง GUI ที่สามารถนำเอา Component อื่นมาเป็นส่วนประกอบได้มีสองประเภท คือ • Non-top-level Container • Top-level Container

  11. การสร้าง GUI • Component จะถูกสร้างขึ้นมาแล้ววางใน Container โดยที่การจัดวาง component นั้นจะอาศัย Layout Manager เป็นตัวช่วยในการจัดวาง • จะต้องมีคำสั่ง setSise(int, int) • จะต้องมีคำสั่ง setShow() หรือ setVisible(true)

  12. Top Level Container (Swing Component : JFrame)

  13. JFrame • JFrame เป็น container ที่สืบทอดมาจากFrame ของAWT ซึ่งเป็น Top level container ที่สามารถอยู่เดี่ยวๆได้ ไม่ต้องอาศัย window ลักษณะที่สำคัญของ JFrame คือ การมีปุ่มปิดปุ่มคืนสภาพ และปุ่มย่อขนาดให้เล็กสุด

  14. Minimize Button Title Bar Restore Button Close Button ความสูง : y ความยาว : x

  15. การสร้าง • สร้างวัตถุ JFrame ขึ้นใหม่ โดยใช้คำสั่ง JFrame ชื่อเฟรม = new JFrame(" Title bar ") • การสืบทอดจากJFame โดยใช้คำสั่ง class ชื่อคลาส extends JFrame

  16. import javax.swing.*; • class JFrameDemo { • public static void main(String s[]) { • JFrame frame = new JFrame("JFrame Source Demo"); • frame.setSize(200,150); • frame.setVisible(true); • } • }

  17. import javax.swing.*; • class JFrameDemo extends JFrame { • public static void main(String s[ ]) { • JFrameDemo f = new JFrameDemo(); • f.setTitle("Test"); • f.setSize(200,150); • f.setVisible(true); • } • }

  18. Method การกำหนดค่า

  19. Method การรับค่า

  20. JPanel

  21. Top Level Container JPanel OK Top Level Container OK OK JPanel • เป็น container ที่ไม่สามารถอยู่อย่างอิสระต้องอยู่ภายใต้ Top Level Container

  22. import java.awt.*; • class TestPanel{ • public static void main(String s[]){ • JFrame f = new JFrame ("Test"); • JPanel p = new JPanel(); • JPanel p1 = new JPanel(); • f.setLayout(new GridLayout()); • p.setBackground(Color.blue); • p1.setBackground(Color.BLACK); • f.add(p); • f.add(p1); • f.setSize(100,100); • f.show(true); • } • }

More Related