JPanel和JTextField数组错误

时间:2014-10-04 11:53:43

标签: jpanel jtextfield interpreter

我写的是这样的

int p = Integer.parseInt(n.getText());
mas = new JTextField[p];
int i;
for (i=0; i<p; i++) panel.add(mas[i]);

在JTextField中写入n之后单击按钮。我想添加到我的JPanel - 面板新的n个文本字段。但是当我点击按钮时,我会在命令行中看到这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at java.awt.Container.addImpl(Unknown Source)
        at java.awt.Container.add(Unknown Source)
        at pbs.actionPerformed(pbs.java:42)
        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$400(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)

我的所有代码都在这里:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Main implements ActionListener {
    JFrame frame;
    JPanel panel;
    JButton button;
    JLabel nl;
    JTextField n;
    JTextField mas[];
    JTextArea m;
    public static void main(String[] args) {
        Main a = new Main();
        a.go();
    }
    public void go() {
        frame = new JFrame();
        panel = new JPanel();
        panel.setBackground(Color.darkGray);
        button = new JButton("OK");
        button.addActionListener(this);
        n = new JTextField(20);
        nl = new JLabel("Set count of data: ");
        nl.setLabelFor(n);
        nl.setForeground(Color.white);
        panel.add(nl);
        panel.add(n);
        panel.add(BorderLayout.NORTH,button);
        frame.getContentPane().add(panel);
        frame.setSize(300,300);
        frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent event) {
        button.setText("Clicked");
        int p = Integer.parseInt(n.getText());
        mas = new JTextField[p];
        int i;
        for (i=0; i<p; i++) panel.add(mas[i]);
    }
}

1 个答案:

答案 0 :(得分:2)

mas[i]始终为null,因为您没有为JTextField()调用缩写器。 mas = new JTextField[p];只是初始化大小为p的数组。

执行类似

的操作
mas = new JTextField[p];
int i;
for (i=0; i<p; i++){
    mas[i] = new JTextField(...);
    panel.add(mas[i]);
}