2017-08-24 130 views
1

使用JOptionPane.showConfirmDialog多輸入:JOptionPane.showConfirmDialog多輸入月初返回時,返回鍵擊

int result = JOptionPane.showConfirmDialog(null, panel, 
      prompt, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 

面板是建立在以下方式:

JTextField percentField = new JTextField(5); 

JComboBox cb = new JComboBox(movingAveragesList);    
JPanel myPanel = new JPanel(); 
myPanel.add(new JLabel("Enter %:")); 
myPanel.add(percentField); 
myPanel.add(Box.createHorizontalStrut(5)); // a spacer 
myPanel.add(new JLabel("Select MA:")); 
myPanel.add(cb); 

當用戶進入%字段和命中返回,代碼返回沒有完成組合框選擇。返回鍵==單擊確定按鈕。無論如何要解決這個問題,所以確定按鈕需要打回來之前?

回答

1

兩個選項:

有三個選項:

(可能更多)

  • 不要使用的JOptionPane這將對您的JTextField中輸入默認按來電代碼「接受」JOptionPane,關閉它。相反,請創建您自己的模態JDialog,並忽略在JTextField中輸入按鈕或忽略下一個組件的選項卡。
  • 您可以重新分配您的JTextField中的Enter鍵的鍵綁定,以便不執行任何操作,或者切換到下一個組件。這是一個小tricker但更有趣的我,所以我決定嘗試一下,結果如下圖所示:

public static void main(String[] args) { 
    SwingUtilities.invokeLater(() -> { 
     JTextField percentField = new JTextField(5); 

     // get the JTextField's action and input map 
     ActionMap actionMap = percentField.getActionMap(); 
     int condition = JComponent.WHEN_FOCUSED; // only interested in the input where we have focus 
     InputMap inputMap = percentField.getInputMap(condition); 

     // get the key for the enter key press in the input map 
     Object enterKey = inputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); 

     // now re-assign its entry in the action map to tab to next component 
     actionMap.put(enterKey, new AbstractAction() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); 
       manager.focusNextComponent(); 
      } 
     }); 

     String[] myData = { "One", "Two", "Three", "Four" }; 
     JComboBox cb = new JComboBox(myData); 
     JPanel myPanel = new JPanel(); 
     myPanel.add(new JLabel("Enter %:")); 
     myPanel.add(percentField); 
     myPanel.add(Box.createHorizontalStrut(5)); // a spacer 
     myPanel.add(new JLabel("Select MA:")); 
     myPanel.add(cb); 

     String prompt = "Please Select"; 
     int result = JOptionPane.showConfirmDialog(null, myPanel, prompt, 
       JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 
    }); 
} 

第三位簡單的選擇:

  • 簡單地給你的JTextField一個ActionListener選項卡到下一個組件。由於JTextField的ActionListener只要具有焦點並按下Enter鍵就會觸發,這將導致您按下Enter時所需的操作。

public static void main(String[] args) { 
    SwingUtilities.invokeLater(() -> { 
     JTextField percentField = new JTextField(5); 
     percentField.addActionListener(e -> { 
      KeyboardFocusManager manager = KeyboardFocusManager 
        .getCurrentKeyboardFocusManager(); 
      manager.focusNextComponent(); 
     }); 

     String[] myData = { "One", "Two", "Three", "Four" }; 
     JComboBox cb = new JComboBox(myData); 
     JPanel myPanel = new JPanel(); 
     myPanel.add(new JLabel("Enter %:")); 
     myPanel.add(percentField); 
     myPanel.add(Box.createHorizontalStrut(5)); // a spacer 
     myPanel.add(new JLabel("Select MA:")); 
     myPanel.add(cb); 

     String prompt = "Please Select"; 
     int result = JOptionPane.showConfirmDialog(null, myPanel, prompt, 
       JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 
    }); 
} 
+0

我喜歡第三個選項,會試試看。謝謝! – wsteve

+0

@wsteve:不客氣。如果您不想選擇下一個組件,您也可以將操作偵聽器的主體留空。 –