Javax.swing 計時器重複正常,但ActionListener 不執行任何動作
在提供的程式碼片段中,計時器已成功配置為定期重複。但是,與計時器關聯的 ActionListener 在計時器到期時不會觸發任何操作。
ActionListener 的 actionPerformed 方法負責在計時器觸發時採取操作。在本例中,該方法嘗試在白色和粉紅色之間切換文字欄位的背景顏色。然而,儘管計時器不斷執行,文字欄位的背景顏色仍然保持不變。
問題的根源在於使用靜態內部類別作為 ActionListener。靜態內部類別有一個獨特的特徵,它們只能存取其封閉類別的靜態成員。在這種情況下,spreademPanel 和historyPnl 變數不是靜態的,因此在Flash 類別中無法存取。
要解決此問題,可以將spreademPanel 和historyPnl 設為靜態,或建立Flash 類別的實例並傳遞它作為 ActionListener。
這是基於實例的方法的範例:
<code class="java">// Instance-based ActionListener class Flash implements ActionListener { private JComponent textfield; public Flash(JComponent textfield) { this.textfield = textfield; } @Override public void actionPerformed(ActionEvent evt) { if (this.flasher) { textfield.setBackground(Color.white); } else { textfield.setBackground(Color.pink); } this.flasher = !this.flasher; } //actionPerformed } // Main class ... // Setup timer Flash flash = new Flash(SpreademPanel.historyPnl.NameTxt); // Pass the text field to the Flash instance tmr = new javax.swing.Timer(1000, flash); ...</code>
以上是儘管計時器正確重複,為什麼我的 Java Swing Timer 的 ActionListener 沒有觸發操作?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Java在不同操作系統上的表現存在細微差異。 1)JVM實現不同,如HotSpot、OpenJDK,影響性能和垃圾回收。 2)文件系統結構和路徑分隔符不同,需使用Java標準庫處理。 3)網絡協議實現差異影響網絡性能。 4)GUI組件外觀和行為在不同系統上有別。通過使用標準庫和虛擬機測試,可減少這些差異的影響,確保Java程序穩定運行。

javaoffersrobustobject-IentiendedProgrammming(OOP)和Top-Notchsecurityfeatures.1)OopinjavainCludesClasses,對象,繼承,多態性,和列出,andeclingfleximaintainablesys.ss.2)SecurityFeateTuersLudEtersludEterMachine(

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

JAVAACHIEVESPLATFORMINDEPENTENCETHROUGHJAVAVIRTAILMACHINE(JVM)和BYTECODE.1)THEJVMINTERPRETSBBYTECODE,允許theingthesmecodetorunonanyanyanyanyplatformwithajvm.2)

java'splatformendependecemeansapplicationscanrunonanyplatformwithajvm,使“ Writeonce,runanywhere”。

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生產性。 1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允許CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用