我写了一个差不多的,没出这个问题啊
import java.awt.*;
import javax.swing.*;public class Test
{
public static void main(String[] args)
{
JFrame2 jf2 = new JFrame2();
jf2.setSize(300,300);
jf2.setVisible(true);
}
}
class JFrame1 extends JFrame
{
Container con;
JButton jb1;
public JFrame1()
{
 con = this.getContentPane();
 con.setLayout(new FlowLayout());
 jb1 = new JButton("Button1");
 con.add(jb1);
}
}
class JFrame2 extends JFrame1
{
JButton jb2;
public JFrame2()
{
con = this.getContentPane();
jb2 = new JButton("Button2");
con.add(jb2);
}
}
楼主最好把源代码贴出来,看看是不是因为加的组件只声明而忘记构造了。

解决方案 »

  1.   

    NullPointerException一般是没初始化
      

  2.   

    晕,写super()干吗?
    你每次自己写窗体继承了JFrame的时候,构造函数里写super()么?
      

  3.   

    他是直接继承JFrame吗?如果不是很有可能是自己的一些对象没有初始化。
      

  4.   

    先谢谢大家,问题找到了,是因为JFrame1的init()和JFrame2的init()重名了.大家还要帮我一个忙,我的排版不太对,不知道为什么,大家帮看一下:
    package FrameNoTitle;import java.awt.*;
    import javax.swing.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.Point;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.Toolkit;
    import java.awt.Dimension;public class FrameNoTitle extends JFrame
    {
        javax.swing.JLabel lblUserId = new javax.swing.JLabel();
        javax.swing.JLabel lblSystemTime = new javax.swing.JLabel();
        javax.swing.JLabel lblTitle = new javax.swing.JLabel();
        int iWindowWidth;
        int iWindowHeight;    private Point pressedPt;    public FrameNoTitle()
        {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setSize(400, 300);
            this.setUndecorated(true);
            this.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    pressedPt = e.getPoint();
                }
            });        this.addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    JFrame frame = (JFrame) e.getSource();
                    Point location = frame.getLocation();
                    int x = e.getX() - pressedPt.x + location.x;
                    int y = e.getY() - pressedPt.y + location.y;
                    frame.setLocation(x, y);
                }
            });        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = this.getSize();
            this.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);        initFrame();
            }    public void initFrame()
        {
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            int anchor = gridBagConstraints.CENTER;
            int iFill = gridBagConstraints.NONE;        getContentPane().setLayout(new GridBagLayout());
            setSize(iWindowWidth, iWindowHeight);        lblUserId.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
            lblUserId.setFont(new Font("Serif", Font.PLAIN, 12));
            lblUserId.setText("20030901");
            getContentPane().add(lblUserId,new GridBagConstraints(1,0,1,1,0.0,0.0,anchor,iFill,new Insets(0,12,0,10),60,9));        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            lblTitle.setFont(new Font("Serif", Font.PLAIN, 18));
            getContentPane().add(lblTitle,new GridBagConstraints(0,1,1,2,0.0,0.0,anchor,iFill,new Insets(12,108,206,0),44,2));        lblSystemTime.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
            lblSystemTime.setFont(new Font("Serif", Font.PLAIN, 12));
            lblSystemTime.setText("2003/09/01 12:01:20");
            getContentPane().add(lblSystemTime,new GridBagConstraints(1,1,1,1,0.0,0.0,anchor,iFill,new Insets(0,12,0,10),3,9));
        }    public void setTitle(String sTitle)
        {
            lblTitle.setText("《" + sTitle + "》");
        }    public void setWindowWidth(int iWindowWidth)
        {
            this.iWindowWidth = iWindowWidth;
        }    public void setWindowHeight(int iWindowHeight)
        {
            this.iWindowHeight = iWindowHeight;
        }    public static void main(String[] args)
        {
            FrameNoTitle app = new FrameNoTitle();
            app.setWindowWidth(426);
            app.setWindowHeight(266);
            app.setTitle("标            题");
            app.show();
        }
    }--------------------------------------------------------------
    package GUI;import java.awt.*;
    import javax.swing.*;
    import FrameNoTitle.*;public class Form2 extends FrameNoTitle
    {
        public Form2()
        {
            init();
        }    public static void main(String[] args)
        {
            Form2 app = new Form2();
            app.setSize(337,380);
            app.setTitle("标题");
            app.show();
        }    public void init()
        {
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            int anchor = gridBagConstraints.CENTER;
            int iFill = gridBagConstraints.NONE;
            int iLine = 3;        getContentPane().setLayout(new GridBagLayout());
            getContentPane().setFont(new Font("Dialog", Font.PLAIN, 12));        lbl.setText("lbl:");
            getContentPane().add(lbl,new GridBagConstraints(0,iLine,1,1,0.0,0.0,anchor,iFill,new Insets(24,36,0,0),72,8));
            lbl.setFont(new Font("Serif", Font.PLAIN, 12));
            getContentPane().add(txt,new GridBagConstraints(1,iLine,2,1,0.0,0.0,anchor,iFill,new Insets(12,0,0,0),156,4));
            txt.setFont(new Font("Serif", Font.PLAIN, 12));
            iLine = iLine + 1;    
        }
        javax.swing.JLabel lbl = new javax.swing.JLabel();
        javax.swing.JTextField txt = new javax.swing.JTextField();
        
    }
      

  5.   

    是父窗体与子窗体不在同一个Container中引起的.
      

  6.   

    package TextBox;import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.PlainDocument;
    import TextBox.*;public class TextBox extends JTextField
    {
        private int iMaxLen;    public TextBox()
        {
            super();
            LoadEvent();
        }    public void setMaxLength(int iMaxLength)
        {
            iMaxLen = iMaxLength;
        }
        public void LoadEvent()
        {
            this.addFocusListener(new FocusListener(){
                public void focusLost(FocusEvent e)
                {
                    LostFocus();
                }
                public void focusGained(FocusEvent e)
                {
                    GetFocus();
                }
            });        this.addKeyListener(new MaxLengthKeyListener());
        }
        public void LostFocus()
        {
            if(!IsNumber(this.getText()))
            {
                this.requestFocus();
            }
        }    public void GetFocus()
        {
            this.selectAll();
        }    public static  boolean IsNumber(String  sTmp)
        {
            String sNumber[]={"0","1","2","3","4","5","6","7","8","9"};
            String sComp="";        for(int j=0;j<sTmp.length();j++ )
            {
                sComp=sTmp.substring(j,j+1);
                for(int i=0;i <10 ;i++ )
                {
                    if( sComp.equals(sNumber[i]) )
                    {
                        break;
                    }
                    if ( i == 9)
                    {
                        return false;
                    }
                }
            }
            return true;
        }    protected Document createDefaultModel()
        {
            return new MaxLengthDocument();
        }    class MaxLengthDocument extends PlainDocument
        {
            private int added = 0;        public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException
            {
                if (arg0 < iMaxLen)
                {
                    super.insertString(arg0, arg1, arg2);
                }
            }
        }    class MaxLengthKeyListener implements KeyListener
        {
            private String oldString;        public void keyPressed(KeyEvent arg0)
            {
                checkMaxLength();
            }        public void keyReleased(KeyEvent arg0)
            {
                checkMaxLength();
            }        public void keyTyped(KeyEvent arg0)
            {
                checkMaxLength();
            }        private void checkMaxLength()
            {
                String checkString = getText();
                byte[] byteLength = checkString.getBytes();
                if (byteLength.length > iMaxLen)
                {
                    setText(oldString);
                } else {
                    oldString = checkString;
                }
            }
        }
    }