Swingطريقة زيادة قيمة الـ JProgressBar بواسطة Thread
المثال التالي يعلمك طريقة زيادة قيمة الـ Progress Bar بوسطة Thread.
مثال
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JProgressBar demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(300, 150); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 150
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // في النافذة بنفسنا Progress Bar لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا
JProgressBar pb = new JProgressBar(0, 1000); // قيمه بين 0-1000 Progress Bar أي قمنا بإنشاء JProgressBar هنا أنشأنا كائن من الكلاس
pb.setStringPainted(true); // ظاهرة Progress Bar هنا جعلنا قيمة الـ
pb.setBounds(70, 40, 140, 30); // frame في الـ Progress Bar هنا قمنا بتحديد حجم و موقع الـ
frame.add(pb); // frame في الـ Progress Bar هنا أضفنا الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
int currentValue = pb.getValue(); // currentValue الأولية في المتغير Progress Bar هنا قمنا بتخزين قيمة الـ
while(currentValue < pb.getMaximum()) // كل 0.1 ثانية Progress Bar هنا سيتم إضافة 10 على قيمة الـ
{
try{ Thread.sleep(100); }
catch(Exception e){ }
currentValue += 10;
pb.setValue(currentValue);
}
}
}
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JProgressBar demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(300, 150); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 150
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // في النافذة بنفسنا Progress Bar لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا
JProgressBar pb = new JProgressBar(0, 1000); // قيمه بين 0-1000 Progress Bar أي قمنا بإنشاء JProgressBar هنا أنشأنا كائن من الكلاس
pb.setStringPainted(true); // ظاهرة Progress Bar هنا جعلنا قيمة الـ
pb.setBounds(70, 40, 140, 30); // frame في الـ Progress Bar هنا قمنا بتحديد حجم و موقع الـ
frame.add(pb); // frame في الـ Progress Bar هنا أضفنا الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
int currentValue = pb.getValue(); // currentValue الأولية في المتغير Progress Bar هنا قمنا بتخزين قيمة الـ
while(currentValue < pb.getMaximum()) // كل 0.1 ثانية Progress Bar هنا سيتم إضافة 10 على قيمة الـ
{
try{ Thread.sleep(100); }
catch(Exception e){ }
currentValue += 10;
pb.setValue(currentValue);
}
}
}
ستظهر لك النافذة التالية عند التشغيل.
لاحظ أن قيمة الـ Progress Bar ستزيد بشكل تلقائي عند تشغل البرنامج حتى تصل للقيمة القصوى.