2009-12-16 64 views
3

任何人都可以解釋我如何在JtextField上添加彈出菜單嗎?我設法增加一個JPopupMenu的:如何將彈出菜單添加到JTextField

JPopupMenu popup = new JPopupMenu(); 
    popup.add("m"); 
popup.add("n"); 

JTextField textField = new JTextField(); 
textField.add(popup); 

.....

但是,當我在「彈出式」滾動鼠標,什麼都沒發生(我需要從彈出的項目)。

回答

9

從您的評論,這聽起來像你正試圖顯示在你的JTextField上出現的彈出窗口中的子菜單。

// 1. Let's add the initial popup to the text field. 
JTextField textField = new JTextField(); 
JPopupMenu popup = new JPopupMenu(); 
textField.add(popup); 
textField.setComponentPopupMenu(popup); 

// 2. Let's create a sub-menu that "expands" 
JMenu subMenu = new JMenu("m"); 
subMenu.add("m1"); 
subMenu.add("m2"); 

// 3. Finally, add the sub-menu and item to the popup 
popup.add(subMenu); 
popup.add("n"); 

希望我回答了你試圖問的問題。如果不是,你能否更詳細地解釋你想要完成的事情?

+2

你確定在這裏需要'textField.add(popup);'嗎? – AvrDragon 2015-01-29 15:16:24

0

我不認爲它像問題的代碼看起來那麼簡單。 你可能想看看這個example

+0

不需要我需要展開一個彈出式菜單。在你的鏈接下面的例子中,不是JTextField的彈出菜單。但我正在尋找這個。 – artaxerxe 2009-12-16 07:01:23

相關問題