lec05 01 jlistsiam2dev.net/e_learning/oop/lec05_01_jlist_pdf.pdf · 2019. 5. 20. · lec05_01_jlist...

31
Lec05_01_jList By Dr. nattapong Songneam 17-05-2562 การใช งาน jList คาส งทาซ (Loop)

Upload: others

Post on 16-Oct-2020

0 views

Category:

Documents


0 download

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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

รายการทีแ่สดงใน 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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

ในปุ่ ม 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 • คาสั่งทาซ้า

ค าสัง่ 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 • คาสั่งทาซ้า

ตัวอย่างโจทย์• จงแสดงตวัเลข 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 • คาสั่งทาซ้า

เพิม่ 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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

จงแสดงตวัเลข 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 • คาสั่งทาซ้า

เพ่ิม 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 • คาสั่งทาซ้า
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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

โจทย ์เพิม่เตมิ

• จงหาผลรวม ตัง้ ½ + 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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

จงหาผลรวม ตัง้แต ่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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

ผลลพัธ์

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 • คาสั่งทาซ้า

การเคลยีรค์า่ใน 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 • คาสั่งทาซ้า

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 • คาสั่งทาซ้า

การเคลียร์หมายถึงเคลียร์ใน 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 • คาสั่งทาซ้า

ลบทีละรายการ 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 • คาสั่งทาซ้า
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 • คาสั่งทาซ้า

// เกบ็รายการท่ีถูกเลือก

• ในเหตกุารณ์ตอนคลกิท่ี 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 • คาสั่งทาซ้า

btnRemove

เขา้ไปทีเ่หตกุารณ์เมาสค์ลกิ