这是一个计算机程序 (未完成) (具体程序在下边)
bug 在计算后关闭程序会出现下列异常
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: component argument pData
at sun.awt.windows.Win32SurfaceData.initOps(Native Method)
at sun.awt.windows.Win32SurfaceData.<init>(Unknown Source)
at sun.awt.windows.Win32SurfaceData.createData(Unknown Source)
at sun.awt.Win32GraphicsConfig.createSurfaceData(Unknown Source)
at sun.awt.windows.WComponentPeer.replaceSurfaceData(Unknown Source)
at sun.awt.windows.WComponentPeer.replaceSurfaceData(Unknown Source)
at sun.awt.windows.WComponentPeer$2.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;public class Counter extends JApplet implements ActionListener {
private Container container;
private JPanel upPanel;
private JPanel midPanel;
private JPanel[] panel;
private JTextField textField;
private JButton[] button;
private int[] hash;
private double sum = 0.0;
private final static int rowPanel = 5;
private final static int colPanel = 1;
private final static int maxSize = 10000;
public void init () {
container = this.getContentPane();
hashInit();
addTextField();
addPanel();
this.setSize(350, 300);
this.setVisible(true);
this.setLocation(500, 300);
}
public void destroy() {
//this.notifyAll();
System.out.println ("destroy");
}
public void hashInit() {
hash = new int[maxSize];
for (int i = 0; i < 10000; i++) {
hash[i] = -1;
}
for (int i = 0; i < 10; i++) {
hash[i] = i;
}
}
public void addTextField() {
textField = new JTextField(25);
upPanel = new JPanel();
upPanel.add(textField);
container.add(upPanel, BorderLayout.NORTH); }
public void addPanel() {
int row, col, cnt;
String name[] = {new String (" Backspace "), new String (" CE "), new String (" C "),
new String ("MC"), new String ("7"), new String ("8"), new String ("9"),
new String ("/"), new String ("sqrt"), new String ("MR"), new String ("4"),
new String ("5"), new String ("6"), new String ("*"), new String (" % "),
new String ("MS"), new String ("1"), new String ("2"), new String ("3"),
new String ("-"), new String (" 1/x "), new String ("M+"), new String ("0"),
new String ("+/-"), new String ("."), new String ("+"), new String (" = ")};
midPanel = new JPanel();
midPanel.setLayout(new GridLayout(rowPanel, colPanel));
container.add(midPanel, BorderLayout.CENTER);
panel = new JPanel[5];
button = new JButton[31];
try {
for (row = cnt = 0; row < 5; row++) {
panel[row] = new JPanel();
midPanel.add(panel[row]);
for (col = 0; col < 6; col++) {
if (row == 0 && col >= 3){
// label[col - 3] = new JLabel(" ");
//panel[row].add(label[col - 3]);
break;
} else {
button[cnt] = new JButton(name[cnt]);
button[cnt].addActionListener(this);
button[cnt].setSize(10,10);
panel[row].add(button[cnt++]);
}
}
}
} catch (Exception e) {
System.out.println (e.getMessage());
}
}
public double answer (double sum, char opera, String a) {
switch (opera) {
case '*':
sum *= Double.valueOf(a);
break;
case '/':
sum /= Double.valueOf(a);
break;
case '-':
sum -= Double.valueOf(a);
break;
case '+':
sum += Double.valueOf(a);
default:
sum = Double.valueOf(a);
}
return sum;
}
public void actionPerformed (ActionEvent e) {
String name = new String (((JButton)e.getSource()).getText());
if (name.length() > 3 && name.charAt(2) == '=') {
sum = 0.0;
String equation = new String (textField.getText());
char opera = '0';
String tmp = new String();
tmp = null;
for (int i = 0; i < equation.length(); i++) {
if (equation.charAt(i) >= '0' && equation.charAt(i) <= '9' ||
equation.charAt(i) == '.') {
if (tmp == null) {
tmp = String.valueOf(equation.charAt(i));
} else {
tmp += equation.charAt(i);
}
} else {
System.out.println (sum + opera + tmp);
sum = answer (sum, opera, tmp);
System.out.println (sum);
opera = equation.charAt(i);
tmp = null;
}
}
System.out.println (sum + opera + tmp);
sum = answer (sum, opera, tmp);
System.out.println (sum);
if (Math.abs((int)sum - sum) > 0)
textField.setText(Double.toString(sum));
else
textField.setText(Integer.toString((int)sum));
//System.exit(0);
} else if (name.charAt(0) - '0' < 0) {
textField.setText(textField.getText() + name);
} else if (hash[name.charAt(0) - '0'] >= 0 && hash[name.charAt(0) - '0'] <= 9) {
if (sum != 0.0) {
sum = 0.0;
textField.setText(String.valueOf(hash[name.charAt(0) - '0']));
} else {
textField.setText(textField.getText() + hash[name.charAt(0) - '0']);
}
}
}
}
bug 在计算后关闭程序会出现下列异常
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: component argument pData
at sun.awt.windows.Win32SurfaceData.initOps(Native Method)
at sun.awt.windows.Win32SurfaceData.<init>(Unknown Source)
at sun.awt.windows.Win32SurfaceData.createData(Unknown Source)
at sun.awt.Win32GraphicsConfig.createSurfaceData(Unknown Source)
at sun.awt.windows.WComponentPeer.replaceSurfaceData(Unknown Source)
at sun.awt.windows.WComponentPeer.replaceSurfaceData(Unknown Source)
at sun.awt.windows.WComponentPeer$2.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;public class Counter extends JApplet implements ActionListener {
private Container container;
private JPanel upPanel;
private JPanel midPanel;
private JPanel[] panel;
private JTextField textField;
private JButton[] button;
private int[] hash;
private double sum = 0.0;
private final static int rowPanel = 5;
private final static int colPanel = 1;
private final static int maxSize = 10000;
public void init () {
container = this.getContentPane();
hashInit();
addTextField();
addPanel();
this.setSize(350, 300);
this.setVisible(true);
this.setLocation(500, 300);
}
public void destroy() {
//this.notifyAll();
System.out.println ("destroy");
}
public void hashInit() {
hash = new int[maxSize];
for (int i = 0; i < 10000; i++) {
hash[i] = -1;
}
for (int i = 0; i < 10; i++) {
hash[i] = i;
}
}
public void addTextField() {
textField = new JTextField(25);
upPanel = new JPanel();
upPanel.add(textField);
container.add(upPanel, BorderLayout.NORTH); }
public void addPanel() {
int row, col, cnt;
String name[] = {new String (" Backspace "), new String (" CE "), new String (" C "),
new String ("MC"), new String ("7"), new String ("8"), new String ("9"),
new String ("/"), new String ("sqrt"), new String ("MR"), new String ("4"),
new String ("5"), new String ("6"), new String ("*"), new String (" % "),
new String ("MS"), new String ("1"), new String ("2"), new String ("3"),
new String ("-"), new String (" 1/x "), new String ("M+"), new String ("0"),
new String ("+/-"), new String ("."), new String ("+"), new String (" = ")};
midPanel = new JPanel();
midPanel.setLayout(new GridLayout(rowPanel, colPanel));
container.add(midPanel, BorderLayout.CENTER);
panel = new JPanel[5];
button = new JButton[31];
try {
for (row = cnt = 0; row < 5; row++) {
panel[row] = new JPanel();
midPanel.add(panel[row]);
for (col = 0; col < 6; col++) {
if (row == 0 && col >= 3){
// label[col - 3] = new JLabel(" ");
//panel[row].add(label[col - 3]);
break;
} else {
button[cnt] = new JButton(name[cnt]);
button[cnt].addActionListener(this);
button[cnt].setSize(10,10);
panel[row].add(button[cnt++]);
}
}
}
} catch (Exception e) {
System.out.println (e.getMessage());
}
}
public double answer (double sum, char opera, String a) {
switch (opera) {
case '*':
sum *= Double.valueOf(a);
break;
case '/':
sum /= Double.valueOf(a);
break;
case '-':
sum -= Double.valueOf(a);
break;
case '+':
sum += Double.valueOf(a);
default:
sum = Double.valueOf(a);
}
return sum;
}
public void actionPerformed (ActionEvent e) {
String name = new String (((JButton)e.getSource()).getText());
if (name.length() > 3 && name.charAt(2) == '=') {
sum = 0.0;
String equation = new String (textField.getText());
char opera = '0';
String tmp = new String();
tmp = null;
for (int i = 0; i < equation.length(); i++) {
if (equation.charAt(i) >= '0' && equation.charAt(i) <= '9' ||
equation.charAt(i) == '.') {
if (tmp == null) {
tmp = String.valueOf(equation.charAt(i));
} else {
tmp += equation.charAt(i);
}
} else {
System.out.println (sum + opera + tmp);
sum = answer (sum, opera, tmp);
System.out.println (sum);
opera = equation.charAt(i);
tmp = null;
}
}
System.out.println (sum + opera + tmp);
sum = answer (sum, opera, tmp);
System.out.println (sum);
if (Math.abs((int)sum - sum) > 0)
textField.setText(Double.toString(sum));
else
textField.setText(Integer.toString((int)sum));
//System.exit(0);
} else if (name.charAt(0) - '0' < 0) {
textField.setText(textField.getText() + name);
} else if (hash[name.charAt(0) - '0'] >= 0 && hash[name.charAt(0) - '0'] <= 9) {
if (sum != 0.0) {
sum = 0.0;
textField.setText(String.valueOf(hash[name.charAt(0) - '0']));
} else {
textField.setText(textField.getText() + hash[name.charAt(0) - '0']);
}
}
}
}
解决方案 »
- JcomboBox事件监听建立
- [求助]如何比较高效的访问远程数据库?
- 使用scanner输入内容后如何终止输入?
- JPanel不能显示在JFrame中..请问是怎么加事
- ftp上传,一个ip成功,另一个ip失败?只有40分了,不够以后可以加
- java文件传输
- java连接mysql数据库,ClassNotFoundException????
- 怎样实现在文本框中按下回车,响应jbutton的actionperformed事件,也就是说和jbutton单击事件一样
- C++ --> Java ????
- 请问怎么给分呢?
- RMB100元求助 急急急!!!
- 紧急求问:我编的web应用无法在老板机器上运行。。。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: component argument pData