程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> gui-實在搞不懂,這裡的錯誤。求助

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看看執行過程


            } 

lhs472194266
danielinbiti
lhs472194266
lhs472194266
lhs472194266
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved