0

Main クラスとクラスの 2 つのクラスを持つ 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つの方法は次のとおりです。

  1. appGUI を「メイン」クラスの内部クラスにします (より適切な名前を付けてください。今は Test と呼びましょう)。

  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 に答える
-1

パスワード文字列を作成するときは、変数を追加する必要があるため、後で文字列 = 上記の変数を定義できます。元。

String password = new String("Somthing");

それから

 appGUI gui = new appGUI();

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

 }

それをメインクラスに追加してから、これをアクションリストに追加します

  String loginPasswordFeild2 = new String(loginPasswordFild2)

事前にパスワードを定義する必要がありますが、それでも機能します。

于 2013-04-03T16:47:58.597 に答える