1 / 19

Java Desktop Application #4

Java Desktop Application #4. โปรแกรมคำนวณ พ.ท. แบบต่าง ๆ NetBean 6.0 & swing set โดย อ. นัฐพงศ์ ส่งเนียม http://www.siam2dev.com. ออกแบบหน้าจอโปรแกรม ดังรูป. หลักการทำงาน

Download Presentation

Java Desktop Application #4

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. Java Desktop Application #4 โปรแกรมคำนวณ พ.ท. แบบต่าง ๆ NetBean 6.0& swing set โดย อ. นัฐพงศ์ ส่งเนียม http://www.siam2dev.com

  2. ออกแบบหน้าจอโปรแกรม ดังรูป หลักการทำงาน โปรแกรมคำนวณพื้นที่ต่าง ๆ แบบนี้สามารถเลือกได้ว่าต้องการ คำนวณค่าใด ๆ โดยผู้ใช้จำเป็นต้องเลือก ค่าการคำนวณ จาก ComboBoxแล้ว ก็ ป้อนข้อมูลที่จำเป็นจากนั้นก็ กด ปุ่ม OK แล้วโปรแกรมจะทำการคำนวณ และแสดง ผลลัพธ์ ออกมา ดังรูป

  3. สร้าง โปรเจ็กต์ใหม่ • ไปที่เมนู File • เลือก New Project • เลือก Java Desktop Application • กำหนดชื่อ desktop_app4

  4. รู้จักและใช้งาน control ต่าง ๆ • jLabel • jTextField • jButton • jComboBox

  5. ออกแบบหน้าจอโปรแกรม jComboBox jTextField jLabel

  6. ออกแบบหน้าจอโปรแกรม (ต่อ…) Button jLabel

  7. การกำหนดคุณสมบัติของ jComboBox1 • คุณสมบัติที่สำคัญของ jComboBox คือ • - model :: ใช้สำหรับกำหนด/เพิ่ม รายการที่อยู่ใน jComboBox1 • - selectedItem :: สำหรับแสดงรายการที่ถูกเลือกจากรายการทั้งหมดที่อยู่ใน model • การกำหนดคุณสมบัติ สามารถทำได้ดังนี้ • Click เลือกที่ jComboBox1 • ในหน้าต่าง Properties ให้ เลือก model • จะปรากฏหน้าต่างในการเพิ่มรายการ ซึ่ง • จะมี item1 – item4 ปรากฏอยู่แต่เรา • สามารถลบ และพิมพ์รายการของเราได้เอง • ในช่อง item แล้วก็กด ปุ่ม add • ในที่นี้ให้เพิ่มรายการ ดังตัวอย่างในรูป

  8. Code ในส่วนของ jComboBox มีการเลือกค่าใด ๆ • การเข้าไปเขียนคำสั่งในเหตุการณ์ jComboBox1ItemStateChanged ทำได้ดังนี้ • Click ขวาที่ jComboBox1 • เลือก Event • เลือก Item • เลือก ItemStateChanged private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) { // TODO add your handling code here: jLabel1.setText(jComboBox1.getSelectedItem().toString()); }

  9. คำสั่งในปุ่ม OK private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: jLabel1.setText(jComboBox1.getSelectedItem().toString()); if(jComboBox1.getSelectedIndex() == 0) { // this is circle area calculation .. int n1 = Integer.parseInt(jTextField1.getText()); double cArea = Math.PI*Math.pow(n1,2); jLabel2.setText(Double.toString(cArea)); } else if (jComboBox1.getSelectedIndex() == 1) { // this is Rectangle area calculation .. } }

  10. Code ในปุ่ม Cancel • การเข้าไปเขียนคำสั่งในเหตุการณ์ jButton2MouseClicked ทำได้ดังนี้ • Click ขวาที่ jButton2 • เลือก Event • เลือก mouse • เลือก mouseclick private void jButton2MouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jLabel1.setText(""); jLabel2.setText(""); jTextField1.requestFocus(); }

  11. อธิบายคำสั่งที่สำคัญ :: การแสดงข้อความใน jLabel • หากเราต้องการพิมพ์ข้อความไปที่ Label ใด ๆ ก็ใช้ method setText() ดัง ตัวอย่าง • ถ้าต้องการพิมพ์ คำว่า “Hello” ใน jLabel1 ก็เขียนคำสั่งได้ ดังนี้ • ถ้าหากข้อความที่ต้องการแสดงนั้นเป็นค่าจากตัวแปร ก็ ไม่ต้องใส่เครื่องหมาย “” ดังตัวอย่าง jLabel1.setText(“Hello”); String str = “Hello”; jLabel1.setText(str);

  12. อธิบายคำสั่งที่สำคัญ :: การลบข้อความใน jLabel • หากเราต้องการลบข้อความที่อยู่ใน Label ใด ๆ ก็ใช้ method setText() เหมือนกันกับตอนพิมพ์ข้อความแต่ต่างกันตรงไม่ต้องระบุอาร์กิวเมนต์หลัง method ดังตัวอย่าง • ถ้าต้องการลบข้อความใน jLabel1 ก็เขียนคำสั่งได้ ดังนี้ jLabel1.setText(“”);

  13. อธิบายคำสั่งที่สำคัญ :: การแสดงค่าที่ผู้ใช้ได้เลือกจาก jComboBox1 • การเข้าไปเขียนคำสั่งในเหตุการณ์ jComboBox1ItemStateChanged ทำได้ดังนี้ • Click ขวาที่ jComboBox1 • เลือก Event • เลือก Item • เลือก ItemStateChanged private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) { // TODO add your handling code here: jLabel1.setText(jComboBox1.getSelectedItem().toString()); }

  14. อธิบายคำสั่งที่สำคัญ :: การแสดงค่าที่ผู้ใช้ได้เลือกจาก jComboBox1 หมายถึง เราจะแสดงข้อความที่ได้จาก การที่ผู้ใช้เลือกรายการ(selectedItem) ที่อยู่ใน jComboBox1 ก็สามารถใช้ method getSelectedItem() ของ jComboBox1 นั่นเอง jLabel1.setText(jComboBox1.getSelectedItem().toString());

  15. การปรับแต่งหน้าจอโปรแกรมให้ สมบูรณ์ขึ้น เพิ่ม jLabel หน้า jTextField ทั้ง 3

  16. การแสดง / ซ่อน swing control ในบางครั้งเราจำเป็นต้องแสดง/ซ่อน ออบเจ็กต์บางตัวเพื่อให้โปรแกรมสามารถรับค่า ข้อมูลให้เหมาะกับ การคำนวณบางประเภท เช่น หากเราต้องการคำนวณค่า พ.ท. สามเหลี่ยม ข้อมูลที่ต้องรับเข้าไป ก็ คือ ค่า ฐาน กับ สูง ดังนั้นเราจึงจำเป็นต้องแสดง jTextFied เฉพาะ ตัวที่ 1 และ 2 เท่านั้น ดังรูป สั่งให้แสดง/ซ่อน ก็ ใช้ method setVisible(boolean b) โดย ถ้า b เป็น true ก็สั่งให้แสดงออบเจ็กต์ ถ้า b เป็น false ก็สั่งให้ซ่อนออบเจ็กต์

  17. การแสดง / ซ่อน swing control จากตัวอย่างข้างต้น นั้นเราสามารถเข้าไปเขียนคำสั่งได้ในเหตุการณ์ ตอนที่ผู้ใช้เลือกรายการจาก jComboBox1 นั่นเอง หรือ เหตุการณ์ jComboBox1ItemStateChanged private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) { // TODO add your handling code here: }

  18. ต.ย. คำสั่งใน jComboBox1ItemStateChanged private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) { // TODO add your handling code here: if(jComboBox1.getSelectedIndex()==0) { jLabel3.setText("รัศมี :"); jLabel4.setVisible(false); jTextField2.setVisible(false); jLabel5.setVisible(false); jTextField3.setVisible(false); } else if(jComboBox1.getSelectedIndex()==1) { jLabel3.setText("ฐาน :"); jLabel4.setText("สูง :"); jLabel4.setVisible(true); jTextField2.setVisible(true); jLabel5.setVisible(false); jTextField3.setVisible(false); } }

More Related