2016-06-14 68 views
0

在我的應用程序中,需要在用戶提供輸入時經常顯示JOptionPane.showOptionDialog。使用自定義面板的JOptionPane.showOptionDialog無法正常工作

有時它不會顯示,並等待用戶輸入並掛起。

在這裏我附上了示例代碼來重現相同。

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridBagLayout; 

import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.JTextPane; 
import javax.swing.ToolTipManager; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DefaultStyledDocument; 
import javax.swing.text.Style; 
import javax.swing.text.StyleConstants; 
import javax.swing.text.StyleContext; 
import javax.swing.text.StyledDocument; 

public class TestJOptionPane { 

public static void main(String[] args) { 

    for (int index = 0; index < 20; index++) { 
     String serverMessage = "Test question message index = "+index; 
     String hintText = "Test question hint index = "+index; 

     JPanel mainPanel = new JPanel(new BorderLayout(0,4)); 
     JPanel answerPanel = new JPanel(new GridBagLayout()); 

     StyleContext context = new StyleContext(); 
     StyledDocument document = new DefaultStyledDocument(context); 

     Style style = context.getStyle(StyleContext.DEFAULT_STYLE); 
     StyleConstants.setFontFamily(style, "Dialog"); 
     StyleConstants.setFontSize(style, 12); 
     StyleConstants.setBold(style, true); 

     try { 
      document.insertString(document.getLength(), serverMessage, style); 
     } catch (BadLocationException badLocationException) { 
      System.out.println(badLocationException.getMessage()); 
     } 

     JTextPane questionTA = new JTextPane(document); 
     questionTA.setEditable(false); 
     questionTA.setOpaque(false); 

     final JTextField answerField = new JTextField(); 
     answerField.setPreferredSize(new Dimension(410,23)); 
     answerField.requestFocus(); 

     JLabel hint = new JLabel(); 

     ToolTipManager.sharedInstance().setDismissDelay(10000); 
     hint.setText("Hint"); 
    // hint.setIcon(Utility.getIcon("HintQuestion.png")); 

     answerPanel.add(answerField); 
     answerPanel.add(new JPanel().add(hint));       
     answerPanel.setPreferredSize(new Dimension(450, 30)); 
     mainPanel.add(questionTA,BorderLayout.NORTH); 
     mainPanel.add(answerPanel,BorderLayout.SOUTH); 

     hint.setToolTipText(hintText); 

     String[] options = {"OK"}; 
     JOptionPane.showOptionDialog(null, mainPanel, "Test showOptionDialog", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 
    } 
} 
} 

的O/P顯示像

「測試的問題的消息索引= 0」,並點擊OK

「測試的問題的消息索引= 1」 並點擊OK

在一個隨機,不顯示JOptionPane.showOptionDialog。

請任何人都可以幫助我!提前致謝。

+1

參見[*初始線程*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)。 – trashgod

回答

2

您需要更改您的代碼,如下所示。

 try { 
      SwingUtilities.invokeAndWait(new Runnable() { 

       @Override 
       public void run() { 
        int result = JOptionPane.showOptionDialog(null, mainPanel, "Test showOptionDialog", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 
       } 
      }); 
     } catch (InvocationTargetException e) { 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
+0

感謝您對貝尼通的迴應。其實我的方法看起來像 公共對象SendResponse(字符串狀態,字符串SERVERMESSAGE,字符串functionName,而字符串hintText)將拋出RemoteException {// 上面的代碼和返回用戶輸入到服務器 } 如果我使用invokeAndWait,它是單獨的線程,然後如何將answerField.getText()返回給我的調用方法 – Tamil

+1

@Lokesh您可以實現ObserverPattern。使文本字段可觀察並讓觀察者在返回userInput時得到通知。 – Beniton

相關問題