搜尋
首頁Javajava教程儘管計時器正確重複,為什麼我的 Java Swing Timer 的 ActionListener 沒有觸發操作?

Why is my Java Swing Timer's ActionListener not triggering actions despite the timer repeating correctly?

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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Java平台獨立性:OS之間的差異Java平台獨立性:OS之間的差異May 16, 2025 am 12:18 AM

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

Java的最佳功能:從面向對象的編程到安全性Java的最佳功能:從面向對象的編程到安全性May 16, 2025 am 12:15 AM

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

JavaScript與Java的最佳功能JavaScript與Java的最佳功能May 16, 2025 am 12:13 AM

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

Java平台獨立性:收益,限制和實施Java平台獨立性:收益,限制和實施May 16, 2025 am 12:12 AM

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

Java:真實詞的平台獨立性Java:真實詞的平台獨立性May 16, 2025 am 12:07 AM

java'splatformendependecemeansapplicationscanrunonanyplatformwithajvm,使“ Writeonce,runanywhere”。

JVM性能與其他語言JVM性能與其他語言May 14, 2025 am 12:16 AM

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

Java平台獨立性:使用示例Java平台獨立性:使用示例May 14, 2025 am 12:14 AM

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

JVM架構:深入研究Java虛擬機JVM架構:深入研究Java虛擬機May 14, 2025 am 12:12 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

北端:融合系統,解釋
1 個月前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
4 週前By尊渡假赌尊渡假赌尊渡假赌
<🎜>掩蓋:探險33-如何獲得完美的色度催化劑
2 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Mac版

SublimeText3 Mac版

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

SublimeText3 英文版

SublimeText3 英文版

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

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用