在鼠标单击上添加文本字段和标签不能正常工作Java

时间:2015-06-08 18:52:56

标签: java swing nullpointerexception jbutton actionlistener

我有这段代码:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Interface2 {

    private JLabel label;
    private JLabel label2;
    private JLabel label3;
    private JTextField textfield;
    private JTextField textfield2;
    private JTextField textfield3;
    private JButton button;
    private JButton button2;
    private JButton button3;
    private JPanel panel;

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

    public Interface2() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);

        panel = new JPanel();

        JPanel buttonPane = new JPanel();
        label = new JLabel("Button 1");
        label2 = new JLabel("Button 2");
        label3 = new JLabel("Button 3");

        textfield = new JTextField(5);

        JButton button = new JButton("cirkel");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label);
                panel.add(textfield);
                panel.revalidate();
                panel.repaint();
                String text = textfield.getText();

                // Van de String 'text' een double maken

//      cirkel C1 = new cirkel();
//      C1.setDiam(diameter);
                label.setText("De diameter = " + 1 + " cm \n\n");
                label.setText("De straal = " + 2 + " cm");
                label.setText("De oppervlakte van de cirkel = " + 3 + " cm2");
            }
        });

        JButton button2 = new JButton("driehoek");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label2);
                panel.add(textfield2);
                panel.revalidate();
                panel.repaint();
                // driehoek
            }
        });

        JButton button3 = new JButton("vierhoek");
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label3);
                panel.add(textfield3);
                panel.revalidate();
                panel.repaint();
                // vierhoek
            }
        });

        buttonPane.add(button);
        buttonPane.add(button2);
        buttonPane.add(button3);

        frame.add(buttonPane, BorderLayout.NORTH);
        frame.add(panel);
        frame.setVisible(true);
    }
}

我想为三个按钮中的每一个添加标签和文本字段。

但如果我在这种情况下点击任何按钮,它会给我一个错误。

我不明白为什么它不起作用,我是Java的新手......

这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Interface.actionPerformed(Interface.java:68)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:2)

请确保textfield2和textfield3已初始化。

textfield2 = new JTextField(5);
textfield3 = new JTextField(5);

否则您将收到NullPointerException。

答案 1 :(得分:2)

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

public Interface2() {
    // ..

main类的Interface2方法可能不应该创建Interface的实例!