两个数在按下按钮时相加,用消息框显示和数
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Caculator extends JFrame implements ActionListener{
/**
* @param args
*/
public Caculator(){
super("计算");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField num0=new JTextField(5);
JTextField num1=new JTextField(5);
JButton button=new JButton("计算");
JPanel p=new JPanel();
//num0.setEditable(true);
//num1.setEditable(true);
button.addActionListener(this);
p.add(num0);
p.add(num1);
p.add(button);
setContentPane(p);
pack();
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Caculator cl=new Caculator();
}
public void actionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
int sum=Integer.parseInt(num0.getText())+Integer.parseInt(num1.getText()); //这里总是提示
num0,num1 can not be solved,为什么啊?
JOptionPane.showMessageDialog(null,sum+"");
}
}