【问题标题】:JPanel and JTextField array errorsJPanel 和 JTextField 数组错误
【发布时间】:2014-11-29 07:33:07
【问题描述】:

我写的是这样的

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]);
    }
}

【问题讨论】:

    标签: jpanel jtextfield interpreter


    【解决方案1】:

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

    做类似的事情

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

    【讨论】:

    • 太棒了。欢迎使用 StackOverflow
    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2014-04-10
    • 2016-08-29
    • 1970-01-01
    相关资源
    最近更新 更多