info1=JOptionPane.showInputDialog(null,msg,text);当我点击取消时,返回值为空,出现了以下错误,请问这是为什么啊。--------------------Configuration:----------------...
info1 = JOptionPane.showInputDialog(null,msg,text);当我点击取消时,返回值为空,出现了以下错误,请问这是为什么啊。
--------------------Configuration: --------------------
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Process completed.
全部程序代码如下
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class mdialog extends JFrame implements ActionListener
{JLabel jlb = new JLabel("计算 1+2+3+......+n的和");
String ss ;
String sb;
String nn;
String info1="",info2="";
mdialog()
{super("演示输入对话框");
setSize(150,150);
setVisible(true);
Container con = getContentPane();
con.setLayout(new BorderLayout());
JButton jbtn = new JButton("从对话框输入数n");
con.add(jbtn,BorderLayout.NORTH);
con.add(jlb,BorderLayout.CENTER);
jbtn.addActionListener(this);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{String msg = " 请输入n的值:";
String text="100";
int s =0;
info1 = JOptionPane.showInputDialog(null,msg,text);
int n = Integer.parseInt(info1);
for(int i=1;i<=n;i++)
{
{s = s+i;}
info2 = String.valueOf(s);
if(info1==null )
{jlb.setText("计算 1+2+3+......+n的和");
}
else
{
if (n ==0)
{jlb.setText("0");}
else
{if (n<5)
{ for(int j=1;j<=n;j++)
{
sb = sb + String.valueOf(j )+ "+";
}
jlb.setText( ss+"....+"+ info1+"="+info2);
}
else
{ jlb.setText( "1+2+3....+"+ info1+"="+info2);}
}
}
}
}
}
public class Example5_15 {
public static void main(String[] args) {
new mdialog();
}
}
展开