(来自喝水吃饭大学的菜鸡,看看有没有校友)
最近写的java作业,感觉还是有所收获的,写个这个程序崩溃了好几次(好菜呀),最后还是找的了bug(一行一行代码审计,因为不是语法错误)
package pack1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1L;
JPanel contentPane,buttonPane;
JTextField display;
JButton bt1=new JButton("=");
MyFrame(){
this.setSize(300,200);
this.setLocationRelativeTo(null);
this.setTitle("计算器");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=(JPanel) this.getContentPane();
display=new JTextField();//单行编辑文本框
display.setPreferredSize(new Dimension(this.getWidth(),35));
display.setEditable(false);
/*为display添加线型边框*/
display.setBorder(new LineBorder(Color.magenta,2));
contentPane.add(display,BorderLayout.NORTH);
buttonPane=new JPanel();
GridLayout layout=new GridLayout(0,5,3,3);
buttonPane.setLayout(layout);
/*为buttonPane添加空边框,相当于设置该组件上下左右的边距*/
buttonPane.setBorder(new EmptyBorder(10,10,10,10));
buttonPane.setBackground(Color.cyan);
contentPane.add(buttonPane,BorderLayout.CENTER);
addButton();
this.setVisible(true);
}
public void addButton(){
String []s={"7","8","9","CE","AC",
"4","5","6","+","-",
"1","2","3","*","/",
"0",".","+/-","M+","="};
for(int i=0;i<s.length-1;i++){
JButton bt=new JButton(s[i]);
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str=bt.getText();
display.setText(display.getText()+str);
}
});
buttonPane.add(bt);
}
bt1.addActionListener(new Mylistener(this));
buttonPane.add(bt1);
}
public static void main(String[] args) {
new MyFrame();
}
}
class Mylistener implements ActionListener{
MyFrame mf;
Mylistener(MyFrame mf){
this.mf=mf;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
mf.display.setText(mf.display.getText()+mf.bt1.getText());
//做加运算
if(mf.display.getText().contains("+")) {
StringTokenizer st1=new StringTokenizer(mf.display.getText(),"+");
double d1=0,d2=0;
d1=Double.parseDouble(st1.nextToken());
while(st1.hasMoreTokens()) {
StringTokenizer st2=new StringTokenizer(st1.nextToken(),"=");
d2=Double.parseDouble(st2.nextToken());
}
mf.display.setText(mf.display.getText()+""+(d1+d2));
}
else if(mf.display.getText().contains("-")) {
StringTokenizer st1=new StringTokenizer(mf.display.getText(),"-");
double d1=0,d2=0;
d1=Double.parseDouble(st1.nextToken());
while(st1.hasMoreTokens()) {
StringTokenizer st2=new StringTokenizer(st1.nextToken(),"=");
d2=Double.parseDouble(st2.nextToken());
}
mf.display.setText(mf.display.getText()+""+(d1-d2));
}
else if(mf.display.getText().contains("*")) {
StringTokenizer st1=new StringTokenizer(mf.display.getText(),"*");
double d1=0,d2=0;
d1=Double.parseDouble(st1.nextToken());
while(st1.hasMoreTokens()) {
StringTokenizer st2=new StringTokenizer(st1.nextToken(),"=");
d2=Double.parseDouble(st2.nextToken());
}
mf.display.setText(mf.display.getText()+""+(d1*d2));
}
else if(mf.display.getText().contains("/")) {
StringTokenizer st1=new StringTokenizer(mf.display.getText(),"/");
double d1=0,d2=0;
d1=Double.parseDouble(st1.nextToken());
while(st1.hasMoreTokens()) {
StringTokenizer st2=new StringTokenizer(st1.nextToken(),"=");
d2=Double.parseDouble(st2.nextToken());
}
mf.display.setText(mf.display.getText()+""+(d1/d2));
}
}
}
在用自定义监听类的时候,写成了这样
应该调用java.long包中Double类中的parseDouble方法
d1=Double.parseDouble(string);
而不应该用(double)string这种类型转换
我们知道在java中 默认的分隔符是空格("")、制表符(\t)、换行符(\n)、回车符(\r)。
如果自己把一个string分成几部分需要引用java.util.StringTokenizer
使用方法参考菜鸟教程