java - JOptionPane 显示消息对话框无法在焦点侦听器中关闭
问题描述
按照对我之前问题的回复中的说明,我已将焦点侦听器添加到我的文本字段中。现在,当按下 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();
'.....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");
解决方案
问题已由我自己解决。使用 ActionListener 而不是 FocusListener。代替 tab 键,输入键或鼠标单击仅用于第一个实例选择文本。现在 OptionPaneMessage 没问题。
推荐阅读
- c# - C# WSDL soap API - 仅在生产服务器上出现即时错误 401?视窗服务器 2008
- linux - 为什么最新版本的 sys-stat 在杀死后不显示平均值..?
- python - 用 x 轴上的索引值和 y 轴上每列值的频率绘制直方图
- python - 最后一个日历周的纪元时间
- python - dtype('
我对python有点陌生,今天我试图在金融DataFrame(YYYY-MM-DD的第一列和值的第二列)中进行一些操作。当我尝试应用一个简单的 pct_change() 操作时,它给了我这个错误:
TypeError: ufunc true_divide cannot use operands with types dtype('<M8[ns]') and dty
- java - 什么是引导方法参数 - Java 字节码
- moodle - Maxima 是否具有求解线性不等式系统的功能,在 Moodle Stack-exams 上也可以使用?
- continuous-integration - 无法在 TFS 2017 中创建用户组以进行发布批准
- javascript - 在没有 eval 和 jQuery 的情况下计算用户输入
- .net-core - 在 ASP.net 核心 IHostedService 中使用 RabbitMQ