[code=Java][/code]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestGui2 {
JFrame frame;
JPanel pane;
JButton equal;
JTextField view;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton button0;
JButton buttonAdd;
JButton buttonReduce;
JButton buttonMultiply;
JButton buttonDivide;
JButton buttonClear;
public void setGui(){
frame = new JFrame();
view = new JTextField();
equal = new JButton();
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
button0 = new JButton("0");
buttonAdd = new JButton("+");
buttonReduce = new JButton("-");
buttonMultiply = new JButton("x");
buttonDivide = new JButton("%");
buttonClear = new JButton("clear");
pane.add(button1);
pane.add(button2);
pane.add(button3);
pane.add(button4);
pane.add(button5);
pane.add(button6);
pane.add(button7);
pane.add(button8);
pane.add(button9);
pane.add(button0);
pane.add(buttonAdd);
pane.add(buttonReduce);
pane.add(buttonMultiply);
pane.add(buttonDivide);
pane.add(equal);
pane.add(buttonClear);
GridLayout layout = new GridLayout(4,4);
pane.setLayout(layout);
frame.getContentPane().add(BorderLayout.NORTH,view);
frame.getContentPane().add(BorderLayout.CENTER,pane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,800);
}
public static void main(String [] args){
new TestGui2().setGui();
}
}
Exception in thread "main" java.lang.NullPointerException
at TestGui2.setGui(TestGui2.java:43)
at TestGui2.main(TestGui2.java:69)
------解决方案--------------------
老大 , JPanel pane 没new 。
------解决方案--------------------
你的pane没有new就用了啊。。。。
pane = new JPanel();
------解决方案--------------------
加上中间那句buttonClear = new JButton("clear");
pane = new JPanel(); pane.add(button1);
------解决方案--------------------
在pane.add(button1)之前加上pane=frame.getContentPane();
------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestGui2 {
JFrame frame;
JPanel pane;
JButton equal;
JTextField view;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton button0;
JButton buttonAdd;
JButton buttonReduce;
JButton buttonMultiply;
JButton buttonDivide;
JButton buttonClear;
public void setGui() {
frame = new JFrame();
view = new JTextField();
equal = new JButton();
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");