lec05 01 jlistsiam2dev.net/e_learning/oop/lec05_01_jlist_pdf.pdf · 2019. 5. 20. · lec05_01_jlist...
TRANSCRIPT
![Page 1: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/1.jpg)
Lec05_01_jList
By
Dr. nattapong Songneam
17-05-2562
• การใชง้าน jList• ค าสัง่ท าซ ้า (Loop)
![Page 2: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/2.jpg)
jList
• Control / Tool : jList• ส ำหรบัแสดงผลรำยกำร ทลีะหลำยรำยกำร
• เชน่แสดงตวัเลข 1..100
• แสดงตวัเลขคี ่1 3 5 7 9 ..99
• แสดงตวัเลขคู ่2 4 6 8 10 ..100
• แสดงรำยกำรสนิคำ้
• แสดงรำยชือ่พนกังำน
![Page 3: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/3.jpg)
รายการทีแ่สดงใน jList
• iTem
• Model คอื ชดุของขอ้มลูทีจ่ะแสดงใน list
• จะเพิม่จะลบ ก็ท างานผา่น Model
![Page 4: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/4.jpg)
1. Declare an instance of DefaultListModel:
1.1 import javax.swing.DefaultListModel;
1.2 DefaultListModel listModel;
ประกาศ listModel เป็นตวัแปรของคลาสซึง่สามารถใชง้านภายในคลาส ไดห้ลายเหตกุารณ์
![Page 5: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/5.jpg)
2. Create it:
listModel = new DefaultListModel();
DefaultListModel listModel = new DefaultListModel();
สามารถประกาศตวัแปรพรอ้มกบัสรา้งอนิสแตนซพ์รอ้มกนัไดด้งันี้
![Page 6: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/6.jpg)
3. Populate it (now or later):การเพ่ิมรายการใน Model• listModel.addElement(“item 1”); // String
• listModel.addElement(“item 2”); // String
• listModel.addElement(3); // constant
• listModel.addElement(x); // variable
• listModel.addElement(empName); // variable
item 1item 2
3?
????
![Page 7: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/7.jpg)
4. Use the DefaultListModel in the constructor of a JList (instead of a String array):
• JList list = new JList(listModel);
• jList1 = new JList(listModel);
item 1item 2
3?
????
![Page 8: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/8.jpg)
btnFor
เขา้ไปทีเ่หตกุารณ์การคลกิเมาส์
EventMouseMouse_click
![Page 9: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/9.jpg)
ในปุ่ ม For ใหว้นลปูต ัง้แต ่1 – 10 แลว้แสดงตวัเลข ใน listModelจำกน ัน้น ำ listModel ใสใ่น jList1
1..100
![Page 10: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/10.jpg)
ค าสัง่ for..loop
• รปูแบบfor(…… , ….. ,….. ) {
}
• รปูแบบfor(คา่เริม่ตน้ , เงือ่นไข , เพิม่/ลดคา่) {
}
• ตวัอยา่งfor(byte b=1; , b <=5; , b++) {
}
• byte
• -128 ..127
![Page 11: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/11.jpg)
ตัวอย่างโจทย์• จงแสดงตวัเลข 1..100
• จงแสดงตวัเลข 1..1000
• จงแสดงตวัเลข 1 3 5 7 9 ...99
• จงแสดงตวัเลข 2 4 6 8 10 ...100
• จงแสดงตวัเลข 99 97 95 93..3 1
• จงแสดงตวัเลข 98 96 94 92..2
• จงหาผลรวมของตวัเลข 1+2+3+4+..100
• จงหาผลรวมของตวัเลข 1+3+5+7+9+...99
• จงหาผลรวมของ 99 + 97 + 95 + ... + 3 +1
• จงหาผลรวมของ 98 + 96 + 94 + ... + 2
jLabeljTextfield
jListInterval=2 คาบ หรือ ช่วงข้อมลู
![Page 12: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/12.jpg)
เพิม่ button 2 ตัว ดังรปู
• btnFor2
• btnFor3จงแสดงตวัเลข 99 97 95 93..3 1
จงหาผลรวมของ 98 + 96 + 94 + ... + 2
![Page 13: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/13.jpg)
for2_MouseClicked
![Page 14: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/14.jpg)
จงแสดงตวัเลข 99 97 95 93..3 1
![Page 15: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/15.jpg)
เพ่ิม jLabel
lbResult
จงหาผลรวมของ 98 + 96 + 94 + ... + 2
![Page 16: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/16.jpg)
![Page 17: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/17.jpg)
do…while
• รูปแบบ
• do {
• } while(……);
ตวัอยา่ง
int x;int sum=0;
do {
x++;
sum+=x;} while(x<10);
1..10
จงแสดงตวัเลข 2 4 6 8 10 ...100 jList
![Page 18: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/18.jpg)
โจทย ์เพิม่เตมิ
• จงหาผลรวม ตัง้ ½ + 1/3 + ¼…..1/101
• จงหาผลรวม ตัง้แต ่22 + 24 + 26 + …+ 212
• จงหาผลรวม ตัง้แต ่21 + 22 + 23 + …+ 210
• จงหาผลรวม ตัง้แต ่11 + 22 + 33 + …+ 1010
• Math.pow(x,y)
![Page 19: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/19.jpg)
btnDo
![Page 20: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/20.jpg)
do…while
![Page 21: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/21.jpg)
จงหาผลรวม ตัง้แต ่11 + 22 + 33 + …+ 1010
รปูแบบ
while(……) {
….
}
ตัวอยา่ง 1..10
int I=1;
while(i<=10) {
i++;
System.out.println(i);
}
ถา้ i=11 จะออกจากลปู
Math.pow(i,i);
ตวัอยา่ง 11 + 22 + 33 + …+ 1010
int i=1;
double sum=0.0;
while(i<=10) {
sum+=Math.pow(i,i);
i++;
}
lbResult.setText(String.valueOf(sum));
double
![Page 22: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/22.jpg)
btnWhile
• btnWhileMouseClicked
![Page 23: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/23.jpg)
method
![Page 24: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/24.jpg)
ผลลพัธ์
![Page 25: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/25.jpg)
การเคลยีรค์า่ใน jList
การเคลยีร ์ทกุรายการ หรอืทัง้หมด
• listModel.clear();
• jList1.setModel(listModel);
![Page 26: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/26.jpg)
btnClear
![Page 27: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/27.jpg)
การเคลียร์หมายถึงเคลียร์ใน listModelดงันัน้ตอนแรกสดุของเหตกุารณ์ คือตอนฟอร์มโหลด หรือ activatejList ยงัไมไ่ด้เพิ่ม listModel เข้าไป อาจจะมีข้อผิดพลาดได้
![Page 28: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/28.jpg)
ลบทีละรายการ remove
• listModel.remove(index); // index เร่ิมต้นจาก 0…..
• listModel.remove(5);
0
4
เม่ือคลิกเมาส์ท่ี jList ให้ก าหนดรายการท่ีเลือก ดงันัน้ต้องประกาศตวัแปร ของคลาสเป็นจ านวนเตม็เพ่ือจะเก็บล าดบัท่ีต้องการจะลบออก
![Page 29: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/29.jpg)
![Page 30: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/30.jpg)
// เกบ็รายการท่ีถูกเลือก
• ในเหตกุารณ์ตอนคลกิท่ี jList ให้เก็บค่า index ท่ีถกูเลือก
![Page 31: Lec05 01 jListsiam2dev.net/E_Learning/OOP/Lec05_01_jList_PDF.pdf · 2019. 5. 20. · Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 • การใชง้านjList • คาสั่งทาซ้า](https://reader036.vdocuments.mx/reader036/viewer/2022090603/605562dfb31d486f253cb09d/html5/thumbnails/31.jpg)
btnRemove
เขา้ไปทีเ่หตกุารณ์เมาสค์ลกิ