Я играл с алгоритмом RSA и JFrame. Я изо всех сил пытаюсь понять, как печатать выходные данные в 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;
}
}
Добавить выходное поле JTextfield или JLabel или JTextArea в рамку и установить его текст. –
* «Кто-нибудь может показать мне, как печатать результаты в JFrame?» * Это действительно звучит так: «Может ли кто-то закончить этот код для меня?». Что вы пробовали? Какая проблема остановила вас? –
Я создал ярлык, но я не могу передать значения в метку, я могу установить твердое значение в метку, но это – Hayes121