Java JTextfield只给我空数据

我做了一个Mainmenu JFrame,它可以生成4个不同的新框架代表每个菜单。 Mainmenu框架工作正常。 但我发现它无法从新的框架文本框中获取文本。 这里是错误报告和源代码。 请让我知道如何解决它。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.JTable;


public class PhoneBook extends JFrame implements ActionListener
{

    private String[] colName;


    JPanel panel_main;
    JPanel panel_phbook;
    JPanel panel_numb;
    JPanel panel_grp;

    JButton btn_main_phb;
    JButton btn_main_name;
    JButton btn_main_numb;
    JButton btn_main_grp;


    JTextArea ta_name_serch;
    TextField tf_name_input;

    JButton btn_name_search;
    JButton btn_name_cancel;
    JButton btn_name_modify;
    JButton btn_name_del;
    JButton btn_name_send;

    JTextArea ta_numb_serch;
    TextField tf_numb_input;

    JButton btn_numb_search;
    JButton btn_numb_cancel;
    JButton btn_numb_modify;
    JButton btn_numb_del;
    JButton btn_numb_send;

    JButton btn_grp_search;
    JButton btn_grp_cancel;
    JButton btn_grp_modify;
    JButton btn_grp_del;
    JButton btn_grp_send;
    private JTable table;




    public static void main(String[] args) 
    {
        PhoneBook frame = new PhoneBook();

    }
    public PhoneBook() 
    {

        colName = new String[3];

        panel_main = new JPanel();
        panel_main.setLayout(new GridLayout());

        btn_main_phb = new JButton("phonbook");
        btn_main_name = new JButton("name");
        btn_main_numb = new JButton("number");
        btn_main_grp = new JButton("grp");

        btn_main_phb.addActionListener(this);
        btn_main_name.addActionListener(this);
        btn_main_numb.addActionListener(this);
        btn_main_grp.addActionListener(this);


        panel_main.add(btn_main_phb);
        panel_main.add(btn_main_name);
        panel_main.add(btn_main_numb);
        panel_main.add(btn_main_grp);





//      table = new JTable();
//      table.setColumnSelectionAllowed(true);
//      panel_main.add(table);
//      table.set


        panel_phbook = new JPanel();
        panel_phbook.setLayout(new GridLayout());


        //      panel_phbook.add(btn_main_phb);
//      panel_phbook.add(btn_main_name);
//      panel_phbook.add(btn_main_numb);
//      panel_phbook.add(btn_main_grp);

        btn_name_search = new JButton("Serch");
        btn_name_cancel = new JButton("Serch Cancel");
        btn_name_modify = new JButton("Modi");
        btn_name_del = new JButton("del");
        btn_name_send = new JButton("sms send");

        TextField tf_name_input = new TextField();
        panel_phbook.add(tf_name_input);
        panel_phbook.add(btn_name_search);
        btn_name_search.addActionListener(this);

//      

        panel_numb = new JPanel();
        panel_grp = new JPanel();

        //getContentPane().setLayout(BorderLayout());
        getContentPane().setLayout(new BorderLayout());
        setLocation(500, 400);
        getContentPane().add(panel_main);
        setSize(650,150);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }



    @Override       public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource().equals(btn_main_phb))
        {

            JFrame frame2 = new JFrame();
            frame2.getContentPane().add(panel_phbook);
            frame2.setLocation(400, 300);
            frame2.setSize(500,400);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame2.setVisible(true);

        }
        else if(e.getSource().equals(btn_main_name))
        {

        }
        else if(e.getSource().equals(btn_main_numb))
        {

        }
        else if(e.getSource().equals(btn_main_grp))
        {

        }
        else if(e.getSource().equals(btn_name_search))//this is the problem
        {
            System.out.println(tf_name_input.getText());
        }
    }

异常在线程“AWT-EventQueue-0”java.lang.NullPointerException在PhoneBook.actionPerformed(PhoneBook.java:166)在javax.swing.AbstractButton.fireActionPerformed(未知源)在javax.swing.AbstractButton $ Handler.actionPerformed(未知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 java.awt.Component.processEvent(Unknown Source)at java.awt.Component.dispatchEventImpl(Unknown)(位于java.awt.Component.processEvent(Unknown Source))java.awt.Component.processEvent(Unknown Source)在javax.swing.JComponent.processMouseEvent(Unknown Source) Source)在java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)上的java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)java.awt.Component.dispatchEventImpl(Unknown Source)在java.awt.Component.dispatchEvent(Unknown Source)在java.awt.Lightwe 在java.awt.Window.dispatchEventImpl(Unknown Source)上的java.awt.Container.dispatchEventImpl(Unknown Source)处的java.awt.Component.dispatchEvent(Unknown Source)处的java.awt.EventQueue处的ightDispatcher.dispatchEvent(Unknown Source)。 java.awt.EventQueue上的dispatchEventImpl(Unknown Source)java.awt.EventQueue上的$ 200(Unknown Source)java.awt.EventQueue上的$ 3.run(Unknown Source)java.security.AccessController上的$ 3.run(Unknown Source) java.security.ProtectionDomain上的.doPrivileged(Native方法)java.security.ProtectionDomain上的$ 1.doIntersectionPrivilege(Unknown Source)java.awt.EventQueue上的$ 1.doIntersectionPrivilege(Unknown Source)java.awt上的$ 4.run(Unknown Source)。在java.security.ProtectionDomain上的java.security.AccessController.doPrivileged(Native方法)处的EventQueue $ 4.run(未知源)在java.awt上的java.awt.EventQueue.dispatchEvent(Unknown Source)上的$ 1.doIntersectionPrivilege(Unknown Source)。 java.awt.EventDispatchThread的EventDispatchThread.pumpOneEventForFilters(Unknown Source) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)上的java.util.PeaseEventsForFilter(Unknown Source)在java.awt.EventDispatchThread.pumpEvents(Unknown Source)在java.awt.EventDispatchThread.pumpEvents(Unknown Source)


你遇到这个问题的原因是因为你创建了一个新的局部变量,其名称与你的一个类成员变量相同:

TextField tf_name_input = new TextField();

代替:

tf_name_input = new TextField();
链接地址: http://www.djcxy.com/p/73439.html

上一篇: Java JTextfield gives me only null data

下一篇: NullPointerException when making a change from a JDialog to a JFrame