2017-04-27 93 views
0

我正在開發一個程序,該程序在啓動時顯示以下menumenu_image)。我有一個小問題:我想將其展示在其他窗口的頂部,但我無法實現此目的。在其他窗口頂部顯示JOptionPane

class Menu { 
    public String showMenu(){ 
     Object[] options = {"option1", "option2", "option3"}; 
     Object selectionObject = JOptionPane.showInputDialog(null, "Choose", "Menu", JOptionPane.PLAIN_MESSAGE, null, options, options[0]); 
     String selectionString = selectionObject.toString(); 
     return selectionString; 
    } 
} 

有人可以幫我嗎?謝謝你在前進

+0

通過你的主窗口作爲第一個參數,而不是傳遞'null'。 – Berger

+0

[如何在所有窗口的頂部顯示JOptionPane]的可能重複(http://stackoverflow.com/questions/10880981/how-to-show-joptionpane-on-the-top-of-all-windows) – Berger

+1

好的,我做到了。感謝您的幫助! –

回答

0

基於伯傑的建議下,我解決我的問題通過以下方式...

class Menu { 
    public String showMenu(){ 
     //i solved my problem adding the following 2 lines of code... 
     JFrame frame = new JFrame(); 
     frame.setAlwaysOnTop(true); 

     Object[] options = {"option1", "option2", "option3"}; 
     //...and passing `frame` instead of `null` as first parameter 
     Object selectionObject = JOptionPane.showInputDialog(frame, "Choose", "Menu", JOptionPane.PLAIN_MESSAGE, null, options, options[0]); 
     String selectionString = selectionObject.toString(); 
     return selectionString; 
    } 
}