2017-08-24 134 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:不客气。如果您不想选择下一个组件,您也可以将操作侦听器的主体留空。 –