Java程序 报错。高手 看下 哪里有问题。
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
简易计算器 包括 加法 减法
*/
public class calculator extends JFrame implements ActionListener
{
public static final int WIDTH=400;
public static final int HEIGHT=200;
public static final int NUMBER_OF_DIGITS=30;
private JTextField ioField;
private double result=0.0 ;
public static void main (String[] args)
{
calculator aCalculator=new calculator();
aCalculator.setVisible(true);
}
public calculator()
{
setTitle("Simplified Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
setSize(WIDTH,HEIGHT);
setLayout(new BorderLayout());
JPanel textPanel=new JPanel();
textPanel.setLayout(new FlowLayout());
ioField=new JTextField("Enter numbers here.",NUMBER_OF_DIGITS);
ioField.setBackground(Color.WHITE);
textPanel.add(ioField);
add(textPanel,BorderLayout.NORTH );
JPanel buttonPanel=new JPanel();
buttonPanel.setBackground(Color.BLUE );
buttonPanel.setLayout(new FlowLayout());
JButton addButton=new JButton("+");
addButton.addActionListener(this);
buttonPanel.add(addButton);
JButton subtractButton=new JButton("-");
subtractButton.addActionListener(this);
buttonPanel.add(subtractButton);
JButton resultButton=new JButton("=");
resultButton.addActionListener(this);
buttonPanel.add(resultButton);
JButton resetButton=new JButton("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
add(buttonPanel,BorderLayout.CENTER );
}
public void actionPerformed(ActionEvent e)
{
try
{
assumingCorrectNumberFormates(e);
}
catch (NumberFormatException e2)
{
ioField.setText("Error:Reenter Number.");
}
}
public void assumingCorrectNumberFormates(ActionEvent e)
{
String actionCommand=e.getActionCommand();
if (actionCommand.equals("+"))
{
result=result+stringToDouble(ioField.getText());
ioField.setText(Double.toString(result));
}
else if(actionCommand.equals("-"))
{
result=result-stringToDouble(ioField.getText());
ioField.setText(Double.toString(result));
}
else if(actionCommand.equals("="))
{
ioField.setText(Double.toHexString(result));
}
else if(actionCommand.equals("Reset"))
{
result=0.0;
ioField.setText("0.0");
}
else
ioField.setText("Unexpected error.");
}
private static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim());
}
}
答案:看一下Test_DataManager.java的53行。是不是有Integer.parseInt(); 这里括号里的值现在是空字符串, 你让他变成有值就可以了。 很明显了,test.
其他:请问,控制台报错的信息在哪呢? import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
简易计算器 包括 加法 减法
*/
public class calculator extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 2681191641845989494L;
public static final int WIDTH=400;
public static final int HEIGHT=200;
public static final int NUMBER_OF_DIGITS=30;
private JTextField ioField;
private double result=0.0 ;
public static void main (String[] args)
{
calculator aCalculator=new calculator();
aCalculator.setVisible(true);
}
public calculator()
{
setTitle("Simplified Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
setSize(WIDTH,HEIGHT);
setLayout(new BorderLayout());
JPanel textPanel=new JPanel();
textPanel.setLayout(new FlowLayout());
ioField=new JTextField("Enter numbers here.",NUMBER_OF_DIGITS);
ioField.setBackground(Color.WHITE);
textPanel.add(ioField);
add(textPanel,BorderLayout.NORTH );
JPanel buttonPanel=new JPanel();
buttonPanel.setBackground(Color.BLUE );
buttonPanel.setLayout(new FlowLayout());
JButton addButton=new JButton("+");
addButton.addActionListener(this);
buttonPanel.add(addButton);
JButton subtractButton=new JButton("-");
subtractButton.addActionListener(this);
buttonPanel.add(subtractButton);
JButton resultButton=new JButton("=");
resultButton.addActionListener(this);
buttonPanel.add(resultButton);
JButton resetButton=new JButton("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
add(buttonPanel,BorderLayout.CENTER );
}
public void actionPerformed(ActionEvent e)
{
try
{
assumingCorrectNumberFormates(e);
}
catch (NumberFormatException e2)
{
ioField.setText("Error:Reenter Number.");
}
}
public void assumingCorrectNumberFormates(ActionEvent e)
{
String actionCommand=e.getActionCommand();
if (actionCommand.equals("+"))
{
result=result+stringToDouble(ioField.getText());
ioField.setText(Double.toString(result));
}
else if(actionCommand.equals("-"))
{
result=result-stringToDouble(ioField.getText());
ioField.setText(Double.toString(result));
}
else if(actionCommand.equals("="))
{
ioField.setText(Double.toHexString(result));
}
else if(actionCommand.equals("Reset"))
{
result=0.0;
ioField.setText("0.0");
}
else
ioField.setText("Unexpected error.");
}
private static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim());
}
}
上一个:sql server 2005 连接java,出现如下错误代码 是配置环境变量问题?是连接sql问题 请问你如何解决的
下一个:java面试时可以查API文档不?