giáo trình Java By Example phần 9

Tham khảo tài liệu 'giáo trình java by example phần 9', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 100 catch InterruptedException e public void paint Graphics g displayStr 50 130 Tell Java that the applet uses the classes in the awt package. Tell Java that the applet uses the classes in the applet package. Derive ThreadApplet from Applet and implement Runnable. Declare the class s data fields including a Thread object. Override the start method. Create and set the applet s display font. Initialize data fields. Create and start the thread. Override the stop method. Stop the thread. Implement the run method Loop one thousand times. Increment the counter. Create the display string from the counter. Tell Java to repaint the applet. Suspend the thread for one hundred milliseconds. Override the paint method. http Draw the display string. There are a couple of interesting things in ThreadApplet of which you should be aware. First notice that in run the thread loops one thousand times after which the while loop ends. When the while loop ends so does the run method. This means that when you run ThreadApplet if you let it count all the way to one thousand the thread ends on its own. However what if you switch to a different Web page before ThreadApplet has counted all the way to one thousand Then Java calls the applet s stop method which ends the thread by calling the thread s stop method. The next point of interest is what s going on inside run . At the beginning of the loop the program increments the counter converts the counter s value to a string and then repaints the applet so that the new count value appears in the window. That code should be as clear as glass to you by now. But what s all that malarkey after the call to repaint That s where the thread not only times the animation but also relinquishes the computer so that other threads get a chance to run. Simply the call to the thread s sleep method suspends the thread for the number of milliseconds given as its single argument. In this case the sleep time is 100 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
100    349    2    29-04-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.