الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

TimeUnit example

بدأه Muhammed Magdy في 19 أكتوبر 2014 · 1 رد · 967 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

 

في هذا المثال المفروض يغير النص كل 3 ثواني ، عند عمل Run يظهر آخر نص فقط "Welcome"

 

 

TimeUnit01_zps070dd0b3.png

 

الكود

import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class TimeUnit_Ex extends JFrame {

    public TimeUnit_Ex() throws Exception {
        
        final JLabel l1 = new JLabel();

        TimeUnit.SECONDS.sleep(3);
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                l1.setText("A Label");
            }
        });

        TimeUnit.SECONDS.sleep(3);
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                l1.setText("this is different");
            }
        });
        TimeUnit.SECONDS.sleep(3);
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                l1.setText("Welcome");
            }
        });
        setTitle("Exception example 01");
        setVisible(true);
        setSize(300, 100);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        add(l1);
    }

}
#2

import java.awt.FlowLayout;

import java.util.concurrent.TimeUnit;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.SwingUtilities;

public class TimeUnit_Ex extends JDialog {

public TimeUnit_Ex() throws Exception {

getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));

final JLabel label = new JLabel("Loading ..");

add(label);

pack();

setLocationRelativeTo(this);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setVisible(true);

TimeUnit.SECONDS.sleep(3);

SwingUtilities.invokeLater(new Runnable() {

public void run() {

label.setText("A Label");

}

});

TimeUnit.SECONDS.sleep(3);

SwingUtilities.invokeLater(new Runnable() {

public void run() {

label.setText("This is different");

}

});

TimeUnit.SECONDS.sleep(3);

SwingUtilities.invokeLater(new Runnable() {

public void run() {

label.setText("Welcome");

}

});

}

public static void main(String args[]) throws Exception {

new TimeUnit_Ex();

}

}

1

مواضيع مشابهة