package grid;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calc extends JFrame implements ActionListener {
/**
*
*/
private JTextField txtF1, txtF2, txtF3;
private JButton btn1, btn2, btn3, btn4, btn5, btn6;
public Calc() {
// TODO Auto-generated constructor stub
super("Grid Layout Calculator");
Container c = getContentPane();
c.setLayout(new GridLayout(3, 3));
setContentPane(c);
c.setLayout(null);
JPanel panel1 = new JPanel(), panel2 = new JPanel(), panel3 = new JPanel(), panel4 = new JPanel(), panel5 = new JPanel(), panel6 = new JPanel(), panel7 = new JPanel(), panel8 = new JPanel(), panel9 = new JPanel();
btn1 = new JButton("Add");
btn1.addActionListener(this);
btn2 = new JButton("Subtract");
btn2.addActionListener(this);
btn3 = new JButton("Multiply");
btn3.addActionListener(this);
btn4 = new JButton("Divide");
btn4.addActionListener(this);
btn5 = new JButton("Modulus");
btn5.addActionListener(this);
btn6 = new JButton("=");
btn6.addActionListener(this);
panel1.setLayout(new GridLayout(1, 1));
panel1.add(txtF1);
panel2.setLayout(new GridLayout(1, 2));
panel2.add(txtF2);
panel3.setLayout(new GridLayout(1, 3));
panel3.add(txtF3);
panel4.setLayout(new GridLayout(2, 1));
panel4.add(btn1);
panel5.setLayout(new GridLayout(2, 2));
panel5.add(btn2);
panel6.setLayout(new GridLayout(2, 3));
panel6.add(btn3);
panel7.setLayout(new GridLayout(3, 1));
panel7.add(btn4);
panel8.setLayout(new GridLayout(3, 2));
panel8.add(btn5);
panel9.setLayout(new GridLayout(3, 3));
panel9.add(btn6);
c.add(panel1);
c.add(panel2);
c.add(panel3);
c.add(panel4);
c.add(panel5);
c.add(panel6);
c.add(panel7);
c.add(panel8);
c.add(panel9);
setSize(500,250);
show();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1){
int x = Integer.parseInt(txtF1.getText());
int y = Integer.parseInt(txtF2.getText());
int sum = x + y;
txtF3.setText(String.valueOf(sum));
}
else if (e.getSource() == btn2){
int x = Integer.parseInt(txtF1.getText());
int y = Integer.parseInt(txtF2.getText());
int difference = x - y;
txtF3.setText(String.valueOf(difference));
}
else if (e.getSource() == btn3){
int x = Integer.parseInt(txtF1.getText());
int y = Integer.parseInt(txtF2.getText());
int product = x * y;
txtF3.setText(String.valueOf(product));
}
else if (e.getSource() == btn4){
int x = Integer.parseInt(txtF1.getText());
int y = Integer.parseInt(txtF2.getText());
int quotient = x / y;
txtF3.setText(String.valueOf(quotient));
}
else if (e.getSource() == btn5){
int x = Integer.parseInt(txtF1.getText());
int y = Integer.parseInt(txtF2.getText());
int mod = x % y;
txtF3.setText(String.valueOf(mod));
}
}
public static void main(String[] args) {
Calc app = new Calc();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
我收到这些错误
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at grid.Calc.<init>(Calc.java:49)
at grid.Calc.main(Calc.java:121)