如何同步 Swing 按鈕和選單項目以進行獨佔選擇?
使用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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

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

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

Dreamweaver CS6
視覺化網頁開發工具

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

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本啟動Spring...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...
