我正在编写一个加密程序,让用户输入一条消息并将其编码为矩阵,或者解码一组矩阵。我已经完成了核心,并决定它将作为一系列JFrame弹出窗口更加用户友好。我讨厌程序的最后一个问题,那就是解码方法。它应该工作的方式是,用户输入构成编码消息的矩阵的数量,并且用户输入在一组3个输入中循环很多次,以便通过矩阵解码消息矩阵。我遇到的问题是我有点迷失在如何循环我的用户输入JFrame,就像我用我的示例代码。这是用户输入多少矩阵的JFrame:
package matrix_with_GUI;
import javax.swing.* ;
import java.awt.event.* ;
import java.awt.* ;
public class DecodeMenu2 extends JFrame implements ActionListener{
private JButton action1 = new JButton ("");
private JPanel pane = new JPanel();
private JLabel lbl;
private TextField slot1 = new TextField(2);
private double[][] junk = new double[3][3];
private String message = "";
public void DecodeMenu2(double[][] code){
junk = code;
JPanel pane=new JPanel();
setTitle ("Start Menu") ;
JFrame frame = new JFrame("");
setVisible(true);
setSize (600, 150) ;
setLocationRelativeTo (null) ;
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;
action1 = new JButton("Continue");
lbl = new JLabel (" How Many Matrices make up your coded message? (Every set of numbers in brackets is 1 matrix) ");
setLayout(new FlowLayout());
add (lbl) ;
add(slot1);
add(action1, BorderLayout.CENTER);
action1.addActionListener (this);
}
@Override
public void actionPerformed(ActionEvent e) {
int size = Integer.parseInt(slot1.getText());
DecodeCore x = new DecodeCore();
for(int count = size; count > 0; count--){
x.DecodeCore(junk);
message += x.getMessage();
}
DecodePrint y = new DecodePrint();
y.DecodePrint(message);
this.dispose();
}
}
以下是每次单击“继续”按钮时应循环的代码,直到达到“大小”:
package matrix_with_GUI;
import javax.swing.* ;
import java.awt.event.* ;
import java.awt.* ;
public class DecodeCore extends JFrame implements ActionListener{
private JButton action1 = new JButton ("");
private JPanel pane = new JPanel();
private JLabel lbl;
private TextField slot1 = new TextField(5);
private TextField slot2 = new TextField(5);
private TextField slot3 = new TextField(5);
private String message = "";
private double[][] inverseCode = new double[3][3];
public void DecodeCore(double[][] code){
inverseCode = code;
JPanel pane=new JPanel();
setTitle ("Start Menu") ;
JFrame frame = new JFrame("");
setVisible(true);
setSize (380, 150) ;
setLocationRelativeTo (null) ;
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;
action1 = new JButton("Continue");
lbl = new JLabel (" Begin entering your Coded Message, one matrix at a time. ");
setLayout(new FlowLayout());
add (lbl) ;
add(slot1);
add(slot2);
add(slot3);
add(action1, BorderLayout.CENTER);
action1.addActionListener (this);
}
@Override
public void actionPerformed(ActionEvent e) {
int sum = 0, count = 0;
int[] matrix = new int[3];
int[] entry = new int[3];
entry[0] = Integer.parseInt(slot1.getText());
entry[1] = Integer.parseInt(slot2.getText());
entry[3] = Integer.parseInt(slot3.getText());
for (int col = 0 ; col < 3 ; col++ ){
for (int row = 0 ; row < 3 ; row++ ){
sum += entry[row]*inverseCode[row][col];
}
matrix[col] = sum;
sum = 0;
}
while(count < 3){
matrix[count] = Math.round(matrix[count] + 32);
message += Character.toString((char) matrix[count]);
count++;
}
count = 0;
}
public String getMessage(){
return message;
}
}
很抱歉这篇长篇文章。