使用ButtonGroup 將Swing 按鈕和選單項目分組
簡介:
簡介:問題:
一個學校專案需要建立一個繪圖應用程序,包括用於繪製線條、橢圓形和矩形的工具列按鈕和選單項目。面臨的挑戰是確保當使用者選擇工具列按鈕時,相應的選單項目也會被選擇,反之亦然,同時取消選擇所有其他按鈕和選單項目。
解決方案:
使用ButtonGroups:
ButtonGroups 可用於將AbstractButtons 連結在一起,允許您建立一個專屬的按鈕組。雖然 ButtonGroups 確實可以處理多個組,但它們在應用於平行組時有其限制。
使用動作:
另一個方法是使用 Action 介面。操作抽象化了「命令」的概念,並封裝了執行該命令所需的所有操作。將相同的操作指派給工具列按鈕和選單項,您可以共用用於執行命令的相同代碼。
Netbeans GUI 設計器整合:
在Netbeans 中GUI 設計器中,您可以透過導覽至「屬性」窗口,選擇「操作」標籤,然後從「操作」拖曳所需的操作來設定組件的操作。 “操作”調色板。
範例程式碼:
import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JToolBar; public class ShapeSelector extends JFrame { private ButtonGroup toggleGroup = new ButtonGroup(); public ShapeSelector() { super("Shape Selector"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create the Action for shape selection Action shapeSelectAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { // Perform shape selection based on Action command } }; // Create a toolbar with toggle buttons JToolBar toolbar = new JToolBar(); addButton("Line", toolbar, shapeSelectAction); addButton("Oval", toolbar, shapeSelectAction); addButton("Rectangle", toolbar, shapeSelectAction); // Create a menu with menu items JMenuBar menuBar = new JMenuBar(); JMenu shapeMenu = new JMenu("Shape"); menuBar.add(shapeMenu); addMenuItem("Line", shapeMenu, shapeSelectAction); addMenuItem("Oval", shapeMenu, shapeSelectAction); addMenuItem("Rectangle", shapeMenu, shapeSelectAction); // Add the components to the frame JPanel contentPane = new JPanel(); contentPane.add(toolbar); setJMenuBar(menuBar); add(contentPane); pack(); setVisible(true); } private void addButton(String text, JToolBar toolbar, Action action) { JButton button = new JButton(action); button.setText(text); toggleGroup.add(button); toolbar.add(button); } private void addMenuItem(String text, JMenu menu, Action action) { JMenuItem menuItem = new JMenuItem(action); menuItem.setText(text); menu.add(menuItem); } public static void main(String[] args) { new ShapeSelector(); } }
以下程式碼片段示範如何建立一個簡單的Netbeans GUI 應用程序,該應用程式使用操作來連結工具列按鈕和選單項目:
在此程式碼中,自訂操作shapeSelectAction用於工具列按鈕和選單項目,確保選擇一個自動取消選擇其他。以上是如何同步 Swing 按鈕和選單項目以進行獨佔選擇?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java實現“一次編寫,到處運行”通過編譯成字節碼並在Java虛擬機(JVM)上運行。 1)編寫Java代碼並編譯成字節碼。 2)字節碼在任何安裝了JVM的平台上運行。 3)使用Java原生接口(JNI)處理平台特定功能。儘管存在挑戰,如JVM一致性和平台特定庫的使用,但WORA大大提高了開發效率和部署靈活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允許Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,對象與偏見,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的頂級功能包括:1)面向對象編程,支持多態性,提升代碼的靈活性和可維護性;2)異常處理機制,通過try-catch-finally塊提高代碼的魯棒性;3)垃圾回收,簡化內存管理;4)泛型,增強類型安全性;5)ambda表達式和函數式編程,使代碼更簡潔和表達性強;6)豐富的標準庫,提供優化過的數據結構和算法。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

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