gui-实在搞不懂,这里的错误。求助

问题描述

实在搞不懂,这里的错误。求助
 package viewer;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javafx.scene.layout.Border;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import util.EmpUtil;
import model.EmpException;
import model.User;

public class UserPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private JPanel jp1, jp2;
    private JLabel jl1;
    private JTable jt;
    private JScrollPane jsp;
    private JButton jb1, jb2, jb3;
    private JTable jtable;
    private UserTableModel usermodel;
    private AddDialog ad;
    private JFrame jf;

    public UserPanel(JFrame jf) {
        this.setLayout(new BorderLayout());
        this.jf = jf;
        jp1 = new JPanel();
        jp2 = new JPanel();
        jl1 = new JLabel("用户管理界面");
        jb1 = new JButton("添加用户");
        jb1.addActionListener(new UserManagerClick());
        jb2 = new JButton("删除用户");
        jb2.addActionListener(new UserManagerClick());
        jb3 = new JButton("修改用户");
        jb3.addActionListener(new UserManagerClick());
        jp1.add(jl1);
        jp2.add(jb1);
        jp2.add(jb2);

        jp2.add(jb3);
        usermodel = new UserTableModel();
        jtable = new JTable(usermodel);
        jsp = new JScrollPane(jtable);
        this.add(jp1, BorderLayout.NORTH);
        this.add(jp2, BorderLayout.SOUTH);
        this.add(jsp);
    }

    private class UserManagerClick implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jb1) {
                // 添加
                ad = new AddDialog();

            } else if (e.getSource() == jb2) {
                // 删除
            } else if (e.getSource() == jb3) {
                // 修改
            }
        }

    }

    private class AddDialog extends JDialog {
        private JLabel jl1, jl2, jl3;
        private JButton jb1, jb2;
        private JPanel jp1, jp2, jp3, jp4;
        private JTextField jtf1, jtf2;
        private JPasswordField jpf;
        private JDialog jdg=this;
        public AddDialog() {
            this.setSize(300, 200);
            this.setModal(true); // 设置为模态对话框,不能操作主窗体。如果这里用JFrame就无法实现该功能了。
            this.setLocation(jf.getX() + 50, jf.getY() + 50);
            this.setTitle("添加用户信息");
            this.setLayout(new GridLayout(4, 1));
            jp1 = new JPanel();
            jp2 = new JPanel();
            jp3 = new JPanel();
            jp4 = new JPanel();
            jl1 = new JLabel("用  户  名:");
            jl2 = new JLabel("用户密码:");
            jl3 = new JLabel("用户昵称:");
            jb1 = new JButton("添加用户");jb1.addActionListener(new AddDialogClick());
            jb2 = new JButton("重置数据");jb2.addActionListener(new AddDialogClick());
            jtf1 = new JTextField(20);
            jtf2 = new JTextField(20);
            jpf = new JPasswordField(20);
            jp1.add(jl1);
            jp1.add(jtf1);
            jp2.add(jl2);
            jp2.add(jpf);
            jp3.add(jl3);
            jp3.add(jtf2);
            jp4.add(jb1);
            jp4.add(jb2);
            this.add(jp1);
            this.add(jp2);
            this.add(jp3);
            this.add(jp4);
            this.pack();
            this.setResizable(false);
            this.setVisible(true);

        }

        private void reset() {
            jtf1.setText("");
            jtf2.setText("");
            jpf.setText("");
        }

        private class AddDialogClick implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if (e.getSource() == jb1) {
                        // 添加用户
                        String username=jtf1.getText();
                        if (username == null
                                ||"".equals(username.trim())) {
                            //属于哪个窗口就是当弹出提示对话框时,非属于的其他窗体全部隐藏不显示.
                        /*  JOptionPane.showMessageDialog(jdg, "请输入正确的用户名!", "发现错误!",
                                    JOptionPane.ERROR_MESSAGE);*/
                            EmpUtil.showError(jdg, "请输入正确的用户名!");
                            return;//必须得有return。虽然弹出提示对话框,但是程序还是继续往下执行的。
                        }
                        String password=new String(jpf.getPassword());
                        String nickname=jtf2.getText();
                        User u=new User();
                        u.setUsername(username);
                        u.setPassword(password);
                        u.setNickname(nickname);
                        usermodel.getUd().add(u);
                        AddDialog.this.setVisible(false);
                            //ad.dispose();
                    } else if (e.getSource() == jb2) {
                        // 重置数据
                        reset();
                    }
                } catch (EmpException e1) {
                    EmpUtil.showError(jdg, e1.getMessage());
            }

        }
    }
    }}


AddDialog.this.setVisible(false);为什么这句改成ad.setVisible(false);
会报空指针异常。

解决方案

 if (e.getSource() == jb1) {
                // 添加
                ad = new AddDialog();//弹出窗体,然后这里是不往下执行,只有窗体不显示了才往下执行。所以在弹出的时候,界面内操作时,ad还没有赋值完成,是null,你可以这句话前后加print看看执行过程

            } 

解决方案二:

你确定时ad空指针错误么?我运行了你的代码,把你没有提供的类注释掉后,可以完成你想要的操作。这是我运行的代码,没有异常,贴代码挺好的,但是贴异常信息最重要。


import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class UserPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private JPanel jp1, jp2;
    private JLabel jl1;
    private JTable jt;
    private JScrollPane jsp;
    private JButton jb1, jb2, jb3;
    private JTable jtable;
    private AddDialog ad;
    private JFrame jf;

    public UserPanel(JFrame jf) {
        this.setLayout(new BorderLayout());
        this.jf = jf;
        jp1 = new JPanel();
        jp2 = new JPanel();
        jl1 = new JLabel("用户管理界面");
        jb1 = new JButton("添加用户");
        jb1.addActionListener(new UserManagerClick());
        jb2 = new JButton("删除用户");
        jb2.addActionListener(new UserManagerClick());
        jb3 = new JButton("修改用户");
        jb3.addActionListener(new UserManagerClick());
        jp1.add(jl1);
        jp2.add(jb1);
        jp2.add(jb2);

        jp2.add(jb3);
        // usermodel = new UserTableModel();
        jtable = new JTable();
        jsp = new JScrollPane(jtable);
        this.add(jp1, BorderLayout.NORTH);
        this.add(jp2, BorderLayout.SOUTH);
        this.add(jsp);
    }

    private class UserManagerClick implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jb1) {
                // 添加
                ad = new AddDialog();

            } else if (e.getSource() == jb2) {
                // 删除
            } else if (e.getSource() == jb3) {
                // 修改
            }
        }

    }

    private class AddDialog extends JDialog {
        private JLabel jl1, jl2, jl3;
        private JButton jb1, jb2;
        private JPanel jp1, jp2, jp3, jp4;
        private JTextField jtf1, jtf2;
        private JPasswordField jpf;
        private JDialog jdg = this;

        public AddDialog() {
            this.setSize(300, 200);
            this.setModal(true); // 设置为模态对话框,不能操作主窗体。如果这里用JFrame就无法实现该功能了。
            this.setLocation(jf.getX() + 50, jf.getY() + 50);
            this.setTitle("添加用户信息");
            this.setLayout(new GridLayout(4, 1));
            jp1 = new JPanel();
            jp2 = new JPanel();
            jp3 = new JPanel();
            jp4 = new JPanel();
            jl1 = new JLabel("用  户  名:");
            jl2 = new JLabel("用户密码:");
            jl3 = new JLabel("用户昵称:");
            jb1 = new JButton("添加用户");
            jb1.addActionListener(new AddDialogClick());
            jb2 = new JButton("重置数据");
            jb2.addActionListener(new AddDialogClick());
            jtf1 = new JTextField(20);
            jtf2 = new JTextField(20);
            jpf = new JPasswordField(20);
            jp1.add(jl1);
            jp1.add(jtf1);
            jp2.add(jl2);
            jp2.add(jpf);
            jp3.add(jl3);
            jp3.add(jtf2);
            jp4.add(jb1);
            jp4.add(jb2);
            this.add(jp1);
            this.add(jp2);
            this.add(jp3);
            this.add(jp4);
            this.pack();
            this.setResizable(false);
            this.setVisible(true);

        }

        private void reset() {
            jtf1.setText("");
            jtf2.setText("");
            jpf.setText("");
        }

        private class AddDialogClick implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if (e.getSource() == jb1) {
                        // 添加用户
                        String username = jtf1.getText();
                        if (username == null || "".equals(username.trim())) {
                            // 属于哪个窗口就是当弹出提示对话框时,非属于的其他窗体全部隐藏不显示.
                            /*
                             * JOptionPane.showMessageDialog(jdg, "请输入正确的用户名!",
                             * "发现错误!", JOptionPane.ERROR_MESSAGE);
                             */
                            // EmpUtil.showError(jdg, "请输入正确的用户名!");
                            return;// 必须得有return。虽然弹出提示对话框,但是程序还是继续往下执行的。
                        }
                        String password = new String(jpf.getPassword());
                        String nickname = jtf2.getText();
                        // User u = new User();
                        // u.setUsername(username);
                        // u.setPassword(password);
                        // u.setNickname(nickname);
                        // usermodel.getUd().add(u);
                        AddDialog.this.setVisible(false);
                        // ad.dispose();
                    } else if (e.getSource() == jb2) {
                        // 重置数据
                        reset();
                    }
                } catch (Exception e1) {
                    // EmpUtil.showError(jdg, e1.getMessage());
                }

            }
        }
    }

    public static void main(String[] args) {
        JFrame frm = new JFrame();
        UserPanel up = new UserPanel(frm);
        frm.getContentPane().add(up);
        frm.setSize(300, 400);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}

解决方案三:

整个代码只有UserManagerClick里会初始化ad
如果直接执行AddDialogClick,ad就是null

解决方案四:

AddDialog 没有实例化

解决方案五:

解决方案六:

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at viewer.UserPanel$AddDialog$AddDialogClick.actionPerformed(UserPanel.java:141)
    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.pumpEventsForFilter(Unknown Source)
    at java.awt.WaitDispatchSupport$2.run(Unknown Source)
    at java.awt.WaitDispatchSupport$4.run(Unknown Source)
    at java.awt.WaitDispatchSupport$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.WaitDispatchSupport.enter(Unknown Source)
    at java.awt.Dialog.show(Unknown Source)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at java.awt.Dialog.setVisible(Unknown Source)
    at viewer.UserPanel$AddDialog.<init>(UserPanel.java:108)
    at viewer.UserPanel$UserManagerClick.actionPerformed(UserPanel.java:59)
    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)

解决方案七:

if (e.getSource() == jb1) {
// 添加
ad = new AddDialog();//弹出窗体,然后这里是不往下执行,只有窗体不显示了才往下执行。所以在弹出的时候,界面内操作时,ad还没有赋值完成,是null,你可以这句话前后加print看看执行过程

时间: 2025-01-31 01:43:36

gui-实在搞不懂,这里的错误。求助的相关文章

图片-大量悬赏 数据结构的问题 自己是新手 实在搞不懂 求大神帮忙

问题描述 大量悬赏 数据结构的问题 自己是新手 实在搞不懂 求大神帮忙 解决方案 是要题目的翻译么? 实现一个链表及以下操作 插入,给定Key值,找到拥有该Key值的节点,并把新节点插入此节点后.若没有在链表中找到该Key值,打印出错误信息. 删除,给定Key值,找到拥有该Key值的节点并删除.若没有在链表中找到该Key值,打印出错误信息. 查询前序节点,给定Key值,找到拥有该Key值的节点的前序节点.若没有在链表中找到该Key值,打印错误信息. 显式整个链表,若链表为空,则显示链表为空. 从

c语言-简单的数组问题有两个bug实在搞不懂

问题描述 简单的数组问题有两个bug实在搞不懂 #include<stdio.h> #include<string.h> void bubsort(char *name[],int n) { int k,m,i,j; char d; k=0; m=n-1; while(k<m) { j=m-1; for(i=0;i<=j;i++) if(strcmp(name[i],name[i+1])) {d=*name[i];*name[i]=*name[i+1];*name[i+

c语言-请问这句话如何编写成C语言代码?(逻辑关系实在搞不懂)

问题描述 请问这句话如何编写成C语言代码?(逻辑关系实在搞不懂) 当电压大于5时,开关断开:直到电压小于3时,开关闭合:在这期间,开关保持断开. 开关闭合后,直到电压大于5,开关断开:在这期间,开关保持闭合. ps:我试着用if ?elseif语句,发现最后一点怎么也满足不了,请各位大神帮忙解答,谢谢! 解决方案 我说了有3个变量 v>5 s v 000 001 010 ... 你排列下去,因为v>5 v<3不同时存在,可以去掉 剩下的你可以用卡诺图化简得到表达式. 解决方案二: 首先,

实在搞不懂了,请高人们进来指点指点.

问题描述 问题是这样的.网站www.cmbin.com用到了ajax,我在本地调试时是好好的,但是上传到服务器上后那个分页的部分一直显示:正在加载数据.我通过alert设置了"断点"看了一下,它根本就没有执行某一段代码,具体是这样的functionreadsss(){//代码片断1for(){//forxunhuan里的代码片段2}//代码片段3}问题是它执行完代码片段2后直接跳出来了,没有执行代码片段3里面的分页代码.这是怎么回事?另外:为什么我在本地调试好的代码上传到服务器上后会发

c++-新手学C++求助大神,编译过了,运行错误搞不懂啊

问题描述 新手学C++求助大神,编译过了,运行错误搞不懂啊 #include using namespace std; struct Node { int data; Node *next; }; int count=0; Node *first; void creatList(int a[],int n) { Node *s,*r; r=first; for(int i=0;i { s=new Node; s->data=a[i]; r->next=s;r=s; } r->next=f

c语言-strcpy和strcat的使用问题,搞不懂为什么出现这样的错误?怎么改呢》?

问题描述 strcpy和strcat的使用问题,搞不懂为什么出现这样的错误?怎么改呢>? int _tmain(int argc, _TCHAR* argv[]) { char n; CString strTime; // 用于将CTime对象格式化为字符串 CTime curTime = CTime::GetCurrentTime(); cout<<GetExePath()<<endl; /*cout<<"请输入要删除几天前的文件夹:"; c

c语言-求帮助写一个代码 刚学习数据结构 实在是搞不懂 求大神帮忙谢谢

问题描述 求帮助写一个代码 刚学习数据结构 实在是搞不懂 求大神帮忙谢谢 好心人帮忙翻译好了 求大神帮忙写一下代码 谢谢大家了 解决方案 你的需求,要至少4000C币,你给的100太少了 解决方案二: http://blog.csdn.net/qq_31766907/article/details/50331951这个链接,你看看,或许能帮到你.

winapi-大家帮我检查一下好吗?实在搞不明白,在对话框中输入数据,在主窗口绘直线

问题描述 大家帮我检查一下好吗?实在搞不明白,在对话框中输入数据,在主窗口绘直线 #include #include "resource.h" LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ; char chr1[10], chr2[10], chr3[10],chr4[10]; int number1, nu

javascript-數組的push函數,搞不懂

问题描述 數組的push函數,搞不懂 var a = [123];var b = [456];Array.prototype.push.apply(a b);console.log(a); //[123456] var a = [123];var b = [456];a.push(b);console.log(a); 這兩個有何不同?我覺得應該得出相同的結果才對啊為什麼會不同呢? 解决方案 a.push(b);是把b这个数组对象放到a里面 push.apply(a b);//是因为apply的第