如何使用getSource() 擷取按鈕值
在GUI 計算器中,您已正確使用getSource() 方法來偵測按鈕點擊次數。但是,您僅捕獲用於操作( 、 - 、 * 、 / 、 C )的按鈕,但您還需要處理數字按鈕。
要檢索每個按鈕的值,請按照以下步驟操作:
- 為數字按鈕建立一個單獨的操作偵聽器。 在您的程式碼中,您目前有一個操作偵聽器來處理所有按鈕(bAdd、 bSub 等)。為數字按鈕建立單獨的動作監聽器(例如 numActionListener)。
- 註冊數位按鈕的動作監聽器。 將 numActionListener 加到所有數字按鈕。例如:
b1.addActionListener(numActionListener); b2.addActionListener(numActionListener); b3.addActionListener(numActionListener); // and so on...
- 重寫數字按鈕動作監聽器的actionPerformed方法。 在actionPerformed方法中,您可以檢索特定數字的值使用 getSource() 方法點選的按鈕。
@Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); // Check if the source is a number button if (source instanceof Button) { Button button = (Button) source; // Get the value of the button String buttonValue = button.getLabel(); // Append the value to the input text field (e.g., tf1) tf1.setText(tf1.getText() + buttonValue); } // ... (continue with your existing code to handle operation buttons) }
透過以下操作透過這些步驟,您可以在點擊數字按鈕和操作按鈕時檢索它們的值。這將允許您建立一個功能齊全的計算器,接受使用者輸入的數字和運算。
以上是如何在 Java GUI 計算器中使用 getSource() 檢索數字按鈕值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多態性、Lambda表達式、StreamsAPI、泛型和異常處理。 1.多態性讓不同類的對象可以作為共同基類的對象使用。 2.Lambda表達式使代碼更簡潔,特別適合處理集合和流。 3.StreamsAPI高效處理大數據集,支持聲明式操作。 4.泛型提供類型安全和重用性,編譯時捕獲類型錯誤。 5.異常處理幫助優雅處理錯誤,編寫可靠軟件。

java'stopfeatureSnificallyenhanceItsperformanCandScalability.1)對象 - 方向clincipleslike-polymormormormormormormormormormormormorableableflexibleandscalablecode.2)garbageCollectionAutectionAutoctionAutoctionAutoctionAutoctionAutoctionAutoMenateMememorymanateMmanateMmanateMmanagementButCancausElatemention.3)

JVM的核心組件包括ClassLoader、RuntimeDataArea和ExecutionEngine。 1)ClassLoader負責加載、鏈接和初始化類和接口。 2)RuntimeDataArea包含MethodArea、Heap、Stack、PCRegister和NativeMethodStacks。 3)ExecutionEngine由Interpreter、JITCompiler和GarbageCollector組成,負責bytecode的執行和優化。

Java'ssafetyandsecurityarebolsteredby:1)strongtyping,whichpreventstype-relatederrors;2)automaticmemorymanagementviagarbagecollection,reducingmemory-relatedvulnerabilities;3)sandboxing,isolatingcodefromthesystem;and4)robustexceptionhandling,ensuringgr

Javaoffersseveralkeyfeaturesthatenhancecodingskills:1)對象 - 方向 - 方向上的allowslowsmodelowsmodelingreal-worldentities

thejvmisacrucialcomponentthatrunsjavacodebytranslatingitolachine特定結構,影響性能,安全性和便攜性。 1)theclassloaderloader,links andinitializesClasses.2)theexecutionEngineExecutionEngineExecutionEngineExecuteNexeCuteByteCuteByteCuteByTecuteByteCuteByteCuteBytecuteBytecuteByteCoDeinintolachineinstructionsions.3)Memo.3)Memo


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

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

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

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具