使用Swing Timer 和SwingWorker 持續更新JLabel
為了使用耗時活動的結果定期更新JLabel,建議同時使用Swing Timer 和SwingWorker 。 Swing Timer 處理重複性任務,而 SwingWorker 管理耗時的任務。以下是一個範例程式碼,說明了此策略的運作方式:
<code class="java">import java.awt.event.*; import javax.swing.*; import java.net.Socket; public class LabelUpdateUsingTimer { static String hostnameOrIP = "stackoverflow.com"; int delay = 5000; JLabel label = new JLabel("0000"); LabelUpdateUsingTimer() { label.setFont(label.getFont().deriveFont(120f)); ActionListener timerListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new PingWorker().execute(); } }; Timer timer = new Timer(delay, timerListener); timer.start(); JOptionPane.showMessageDialog( null, label, hostnameOrIP, JOptionPane.INFORMATION_MESSAGE); timer.stop(); } class PingWorker extends SwingWorker { int time; @Override protected Object doInBackground() throws Exception { time = pingTime(); return new Integer(time); } @Override protected void done() { label.setText("" + time); } }; public static int pingTime() { Socket socket = null; long start = System.currentTimeMillis(); try { socket = new Socket(hostnameOrIP, 80); } catch (Exception weTried) { } finally { if (socket != null) { try { socket.close(); } catch (Exception weTried) {} } } long end = System.currentTimeMillis(); return (int) (end - start); } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { new LabelUpdateUsingTimer(); } }; SwingUtilities.invokeLater(r); } }</code>
有關事件調度線程以及在GUI 中執行重複或擴展任務的更多詳細信息,請查閱Swing 中的並發文件.
程式碼的工作原理:
- 初始化:LabelUpdateUsingTimer 建構函式初始化一個名為 label 的 JLabel 並設定其字體。
- ActionListener 和 Timer:建立一個名為timerListener 的ActionListener,它會每隔delay 毫秒觸發PingWorker 的執行。然後使用延遲和timerListener作為參數啟動Timer對象,並隨後啟動它。
- JOptionPane:JLabel顯示在具有指定主機名稱或IP的JOptionPane對話框中。
- PingWorker:PingWorker 類,SwingWorker 的子類,在後台執行緒中執行耗時操作。
- Ping Time:pingTime 方法建立與指定主機名稱或 IP 的 TCP 連線並計算延遲。
透過採用此技術,您可以使用耗時任務的最新結果不斷更新 JLabel,從而實現即時監控和使用者互動。
以上是如何使用 Swing Timer 和 SwingWorker 持續更新 JLabel?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JvMenablesjava的“寫入,runanywhere” bycompilingCodeIntoplatform-獨立bytecode,whatittheninterpretsorpretsorcompilesIntolachine-specificcode.itoptimizesperformizesperformanceWithJitCompilationWithJitCompilation,ManagesMemoryThroughgargargargargarggarbagecollection,and andensuressececerity

JVM版本對Java程序的影響包括兼容性、性能優化、垃圾回收策略、安全性和語言特性。 1)兼容性:確保代碼和依賴庫在新JVM上運行。 2)性能:新JVM提升垃圾回收和JIT編譯性能。 3)安全性:修復安全漏洞,提升整體安全性。 4)新特性:如Java8的Lambda表達式和Java17的ZGC垃圾收集器,簡化代碼並提升效率。

JVM實現Java的“一次編寫,到處運行”通過將Java字節碼轉換為特定於機器的指令。 1.類加載器加載類。 2.運行時數據區存儲數據。 3.執行引擎轉換字節碼。 4.JNI允許與其他語言交互。 5.本地方法庫支持JNI調用。

java'spowerstemsssfrom:1)平台獨立viabytecodeandjvm,enaplingCross-platformDevelopment; 2)面向對象的程序,促進促進,促進modularityThroughCapsulation,sastalitance,sastalitance和pollemyormormormormormormormormormormormorphism; 3)AutomaticMememoryManagementwithGargarGargarGargarBagagagageCollection,reduccoltection,reduccoltection

No,theJVMisnotthesameforeveryplatform.1)TheJVMprovidesalayerofabstractionforrunningJavabytecode,butitsimplementationvariesbyplatform.2)DifferentversionsoftheJVM,likeOracle'sHotSpot,aretailoredforspecificoperatingsystemstooptimizeperformanceandcompati

Java是平台獨立的,因為其"一次編寫,到處運行"的設計理念,依賴於Java虛擬機(JVM)和字節碼。 1)Java代碼編譯成字節碼,由JVM解釋或即時編譯在本地運行。 2)需要注意庫依賴、性能差異和環境配置。 3)使用標準庫、跨平台測試和版本管理是確保平台獨立性的最佳實踐。

Java'splatFormIndenceIsnotsimple; itinvolvesComplexities.1)jvmcompatiblemustbebeeniblemustbeensuredacrossplatforms.2)Nativelibrariesandsystemcallsneedcarefulhandling.3)

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境