日期:2014-05-20  浏览次数:20668 次

为什么直接给文本框赋值就能有结果
package Second;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class Test {
JFrame jf;
JLabel lb1;
JLabel lb2;
JLabel lb3;
JLabel lb4;
JTextArea txt1;
JTextArea txt2;
JTextField txt3;
JButton btn1;
JButton btn2;
public Test(){
jf=new JFrame("商场收银系统");
lb1=new JLabel("单价:");
lb2=new JLabel("数量:");
lb3=new JLabel("总计");
lb4=new JLabel("0.00");
txt1=new JTextArea(4,4);
txt2=new JTextArea(4,4);
txt3=new JTextField(50);
btn1=new JButton("确定");
  btn2=new JButton("重置");
  jf.add(lb1);
  jf.add(lb2);
  jf.add(lb3);
  jf.add(lb4);
  jf.add(txt1);
  jf.add(txt2);
  jf.add(txt3);
  jf.add(btn1);
  jf.add(btn2);
  actionListener1 a=new actionListener1();
  actionListener2 b=new actionListener2();
  btn1.addActionListener(a);
  btn2.addActionListener(b);
   
  jf.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
jf.setSize(300,300);
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);
  }}); 
}
 class actionListener1 implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double TotalPrices=0;
double price=Double.parseDouble(txt1.getText());
double num=Double.parseDouble(txt2.getText());
//String A=txt1.getText();
//String B=txt2.getText();
//double C=Double.parseDouble(A);
//double D=Double.parseDouble(B);
TotalPrices=price*num;
double Total=0; 
Total=Total+TotalPrices;
//String E=String.valueOf(Total);
txt3.setText("单价"+txt1.getText()+"数量"+txt2.getText()+"合计:"+String.valueOf(Total));
lb4.setText(String.valueOf(Total));
}
 
 }
 class actionListener2 implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
txt1.setText("");
txt2.setText("");

txt3.setText("");
}
 
 }

}
package Second;

public class Drive {
public static void main(String args[]){
Test t=new Test();
}
}
为什么直接给文本框赋值就能有结果
自己输入值就由异常啊?

------解决方案--------------------
没问题
为什么两个输入要用TestArea呢?还弄那么大 
别有空格,回车 就输入数字还是没问题的