เอาล่ะ ฉันมีปัญหาซึ่งไม่รู้ว่ามีอะไรผิดปกติ ดังนั้นฉันจึงมี jList และ List ฉันต้องการฟังก์ชั่นที่เมื่อฉันคลิกที่รายการ (ใน jList รายการใด ๆ ) และมันจะเปลี่ยนไปในไอคอนป้ายกำกับของฉัน (ฉันกำลังจัดการกับรูปภาพ) มันใช้งานได้ เปลี่ยนไอคอนป้ายกำกับของฉันเป็นรูปภาพที่ฉันเลือกจาก jList แต่มันส่งข้อยกเว้นมาให้ฉันและโปรแกรมล่ม โดยปกติแล้ว 2 รายการแรกจะไม่ทำให้เกิดข้อผิดพลาด รายการที่สามและรายการถัดไปทำให้เกิดข้อผิดพลาด หลังจากที่มันขัดข้องและโยนข้อความสีแดงจำนวนมากให้ฉัน ฉันยังคงสามารถเปลี่ยนไอคอนได้
นี่คือฟังก์ชั่นที่ฉันได้รับรูปภาพและเพิ่มลงในรายการ (เพิ่มเส้นทางให้กับรูปภาพเหล่านั้น)
private static void getImages(String src) throws IOException {
//Exctract the name of the image from the src attribute
int indexname = src.lastIndexOf("/");
if (indexname == src.length()) {
src = src.substring(1, indexname);
}
indexname = src.lastIndexOf("/");
String name = src.substring(indexname, src.length());
//Open a URL Stream
URL url = new URL(src);
InputStream in = url.openStream();
GPath=fPath+name;
OutputStream out = new BufferedOutputStream(new FileOutputStream( GPath));
//Im adding to the list string (link to image) here
imagesListN.add(GPath);
System.out.println("list size: "+imagesListN.size());
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}
มันก็เพิ่มตามปกติ ใช่ ฉันกำลังดาวน์โหลดมัน นั่นคือเหตุผลว่าทำไมฉันถึงอยากเห็นมันเมื่อดาวน์โหลดเสร็จแล้ว
นี่คือที่ฉันคลิกที่ฟังก์ชัน jList
list.addListSelectionListener(new ListSelectionListener() {
private int a;
@Override
public void valueChanged(ListSelectionEvent arg0) {
if (!arg0.getValueIsAdjusting()) {
String h;
int k;
k = list.getSelectedIndex();
System.out.println("List id: "+k);
a = Main.imagesListN.indexOf(k);
System.out.println("imagesListN id: "+a);
h = Main.imagesListN.get(k);
System.out.println("h : "+h);
ImageIcon img = new ImageIcon(h);
imageReview.setIcon(img);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
นี่คือ jList (รายชื่อ) ข้อยกเว้นอยู่ที่
a = Main.imagesListN.indexOf(k);
มันทำให้ฉัน -1 แต่ h = Main.imagesListN.get(k); ให้ลิงก์ที่ฉันต้องการและให้ ImageIcon img = new ImageIcon(h); em> จากนั้น imageReview.setIcon(img); ไอคอนป้ายกำกับเปลี่ยนทุกครั้งที่ฉันคลิกรายการที่ฉันต้องการ บางทีมันอาจจะไม่ใช่ a = Main.imagesListN.indexOf(k); ที่ฉันกำลังดูอยู่ แต่มีบางอย่างให้ฉัน -1 อย่างไรก็ตามฉันกำลังดำเนินการทุกอย่างใน Thread
`public class Crawler extends Thread {
Main main = new Main();
public void run(){
try {
main.download();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}`
ไม่มีอะไรแฟนซีที่นี่ แต่ละฟังก์ชั่นอยู่ในคลาสของตัวเอง getImages() อยู่ใน main , listListener อยู่ในคลาส Langas (คลาสที่ปุ่มทั้งหมด, ป้ายกำกับ ฯลฯ ไม่ได้ถูกสร้างขึ้นไม่มีอะไรอื่น) และ Thread well , Thread นอกจากนี้ยังใช้งานได้ดีหลังจากดาวน์โหลดทุกอย่างแล้ว โดยไม่มีข้อยกเว้น เกิดข้อผิดพลาดระหว่างกระบวนการดาวน์โหลด
public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (list.getSelectedIndex() == -1) { //No selection, disable fire button. fireButton.setEnabled(false); } else { //Selection, enable the fire button. fireButton.setEnabled(true); } } }
- person ibrahim demir   schedule 11.03.2015