事件监视器问题?
package 计算器;public class jisuanqi {
public static void main(String[] args) {
// TODO 自动生成的方法存根
WindowAction win=new WindowAction();
win.setBounds(100,100,390,360);
win.setTitle("计算器");
}
}
**********************************************
package 计算器;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class WindowAction extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField inputNumberOne, inputNumberTwo;
JTextArea outNumberThree;
JButton[] button;
ComputerLiatener computer;
public WindowAction(){
init();
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init(){
setLayout(new FlowLayout());
inputNumberOne=new JTextField(5);
inputNumberTwo=new JTextField(5);
outNumberThree=new JTextArea(9,10);
button=new JButton[4];
button[0]=new JButton("+");
button[1]=new JButton("-");
button[2]=new JButton("*");
button[3]=new JButton("/");
String[] a={"+","-","*","/"};
for(int i=0;i<a.length;i++){
button[i].setText(a[0]);
}
computer.setJTextFieldOne(inputNumberOne);//请教这里有什么错误??
computer.setJTextFieldTwo(inputNumberTwo);//请教这里有什么错误??
computer.setJTextArea(outNumberThree);//请教这里有什么错误??
computer.setJButton(button);//请教这里有什么错误??
button[0].addActionListener(computer);
button[1].addActionListener(computer);
button[2].addActionListener(computer);
button[3].addActionListener(computer);
add(inputNumberOne);
add(inputNumberTwo);
add(button[0]);
add(button[1]);
add(button[2]);
add(button[3]);
add(outNumberThree);
}
}
*****************************************************************************************
package 计算器;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ComputerLiatener implements ActionListener{
JTextField inputNumberOne,inputNumberTwo;
JTextArea outNumberThree;
String but;
public void setJTextFieldOne(JTextField t) {
// TODO 自动生成的方法存根
inputNumberOne=t;
}
public void setJTextFieldTwo(JTextField t) {
// TODO 自动生成的方法存根
inputNumberTwo=t;
}
public void setJTextArea(JTextArea t) {
// TODO 自动生成的方法存根
outNumberThree=t;
}
public void setJButton(JButton[] button){
but=button.toString();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
try{
double number1=Double.parseDouble(inputNumberOne.getText());
double number2=Double.parseDouble(inputNumberTwo.getText());
double Result=0;
if(but.equals("+")){
Result=number1+number2;
}
else if(but.equals("=")){
Result=number1-number2;
}
else if(but.equals("*")){
Result=number1*number2;
}
else if(but.equals("/")){
Result=number1/number2;
}
outNumberThree.append(number1+" "+but+" "+number2+"="+Result+"\n");
}catch(Exception exception){
outNumberThree.append("输入出错!!");
}
}
}
*****************************************************
Exception in thread "main" java.lang.NullPointerException
at 计算器.WindowAction.init(WindowAction.java:41)
at 计算器.WindowAction.<init>(WindowAction.java:22)
at 计算器.jisuanqi.main(jisuanqi.java:8)