读书人

空指针错误如何解决啊

发布时间: 2012-02-13 17:20:26 作者: rapoo

空指针异常怎么解决啊...
这是一个简单的窗体,但运行时不能显示内部窗体,并报空指针异常,这是怎么回事啊...求指教...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class swing窗体
{ public static void main(String args[])
{ Mywindow win=new Mywindow();
win.validate();
}
}
class Mywindow extends JFrame
{ JButton button1,button2;
JTextArea text;
JScrollPane scroll;
JInternalFrame interframe;
JSplitPane splitOne,splitTwo;
Mywindow()
{ setSize(300,300);
setVisible(true);
Container con=getContentPane();
con.setLayout(new GridLayout(1,2));
button1=new JButton("button1");
button2=new JButton("button2");
text=new JTextArea(6,12);
scroll=new JScrollPane(text);
splitOne=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,button1,button2);
splitTwo=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,splitOne,scroll);
interframe.setSize(200,200);
interframe.setVisible(true);
Container interCon=interframe.getContentPane();
interCon.setLayout(new FlowLayout());
interCon.add(splitTwo);
JDesktopPane desk=new JDesktopPane();
desk.add(interframe);
getContentPane().add(desk);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

[解决办法]

Java code
Mywindow() {        setSize(300, 300);        setVisible(true);        Container con = getContentPane();        con.setLayout(new GridLayout(1, 2));        button1 = new JButton("button1");        button2 = new JButton("button2");        text = new JTextArea(6, 12);        scroll = new JScrollPane(text);        splitOne = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, button1,                button2);        splitTwo = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, splitOne,                scroll);        interframe = new JInternalFrame("Remember to init");   《======= 这里啊        interframe.setSize(200, 200);        interframe.setVisible(true);        Container interCon = interframe.getContentPane();        interCon.setLayout(new FlowLayout());        interCon.add(splitTwo);        JDesktopPane desk = new JDesktopPane();        desk.add(interframe);        getContentPane().add(desk);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }
[解决办法]
interframe没有new - - 2L已经正解了 就不贴代码了
[解决办法]
Java code
import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JDesktopPane;import javax.swing.JFrame;import javax.swing.JInternalFrame;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTextArea;public class swingFrame {    public static void main(String args[]) {        Mywindow win = new Mywindow();        win.validate();    }}class Mywindow extends JFrame {    JButton button1, button2;    JTextArea text;    JScrollPane scroll;    JInternalFrame interframe = new JInternalFrame();//没有初始化 对三楼无话可说了    JSplitPane splitOne, splitTwo;    Mywindow() {        setSize(300, 300);        setVisible(true);        Container con = getContentPane();        con.setLayout(new GridLayout(1, 2));        button1 = new JButton("button1");        button2 = new JButton("button2");        text = new JTextArea(6, 12);        scroll = new JScrollPane(text);        splitOne = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, button1,                button2);        splitTwo = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, splitOne,                scroll);                interframe.setSize(new Dimension(200, 200));        interframe.setVisible(true);        Container interCon = interframe.getContentPane();        interCon.setLayout(new FlowLayout());        interCon.add(splitTwo);        JDesktopPane desk = new JDesktopPane();        desk.add(interframe);        getContentPane().add(desk);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }} 

读书人网 >J2SE开发

热点推荐