JOptionPane的显示消息对话框无法在焦点监听器中关闭。

huangapple go评论89阅读模式
英文:

JOptionPane show message dialog can not be closed in Focus Listener

问题

根据之前问题回复中的指示,我已经为我的文本字段添加了焦点监听器。现在在按下 Tab 键时可以选择文本字段,所以没问题。在 NumberFormattedException 异常处理程序中,我使用了 JOPtionPaneShowMessageDialog 来通知用户不应该将文本字段留空。但是现在当文本字段为空时,消息通常会出现,但无法关闭。关闭后会立即重新打开。如何修复这个问题?请帮忙。

   public void focusLost(FocusEvent ae) {
}
   public void focusGained(FocusEvent ae) {
       Object source =ae.getSource();
           if(source instanceof JTextField) {
               gi.selectAll();
               hra.selectAll();
               ss.selectAll();
               pt.selectAll();

'.....执行代码....... '

}else if (source instanceof JButton) ;
    String w= (String) jcb.getSelectedItem();
try{
    String s1=gi.getText();
        int a=Integer.parseInt(s1);
            String s2=ss.getText();
                int b=Integer.parseInt(s2);
                    String s3=pt.getText();
                        int f=Integer.parseInt(s3);
                            String s4=sd.getText();
                                int h=Integer.parseInt(s4);
'....执行代码....'

}catch(NumberFormatException e) {
    JOptionPane.showMessageDialog(jfrm, "No text field should not be left blank . 
        If no amount in a field put  0 (Zero) .", "WARNING ! ",1);
    }
}
public static void main (String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new IncomeTax();
        }
    });
}
}

'''


'''另外我想在用户单击帮助JMenuItem时显示 JOptionpaneShowMessageDialog我对此的代码如下但不起作用请提供相应的建议

'''


}else if (source instanceof JMenuItem) {
     if(ae.getSource()== jmiHelp)
         JOptionPane.showMessageDialog(null,"USER MANUAL\n"+"1.Select Age 
             Group.\n"+"2.Enter Gross Income(Salary/Pension)+Surrender Leave 
             salary+ interest income etc.)\n"+"3.Enter deductions admissible to 
             you.\n"+"4.Look tool tip texts for more details.\n"+"5.Click New 
             Scheme or Old Scheme for the respective tax calculation.\n"+"6.No 
             text fields should not be left blank.If no amount put zero.\n");

英文:

As instructed in the reply to my earlier question I have added focus listener to my text fields.Now the text fields are selected when the tab key is pressed so it is ok. I have a JOPtionPaneShowMessageDialog in the NumberFormattedException handler to inform the user that the text fields should not be left blank. But now when the text field is blank the message appeared as usual, but it can not be closed.When it closed reopened at the same time. How can it rectified.Please help.

   public void focusLost(FocusEvent ae) {
}
public void focusGained(FocusEvent ae) {
Object source =ae.getSource();
if(source instanceof JTextField) {
gi.selectAll();
hra.selectAll();
ss.selectAll();
pt.selectAll();
'.....code to execute....... '
}else if (source instanceof JButton) ;
String w= (String) jcb.getSelectedItem();
try{
String s1=gi.getText();
int a=Integer.parseInt(s1);
String s2=ss.getText();
int b=Integer.parseInt(s2);
String s3=pt.getText();
int f=Integer.parseInt(s3);
String s4=sd.getText();
int h=Integer.parseInt(s4);
'....code to execute....'
}catch(NumberFormatException e) {
JOptionPane.showMessageDialog(jfrm, "No text field should not be left blank . 
If no amount in a field put  0 (Zero) .", "WARNING ! ",1);
}
}
public static void main (String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new IncomeTax();
}
});
}
}
'''
'Also I want to show JOptionpaneShowMessageDialog when the user click on the "Help" JMenuItem.My code for the same is as follows, but it is not working. Please give suggestion for the same also.
'''
}else if (source instanceof JMenuItem) {
if(ae.getSource()== jmiHelp)
JOptionPane.showMessageDialog(null,"USER MANUAL\n"+"1.Select Age 
Group.\n"+"2.Enter Gross Income(Salary/Pension)+Surrender Leave 
salary+ interest income etc.)\n"+"3.Enter deductions admissible to 
you.\n"+"4.Look tool tip texts for more details.\n"+"5.Click New 
Scheme or Old Scheme for the respective tax calculation.\n"+"6.No 
text fields should not be left blank.If no amount put zero.\n");

答案1

得分: 0

问题已经由我自行解决。不再使用FocusListener ActionListener,而是使用ActionListener。仅针对第一实例,不再使用Tab键,而是使用Enter键或鼠标点击来选择文本。现在OptionPaneMessage没有问题。

英文:

The problem has resolved by myself. Instead of FocusListener ActionListener is used. Instead of tab key, enter key or mouse click for selecting text for first instance only. No problem for OptionPaneMessage's now.

huangapple
  • 本文由 发表于 2020年9月8日 20:12:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63793673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定