在 JFrame 中显示值

Showing Values in JFrame

我一直在研究RSA算法和JFrame。我正在努力弄清楚如何在 JFrame 中而不是在控制台中打印输出。

程序运行时,用户输入一个字符串,点击提交,然后加密解密,结果打印在控制台。

谁能告诉我如何在 JFrame 中打印结果?

工作代码

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.math.BigInteger;
    import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.*;
    import static javax.swing.JFrame.EXIT_ON_CLOSE;
    import static jdk.nashorn.internal.objects.NativeRegExp.test;

        public class Test extends JFrame {

            //String userWord;
            JTextField userInput = new JTextField(10);
            JButton submit = new JButton("Submit");
            JLabel labelMessage = new JLabel();
            JLabel labelEncrypted = new JLabel();
            JLabel labelDecrypted = new JLabel();

            public static final BigInteger TWO_FIVE_SIX = new BigInteger("256");
            // P and Q are our two primes we use to generate the key pair
            public static final BigInteger P = new BigInteger("61");
            public static final BigInteger Q = new BigInteger("53");
            public static final BigInteger N = P.multiply(Q);
            public static final BigInteger Z = P.subtract(BigInteger.ONE).multiply(Q.subtract(BigInteger.ONE));
            // (N,E) and (N,D) are our public and private keys
            private static final BigInteger E = new BigInteger("17");
            public static final BigInteger D = new BigInteger("2753");

            public Test() {
                super("Test");
                JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
                setSize(300, 500);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                //setLocationRelativeTo(null); // This center the window on the screen
                submit.addActionListener( (e)-> {submitAction();
                    });
                centerPanel.add(userInput);
                JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
                southPanel.add(submit);\
                Box theBox = Box.createVerticalBox();
                theBox.add(Box.createVerticalStrut(5));
                theBox.add(centerPanel);
                theBox.add(Box.createVerticalStrut(10));
                theBox.add(southPanel);
                theBox.add(labelMessage);
                theBox.add(labelEncrypted);
                theBox.add(labelDecrypted);
                add(theBox);


            }

            private void submitAction() {
                // You can do some validation here before assign the text to the variable 
                String message = userInput.getText();

                String encrypted = encrypt(message);
                String decrypted = decrypt(encrypted);

                labelMessage(message);
                labelEncrypted(encrypted);
                labelDecrypted(decrypted);
            }

            public static void main(String[] args) {
                new Test().setVisible(true);
            }

            public void labelMessage(String s){
                labelMessage.setText("Message: " + s);
            }

            public void labelEncrypted(String s){
                labelEncrypted.setText("Encrypted:"+ s);
            }

            public void labelDecrypted(String s){
                labelDecrypted.setText("Decrypted:" + s);
            }

            public static BigInteger pow(BigInteger base, BigInteger exponent) {
                BigInteger result = BigInteger.ONE;
                for (BigInteger i = new BigInteger("0"); !i.equals(exponent); i = i.add(BigInteger.ONE)) {
                    result = result.multiply(base);
                }
                return result;
            }

            public static String encrypt(String s) {
                String result = "";
                for (int i = 0; i < s.length(); i++) {
                    BigInteger b = new BigInteger("" + (int)(s.charAt(i)));
                    String r = pow(b, E).mod(N).toString();
                    while (r.length() < 4) {
                        r = "0" + r;
                    }
                    result += r;
                }
                return result;
            }

            public static String decrypt(String s) {
                String result = "";
                for (int i = 0; i < s.length() / 4; i++) {
                    BigInteger b = new BigInteger(s.substring(i * 4, (i + 1) * 4));
                    result += (char)(pow(b, D).mod(N).intValue());
                }
                return result;
                }
            }

在您的 JLabel 上尝试 .setText。我做了一个 GUI here.

Inside method submitAction()
Replace all System.out.println() with
JOptionPane.showMessageDialog();
For example 
System.out.println("HELLO");
JOptionPane.showMessageDialog(null,"HELLO");


private void submit action(){
String message = userInput.getText();
String encrypted = encrypt(message);
String decrypted = decrypt(encrypted);
JOptionPane.showMessageDialog(null,Message to encrypt/decrypt:  
+message+"\nEncrypted:"+encrypted+"\nDecrypted:"+decrypted+
"Decrypted matches original: " + decrypted.equals(message));
}
Edit "Message to encrypt/decrypt:" instead of Message to...."