0

我有一个 Java 程序,它有两个类,主类和appGUI类。

这是主类:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {

    /**
     * @param args
     */

    public static void main(String[] args) throws Exception {

        appGUI gui = new appGUI();
        gui.setVisible(true);

        String password = ""; //The password entered...
        try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        System.out.println(new BigInteger(1, md.digest()).toString(16));

        } catch(NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

    }

    }

这是 appGUI 类:

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 * Created by JFormDesigner on Wed Apr 03 19:24:35 BST 2013
 */



/**
 * @author Hrach Ghapantsyan
 */
public class appGUI extends JFrame {
    public appGUI() {
        initComponents();
    }

    private void loginButtonActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Hrach Ghapantsyan
        loginPasswordField = new JPasswordField();
        loginUsernameField = new JTextField();
        usernameLabel = new JLabel();
        passwordLabel = new JLabel();
        loginButton = new JButton();
        titleLabel = new JLabel();

        //======== this ========
        setTitle("Experimental X | Administrator Login");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(loginPasswordField);
        loginPasswordField.setBounds(80, 65, 100, loginPasswordField.getPreferredSize().height);
        contentPane.add(loginUsernameField);
        loginUsernameField.setBounds(80, 35, 100, loginUsernameField.getPreferredSize().height);

        //---- usernameLabel ----
        usernameLabel.setText("Username:");
        contentPane.add(usernameLabel);
        usernameLabel.setBounds(20, 40, 55, usernameLabel.getPreferredSize().height);

        //---- passwordLabel ----
        passwordLabel.setText("Password:");
        contentPane.add(passwordLabel);
        passwordLabel.setBounds(20, 70, 55, passwordLabel.getPreferredSize().height);

        //---- loginButton ----
        loginButton.setText("Login");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginButtonActionPerformed(e);
            }
        });
        contentPane.add(loginButton);
        loginButton.setBounds(80, 95, 100, loginButton.getPreferredSize().height);

        //---- titleLabel ----
        titleLabel.setText("Experimental X | Administrator Login");
        contentPane.add(titleLabel);
        titleLabel.setBounds(45, 10, 190, titleLabel.getPreferredSize().height);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, ``preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(270, 170);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - Hrach Ghapantsyan
    private JPasswordField loginPasswordField;
    private JTextField loginUsernameField;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JButton loginButton;
    private JLabel titleLabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

当我运行 Java 代码时,appGUI 类被激活,这会导致弹出窗口。弹出一个登录框。弹出登录框时,要求用户输入其用户名和密码。

我想要发生的是当用户单击登录时,它将密码提交给 Main 类并执行以下操作:

    String password = "the password that the user just inputed";

在里面:

    public static void main(String[] args) throws Exception {}

有没有办法做到这一点?

4

3 回答 3

1

一种方法是:

  1. 使您的 appGUI 成为“主”类的内部类(请给一个更好的名称,我们暂时称它为测试。)

  2. 在“测试”类中将您的密码声明为实例变量

  3. 创建内部类的实例

    Test q1=new Test()  
    appGUI gui = q1.new appGUI();
    
  4. 在您的 loginButtonActionPerformed 方法中

           private void loginButtonActionPerformed(ActionEvent e) {
               // TODO add your code here
              password=loginPasswordField.getText();
           }
    
  5. 这不会完全解决您的问题。对内部类的调用可以在一个单独的线程中进行,以便您“测试”类/主线程等到您的用户完成提交,然后您可以使用 MessageDigest 进行帖子解析

希望这可以帮助!

于 2013-04-03T16:56:13.330 回答
0

在您的Main班级中为密码创建一个PRIVATE String变量。您可以简单地将现有的String password并将其移动到班级本身。

然后创建PUBLIC SetPassword方法来为这个字符串赋值,也在Main类中。

appGUI用户单击“登录”按钮后执行操作的方法中的类中,调用此GetPassword方法并将来自 GUI 的密码字符串传递给它。

我希望以上内容可以提供足够的提示,而无需我为您编写实际代码。

于 2013-04-03T16:56:59.683 回答
-1

当您制作密码字符串时,您需要向其中添加一个 var,以便您稍后可以定义该字符串 = 表示 var。前任。

String password = new String("Somthing");

然后

 appGUI gui = new appGUI();

 if(gui.actionPerformed(loginPasswordField2).equals(password){

 }

将其添加到您的主类,然后将其添加到您的 actionlister

  String loginPasswordFeild2 = new String(loginPasswordFild2)

您必须事先定义密码,但它仍然可以使用。

于 2013-04-03T16:47:58.597 回答