使用 Java GUI Builder 和凭证处理类创建登录界面

普拉秋什·库马尔

所以我正在尝试使用 Net-beans GUI Builder 构建一个简单的 Java 程序——一个登录界面应用程序。如果您想查看界面设计,请查看此图像。

编辑:净豆IDE 8.2

目的:以在名称密码输入,他们匹配的证书存储和处理在不同的类(具有public volatile String持有"true""false"作为有价值的,因为我真的无法实现适当的布尔一对原因-“实际参数与形式参数不匹配”(我分配了一个方法到布尔变量) ),并为每次成功登录打开一个特定网页,这是特定于特定 ID 的。[ [注意:Credentials.java 中的密码以二进制形式存储,并使用脚本转换为 ASCII,结果与用户输入的密码匹配]]

包含的类: Java_007.java(主类,管理整个可视化界面并将输入数据发送到Credentials.java)和Credentials.java(处理输入并将ID 输入密码输入与存储在其中的配置文件相匹配

代码:
Java_007.java(我在最左边的代码中做了注释)

import java.awt.Desktop;
import java.net.URL;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author walker_001
 */
public class Java_007 extends javax.swing.JFrame {

    /**
     * Creates new form Java_007
     */
    public Java_007() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

//ONLY ASSIGNMENT PART ALONG WITH THE STATUS OF THE OBJECTS(i.e. public, private..)

            back = new javax.swing.JPanel();
            decoration = new javax.swing.JPanel();
            close = new javax.swing.JButton();
            Title = new javax.swing.JLabel();
            name = new javax.swing.JLabel();
            password = new javax.swing.JLabel();
            n = new javax.swing.JTextField();
            ps = new javax.swing.JPasswordField();
            submit = new javax.swing.JButton();
            invalidinput2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setMinimumSize(new java.awt.Dimension(400, 250));
        setUndecorated(true);
        setResizable(false);

        back.setBackground(new java.awt.Color(255, 255, 255));
        back.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0, 204, 255)));
        back.setForeground(new java.awt.Color(255, 255, 255));
        back.setMaximumSize(new java.awt.Dimension(400, 250));
        back.setMinimumSize(new java.awt.Dimension(400, 250));
        back.setPreferredSize(new java.awt.Dimension(400, 250));

        decoration.setBackground(java.awt.Color.white);
        decoration.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 2, 0, new java.awt.Color(0, 204, 255)));
        decoration.setForeground(new java.awt.Color(255, 255, 255));
        decoration.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                decorationMouseDragged(evt);
            }
        });
        decoration.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                decorationMousePressed(evt);
            }
        });

        close.setBackground(new java.awt.Color(0, 0, 0));
        close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Close.png"))); // NOI18N
        close.setBorder(null);
        close.setBorderPainted(false);
        close.setContentAreaFilled(false);
        close.setFocusPainted(false);
        close.setFocusable(false);
        close.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                closeFocusLost(evt);
            }
        });
        close.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                closeMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                closeMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                closeMouseReleased(evt);
            }
        });
        close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeActionPerformed(evt);
            }
        });

        Title.setBackground(new java.awt.Color(255, 255, 255));
        Title.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
        Title.setForeground(new java.awt.Color(0, 204, 255));
        Title.setText("Login");
        Title.setToolTipText("");

        javax.swing.GroupLayout decorationLayout = new javax.swing.GroupLayout(decoration);
        decoration.setLayout(decorationLayout);
        decorationLayout.setHorizontalGroup(
            decorationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, decorationLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Title)
                .addGap(139, 139, 139)
                .addComponent(close, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        decorationLayout.setVerticalGroup(
            decorationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(decorationLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(close, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(Title, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        name.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        name.setForeground(new java.awt.Color(0, 204, 255));
        name.setText("Name : ");

        password.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        password.setForeground(new java.awt.Color(0, 204, 255));
        password.setText("Password : ");

        n.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        n.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        n.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 2, 0, new java.awt.Color(0, 204, 255)));
        n.setCaretColor(new java.awt.Color(0, 204, 255));
        n.setMaximumSize(new java.awt.Dimension(192, 26));
        n.setMinimumSize(new java.awt.Dimension(192, 26));
        n.setPreferredSize(new java.awt.Dimension(192, 26));
        n.setSelectedTextColor(new java.awt.Color(255, 255, 255));
        n.setSelectionColor(new java.awt.Color(0, 204, 255));
        n.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nFocusGained(evt);
            }
        });

        ps.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        ps.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        ps.setToolTipText("");
        ps.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 2, 0, new java.awt.Color(0, 204, 255)));
        ps.setCaretColor(new java.awt.Color(0, 204, 255));
        ps.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        ps.setEchoChar('\u2219');
        ps.setSelectedTextColor(new java.awt.Color(255, 255, 255));
        ps.setSelectionColor(new java.awt.Color(0, 204, 255));
        ps.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                psFocusGained(evt);
            }
        });

        submit.setBackground(new java.awt.Color(0, 0, 0));
        submit.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        submit.setForeground(new java.awt.Color(0, 204, 255));
        submit.setText("Submit");
        submit.setToolTipText("");
        submit.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 2, 0, new java.awt.Color(0, 204, 255)));
        submit.setContentAreaFilled(false);
        submit.setFocusPainted(false);
        submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitActionPerformed(evt);
            }
        });

        invalidinput2.setFont(new java.awt.Font("Segoe UI", 1, 12)); // NOI18N
        invalidinput2.setForeground(new java.awt.Color(255, 0, 0));
        invalidinput2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout backLayout = new javax.swing.GroupLayout(back);
        back.setLayout(backLayout);
        backLayout.setHorizontalGroup(
            backLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(decoration, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, backLayout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(backLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(submit, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(backLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(backLayout.createSequentialGroup()
                            .addGroup(backLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(name)
                                .addComponent(password))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(backLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(ps)
                                .addComponent(n, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addComponent(invalidinput2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(60, 60, 60))
        );
        backLayout.setVerticalGroup(
            backLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backLayout.createSequentialGroup()
                .addComponent(decoration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(invalidinput2, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(backLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(name)
                    .addComponent(n, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(backLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password)
                    .addComponent(ps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(submit, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(back, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(back, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void closeActionPerformed(java.awt.event.ActionEvent evt) {                                      
        System.exit(0);
    }

//真正的代码从这里开始。下面继续 Java_007.java 的代码......

    //user defined variables...start
    int xmouse;
    int ymouse; 
    Credentials c= new Credentials();
    //user defined variables...end

    private void closeMousePressed(java.awt.event.MouseEvent evt) {                                   
        close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Close_pressed.png")));
    }                                  

    private void decorationMouseDragged(java.awt.event.MouseEvent evt) {                                        
        int x= evt.getXOnScreen();
        int y= evt.getYOnScreen();

        setLocation(x - xmouse, y - ymouse);
    }                                       

    private void decorationMousePressed(java.awt.event.MouseEvent evt) {                                        
        xmouse= evt.getX();
        ymouse= evt.getY();
    }                                       

    private void submitActionPerformed(java.awt.event.ActionEvent evt) {                                       
        String a= n.getText();
        String b= ps.getText();
        if (a.equals("") || b.equals("")){
            if (a.equals("") && b.equals("")){
                n.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(255, 0, 0)));
                ps.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(255, 0, 0)));
                invalidinput2.setText("Please enter your name and password.");
            }
            else if(a.equals("")){
                n.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(255, 0, 0)));
                invalidinput2.setText("Please enter your name.");
            }
            else if(b.equals("")){
                ps.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(255, 0, 0)));
                invalidinput2.setText("Please enter your password.");
            }
        }
        else{
            if (a.equals(Credentials.a1) && c.encoded.equals("true")){
                try {
                    Desktop.getDesktop().browse(new URL("https://inbox.google.com/").toURI());
                } catch (Exception e) {}
                System.exit(0);
            }
            else if (a.equals(Credentials.a2) && c.encoded.equals("true")){
                try {
                    Desktop.getDesktop().browse(new URL("https://www.google.com/").toURI());
                } catch (Exception e) {}
                System.exit(0);
            }
            else if (a.equals(Credentials.a3) && c.encoded.equals("true")){
                try {
                    Desktop.getDesktop().browse(new URL("https://images.google.com/").toURI());
                } catch (Exception e) {}
                System.exit(0);
            }
            else{
                invalidinput2.setText("Invalid Credentials");
                //System.out.println(c.id);System.out.println(Credentials.pd);System.out.println(Credentials.passwrd);System.out.println(Credentials.psv);System.out.println(Credentials.encoded);
            }
            n.setText("");
            ps.setText("");
        }

    }                                      

    private void nFocusGained(java.awt.event.FocusEvent evt) {                              
        n.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 255, 255)));
        ps.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 255, 255)));
            invalidinput2.setText("");
    }                             

    private void psFocusGained(java.awt.event.FocusEvent evt) {                               
        n.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 255, 255)));
        ps.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 255, 255)));
            invalidinput2.setText("");
    }                              

    private void closeMouseExited(java.awt.event.MouseEvent evt) {                                  
        close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Close.png")));
    }                                 

    private void closeMouseReleased(java.awt.event.MouseEvent evt) {                                    
        close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Close.png")));
    }                                   

    private void closeFocusLost(java.awt.event.FocusEvent evt) {                                
        close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Close.png")));
    }                               


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Java_007.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Java_007.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Java_007.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Java_007.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Java_007().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel Title;
    private javax.swing.JPanel back;
    private javax.swing.JButton close;
    private javax.swing.JPanel decoration;
    private javax.swing.JLabel invalidinput2;
    public static javax.swing.JTextField n;
    private javax.swing.JLabel name;
    private javax.swing.JLabel password;
    public static javax.swing.JPasswordField ps;
    public volatile javax.swing.JButton submit;
    // End of variables declaration                   
}

凭证.java

public class Credentials{
    public static String a1= "Strix";
    public static String a2= "PK";
    public static String a3= "Molt";

    static String b1= "0010001100110000011110000101011101001101001100010011000001011000";
    static String b2= "00110000001100000011000000110010001101100011100000110100";
    static String b3= "00110001001100100011001100110100";

    volatile String id= Java_007.n.getText();
    volatile String pd= Java_007.ps.getText();
    volatile String passwrd= passwrd(id);

    volatile String psv= psV(passwrd);

    public volatile String encoded= post(psv);

    public static void main(String[] args) {
        //System.out.println(id);System.out.println(pd);System.out.println(passwrd);System.out.println(psv);System.out.println(encoded);
    }

    private String passwrd(String password){
        if (id.equals(a1)){
            password= b1;
        }
        else if (id.equals(a2)){
            password= b2;
        }
        else if (id.equals(a3)){
            password= b3;
        }else{}
        return password;
    }

    private String psV(String e){
        StringBuilder b = new StringBuilder();
        int len = passwrd.length();
        int i = 0;
        while (i + 8 <= len) {
            char c = convert(passwrd.substring(i, i+8));
            i+=8;
            b.append(c);
            e= b.toString();
        }
        return e;

    }

    private static char convert(String bs) {
      return (char)Integer.parseInt(bs, 2);
    }

    private String post(String r){
        boolean go= false;
        if (id.equals(a1)){
            if (pd.equals(psv)){
                go= true;
            }
            else{
                go= false;
            }
        }
        else if (id.equals(a2)){
            if (pd.equals(psv)){
                go= true;
            }
            else{
                go= false;
            }
        }
        else if (id.equals(a3)){
            if (pd.equals(psv)){
                go= true;
            }
            else{
                go= false;
            }
        }
        else{
            go= false;
        }

        if (go == true){
            r= "true";
        }
        else{
            r= "false";
        }

        return r;
    }

}





问题:该程序根本不显示。Net-Beans 在编译时没有显示任何错误,但是当我运行程序时,输出窗格显示NullPointerException

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Credentials.<init>(Credentials.java:10)
    at Java_007.<init>(Java_007.java:240)
    at Java_007$9.run(Java_007.java:361)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 1 second)



从昨天开始我一直在互联网上搜索,但我找不到真正的解决方案。我确定这与Credentials.java中某些变量的 volatile 声明有关,也可能与Java_007.java文件中的处理相关的其他一些声明有关,但我不确定到底是什么问题。任何帮助将不胜感激!谢谢您阅读此篇。请随意索取我可能未提供的任何其他信息。

普拉秋什·库马尔

所以事实证明,我在创建方法/实例之前就调用了它们......我尝试了一些我能想到的事情,例如不使用直接 getter 而是访问变量...... NullPointerException 已解决,但有一些代码规划问题。所以现在我要重新考虑整个计划并转储这段代码。大家好!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章