2015-10-20 43 views
0

JOptionPane如何從點擊的JOptionPane選項獲取輸入。 - java的

我怎樣才能得到被點擊了JOptionPane從選項中輸入,而不是Okay按鈕,但我已經提供了諸如Emerald的選項,Autumn

這裏是我的代碼

public static ArrayList<String> Skins = new ArrayList<String>(); 

public static void skinJPanel() { 
    try { 
     String[] skins = {"Emerald", "Autumn", "Black Steel", "Blue Steel", "Creme Coffee", "Green Magic"}; 
     for (int i = 0; i <= 5; i++) { 
      Skins.add(skins[i]); 
     } 
     JPanel panel = new JPanel(); 
     panel.setSize(200, 200); 
     JList list = new JList(Skins.toArray()); 
     JScrollPane scrollpane = new JScrollPane(); 
     scrollpane = new JScrollPane(list); 
     panel.add(scrollpane); 
     scrollpane.getViewport().add(list); 
     String string = JOptionPane.showInputDialog(null, scrollpane, "Aftermath Skin Chooser", JOptionPane.PLAIN_MESSAGE); 
     System.out.println(string); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    Skins.clear(); 
} 
+0

所以,我假設你不想等待用戶點擊[好]/[取消],或者你只是想從'JList'中選擇的值? – MadProgrammer

回答

0

要獲得選定的值在用戶按下[好]之後,您應該做更多的事情......

int result = JOptionPane.showConfirmDialog(null, scrollpane, "Aftermath Skin Chooser", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 
if (result == JOptionPane.OK_OPTION) { 
    String value = (String) list.getSelectedValue(); 
    System.out.println(value); 
} 
+0

工作絕對完美,謝謝你花時間現在我知道該怎麼辦了,當再次面對這種情況:) –

+0

很高興它可以幫助;)ps-像外觀和感覺;) – MadProgrammer

相關問題