日期:2014-05-20 浏览次数:21032 次
import java.awt.*;
import java.awt.event.*;
class YourWindow extends Frame implements ActionListener {
TextField text1, text2, text3, text4;
Button b1;
YourWindow(String s) {
super(s);
setLayout(new FlowLayout());
text1 = new TextField(20);
text2 = new TextField(20);
text3 = new TextField("输入明文", 20);
text4 = new TextField("输入密文", 20);
text3.setEditable(false);
text4.setEditable(false);
b1 = new Button("退出");
add(text1);
add(text2);
add(text3);
add(text4);
add(b1);
b1.addActionListener(this);
text1.addActionListener(this);
text2.addActionListener(this);
setBounds(200, 200, 550, 350);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
System.exit(0);
} else if (e.getSource() == text1) {
String s = text1.getText();
convertString(s);
char n[] = s.toCharArray();
int i;
int m = 0;
char k[] = new char[n.length];
for (i = 0; i < n.length; i++) {
m = (int) n[i];
if ((m + 1) <= 90 && (m + 1) >= 65) {
k[i] = (char) (m + 1);
} else if ((m + 1) > 90) {
k[i] = (char) (m + 1 - 26);
}
}
text2.setText(String.valueOf(k));
} else if (e.getSource() == text2) {
String ss = text2.getText();
convertString(ss);
char nn[] = ss.toCharArray();
int ii;
int mm = 0;
char kk[] = new char[nn.length];
for (ii = 0; ii < nn.length; ii++) {
mm = (int) nn[ii];
if ((mm - 1) > 0) {
kk[ii] = (char) (mm - 1);
} else if ((mm - 1) <= 0) {
kk[ii] = (char) (mm - 1 + 26);
}
text1.setText(String.valueOf(kk));
}
}
}
public static String convertString(String src) {
char[] array = src.toCharArray();
int temp = 0;
for (int i = 0; i < array.length; i++) {
temp = (int) array[i];
if (temp <= 90 && temp >= 65) { // array[i]为大写字母
array[i] = (char) (temp + 32);
}
// else if{}
}
return String.valueOf(array);
}
}
public class Acc {
public static void main(String args[]) {
YourWindow a = new YourWindow("解密");
}
}