为什么GUI无法正常工作,代码是否正确?

时间:2013-08-06 23:09:45

标签: java swing nullpointerexception jpanel

所以我试图创建3个面板。第一个面板具有布局设置(例如单选按钮和下一个按钮)我现在添加两个具有不同背景颜色的新面板。但是当我执行代码时,我得到一个Null点异常的错误。我该如何解决这个问题?

以下是代码:

import java.awt.Color;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.CardLayout;
import javax.swing.*;

public class Wizard {

private JLabel lblPicture;
private JRadioButton btLdap, btKerbegos, btSpnego, btSaml2;
private JButton btNext;
private JPanel panel;
private JPanel panelFirst;
private JPanel panelSecond;
CardLayout c1 = new CardLayout();

    public static void main(String[] args) {
        new Wizard();
    }

    public Wizard() {

        JFrame frame = new JFrame("Wizard");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,360);
        frame.setVisible(true);
        MyPanel();
        RadioButtons();
        Button();
        Image();
        groupButton();
        panel.setLayout(c1);
        panelFirst.setBackground(Color.BLUE);
        panelSecond.setBackground(Color.GREEN);
        panel.add(panelFirst,"1");
        panel.add(panelSecond,"2");
        c1.show(panel,"panel");
        frame.add(panel); 
        frame.pack();
        frame.setVisible(true);
    }

    public void MyPanel() {
        panel = new JPanel();
        panel.setLayout(null);
    }

    public void RadioButtons() {
        btLdap = new JRadioButton ("Ldap");
        btLdap.setBounds(60,85,100,20);
        panel.add(btLdap);

        btKerbegos = new JRadioButton ("Kerbegos");
        btKerbegos.setBounds(60,115,100,20);
        panel.add(btKerbegos);

        btSpnego =new JRadioButton("Spnego");
        btSpnego.setBounds(60,145,100,20);
        panel.add(btSpnego);

        btSaml2 = new JRadioButton("Saml2");
        btSaml2.setBounds(60,175,100,20);
        panel.add(btSaml2);
    }

    public void Button() {
        btNext = new JButton ("Next");
        btNext.setBounds(400,260,100,20);
        panel.add(btNext);
        btNext.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    c1.show(panel, "2");
                }
            });
    }

    public void Image() {
        ImageIcon image = new ImageIcon("image.jpg");
        lblPicture = new JLabel(image);
        lblPicture.setBounds(200,20, 330, 270);
        panel.add(lblPicture);
    }

    private void groupButton() {

        ButtonGroup bg1 = new ButtonGroup( );

        bg1.add(btLdap);
        bg1.add(btKerbegos);
        bg1.add(btSpnego);
        bg1.add(btSaml2);

    }

}

3 个答案:

答案 0 :(得分:4)

当我去运行你的代码时,我得到你提到的空指针异常:

Exception in thread "main" java.lang.NullPointerException
    at Wizard.<init>(Wizard.java:35)
    at Wizard.main(Wizard.java:20)

所以,我在向导的构造函数中查找了产生它的行:

panelFirst.setBackground(Color.BLUE);

我看到你在panelFirst上设置了一个属性,它是Wizard类的实例数据成员。

我没有看到您声明panelFirst = new JPanel();的任何地方,这是创建NullPointerException的地方。看起来你还没有初始化许多其他变量(例如,panel是我看到的唯一已初始化的JPanel。)

请在Java API中查找JPanel的构造函数,并了解如何为应用程序创建它们。您也可以考虑使用IDE为您生成GUI代码。

JPanel API(自jdk 1.7起):http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html

由于

答案 1 :(得分:3)

panelFirstpanelSecond对象永远不会被创建。

答案 2 :(得分:2)

panelFirstpanelSecond变量为null,未声明。

在设置面板背景之前,您需要创建它:

panelFirst= new JPanel();
与PanelSecond相同:

panelSecond = new JPanel();

如果有空指针异常(也称为NPE):您应该尝试查找uninitialized variable。声明变量但未初始化时,其指针指向null(即它是空指针!)