Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JFrame中显示值_Java_Swing_Encryption_Rsa - Fatal编程技术网

Java 在JFrame中显示值

Java 在JFrame中显示值,java,swing,encryption,rsa,Java,Swing,Encryption,Rsa,我一直在玩RSA算法和JFrame。我正在努力弄清楚如何在JFrame而不是控制台中打印输出 当程序运行时,用户输入一个字符串并单击submit,然后对其进行加密和解密,并在控制台中打印结果 有人能告诉我如何在JFrame中打印结果吗 工作代码 import java.awt.BorderLayout; import java.awt.FlowLayout; import java.math.BigInteger; import javax.swing.Box;

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

当程序运行时,用户输入一个字符串并单击submit,然后对其进行加密和解密,并在控制台中打印结果

有人能告诉我如何在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;
                }
            }
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入java.math.biginger;
导入javax.swing.Box;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.SwingConstants;
导入javax.swing.*;
在关闭时导入静态javax.swing.JFrame.EXIT_;
导入静态jdk.nashorn.internal.objects.NativeRegExp.test;
公共类测试扩展了JFrame{
//字符串用户字;
JTextField userInput=新的JTextField(10);
JButton submit=新JButton(“提交”);
JLabel labelMessage=新的JLabel();
JLabel labelEncrypted=新JLabel();
JLabel labeldcrypted=新的JLabel();
公共静态最终BigInteger 2_5_6=新的BigInteger(“256”);
//P和Q是我们用来生成密钥对的两个素数
公共静态最终BigInteger P=新的BigInteger(“61”);
公共静态最终BigInteger Q=新的BigInteger(“53”);
公共静态最终大整数N=P乘以(Q);
公共静态final BigInteger Z=P.subtract(BigInteger.ONE)。乘法(Q.subtract(BigInteger.ONE));
//(N,E)和(N,D)是我们的公钥和私钥
私有静态最终BigInteger E=新的BigInteger(“17”);
公共静态最终BigInteger D=新的BigInteger(“2753”);
公开考试(){
超级(“测试”);
JPanel中心面板=新JPanel(新的FlowLayout(FlowLayout.CENTER,15,15));
设置大小(300500);
setDefaultCloseOperation(关闭时退出);
//setLocationRelativeTo(null);//此选项将使窗口在屏幕上居中
submit.addActionListener((e)->{submitAction();
});
添加(用户输入);
JPanel-southPanel=newjpanel(newflowlayout(FlowLayout.CENTER,15,15));
添加(提交)\
Box theBox=Box.createVerticalBox();
添加(Box.createverticalstrust(5));
添加(中心面板);
添加(Box.createverticalstrust(10));
添加(南面板);
添加(标签信息);
添加(标签加密);
添加(标签加密);
添加(方框);
}
私有无效提交(){
//在将文本分配给变量之前,可以在此处进行一些验证
String message=userInput.getText();
字符串加密=加密(消息);
字符串解密=解密(加密);
标签信息(信息);
标签加密(加密);
标签已加密(解密);
}
公共静态void main(字符串[]args){
新测试().setVisible(真);
}
公共无效标签消息(字符串s){
setText(“消息:+s”);
}
公共空白标签加密(字符串s){
labelEncrypted.setText(“加密:+s”);
}
公共无效标签已加密(字符串s){
labelDecrypted.setText(“解密:+s”);
}
公共静态BigInteger pow(BigInteger基,BigInteger指数){
BigInteger结果=BigInteger.ONE;
对于(biginger i=new biginger(“0”);!i.equals(指数);i=i.add(biginger.ONE)){
结果=结果。乘法(基数);
}
返回结果;
}
公共静态字符串加密(字符串s){
字符串结果=”;
对于(int i=0;i
在JLabel上尝试
.setText
。我已经做了一个GUI。

在框架中添加一个输出JTextfield或JLabel或JTextArea,并设置其文本。“有人能告诉我如何在JFrame中打印结果吗?”听起来真像是“有人能帮我完成这段代码吗?”。你试过什么?什么问题阻止了你?我已经创建了一个标签,但我似乎无法将值传递到标签中,我可以在标签中设置一个硬编码值,但这就是我更新了代码,以显示问题我有一点更好,我有一个被调用的函数,调用时接受一个字符串,这个函数打印标签,但这就是我遇到的问题,因为它不工作,您的代码无法编译。你读过编译器的错误信息了吗。你不明白的是什么。请将你的答案代码和你的答案一起发布在这里。@HovercraftFullOfEels这是一个不同的项目,它将完全脱离上下文和篇幅。我只是
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...."