我正在创建一个java程序,当你在textarea上输入内容时,另一个textarea会自动加密字符。我正在使用Caesar的密码。
示例:我在第一个textarea上键入QWERTY。当我输入Q时,第二个文本区域自动显示结果N,然后是W,它将显示T等等。
到目前为止,这是我的代码。它看起来像一个记事本。保存文件时,它将自动转换为Caesar的密码。我想改变它的外观,以便用户可以在他/她打字时看到该消息的样子。
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
public class LeftShift implements ActionListener
{
public static LeftShift f = new LeftShift();
Container content;
JFrame frame;
JTextArea txtarea = new JTextArea("");
JScrollPane scroll = new JScrollPane();
JPanel panel = new JPanel();
JMenuBar mb = new JMenuBar();
JMenu file= new JMenu("File");
JMenu about= new JMenu("About");
JMenuItem new1 = new JMenuItem("New");
JMenuItem open = new JMenuItem("Open");
JMenuItem save= new JMenuItem("Save");
JMenuItem saveas= new JMenuItem("Save As");
JMenuItem exit= new JMenuItem("Exit");
JMenuItem author= new JMenuItem("Author");
File fileopened = null;
public void mainFrame()
{
frame = new JFrame("Untitled - Notepad");
content = frame.getContentPane();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scroll = new JScrollPane(txtarea);
frame.add(scroll);
frame.setJMenuBar(mb);
mb.add(file);
mb.add(about);
file.add(new1);
file.add(open);
file.add(save);
file.add(saveas);
file.add(exit);
about.add(author);
new1.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
saveas.addActionListener(this);
exit.addActionListener(this);
author.addActionListener(this);
}
public static void main(String args[])
{
f.mainFrame();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==new1)
{
txtarea.setText("");
frame.setTitle("Untitled - Notepad");
fileopened = null;
}
else if(e.getSource()==open)
{
JFileChooser chooser = new JFileChooser();
int returnvalue = chooser.showOpenDialog(null);
if(returnvalue==chooser.APPROVE_OPTION)
{
fileopened = chooser.getSelectedFile();
frame.setTitle(fileopened.getName() + " - Notepad");
String s = new String("");
String str = new String("");
try
{
Scanner scanner = new Scanner(fileopened);
while(scanner.hasNext())
{
s = scanner.nextLine();
str += Decrypt(s) + "\n";
}
txtarea.setText(str);
}
catch(FileNotFoundException fnfe)
{}
}
}
else if(e.getSource()==save)
{
if(fileopened != null)
{
String s = new String("");
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(fileopened,false));
Scanner scanner = new Scanner(txtarea.getText());
while(scanner.hasNext())
{
s = scanner.nextLine();
bw.write(Encrypt(s));
bw.newLine();
}
bw.close();
frame.setTitle(fileopened.getName() + " - Notepad");
}
catch(IOException ioe)
{}
}
else
{
JFileChooser chooser = new JFileChooser();
int returnvalue = chooser.showSaveDialog(null);
if(returnvalue==chooser.APPROVE_OPTION)
{
fileopened = chooser.getSelectedFile();
String s = new String("");
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(fileopened,false));
Scanner scanner = new Scanner(txtarea.getText());
while(scanner.hasNext())
{
s = scanner.nextLine();
bw.write(Encrypt(s));
bw.newLine();
}
bw.close();
frame.setTitle(fileopened.getName() + " - Notepad");
}
catch(IOException ioe)
{}
}
}
}
else if(e.getSource()==saveas)
{
JFileChooser chooser = new JFileChooser();
int returnvalue = chooser.showOpenDialog(null);
if(returnvalue==chooser.APPROVE_OPTION)
{
fileopened = chooser.getSelectedFile();
String s = new String("");
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(fileopened,false));
Scanner scanner = new Scanner(txtarea.getText());
while(scanner.hasNext())
{
s = scanner.nextLine();
bw.write(Encrypt(s));
bw.newLine();
}
bw.close();
frame.setTitle(fileopened.getName() + " - Notepad");
}
catch(IOException ioe)
{
}
}
}
else if(e.getSource()==exit)
{
System.exit(0);
}
else if(e.getSource()==author)
{
JOptionPane.showMessageDialog(null,"Caesar Encipher and Decipher LeftShift");
}
}
public String Encrypt(String s)
{
String str = new String();
for ( int i = 0; i < s.length(); ++i )
{
char c = s.charAt(i);
int j = (int) c;
if(j>=65 && j<=67) //Letter ABC
{
switch(j)
{
case 65:
j=88;
break;
case 66:
j=89;
break;
case 67:
j=90;
break;
}
}
else if(j>=97 && j<=99) //Letter abc
{
switch(j)
{
case 97:
j=120;
break;
case 98:
j=121;
break;
case 99:
j=122;
break;
}
}
else if(j>=68 && j<=90) //Letter D-Z
{
j-=3;
}
else if(j>=100 && j<=122) //Letter d-z
{
j-=3;
}
c = (char) j;
str += c;
}
return str;
}
public String Decrypt(String s)
{
String str = new String();
for ( int i = 0; i < s.length(); ++i )
{
char c = s.charAt(i);
int j = (int) c;
if(j>=88 && j<=90) //Letter XYZ
{
switch(j)
{
case 88:
j=65;
break;
case 89:
j=66;
break;
case 90:
j=67;
break;
}
}
else if(j>=120 && j<=122) //Letter xyz
{
switch(j)
{
case 120:
j=97;
break;
case 121:
j=98;
break;
case 122:
j=99;
break;
}
}
else if(j>=65 && j<=87) //Letter A-W
{
j+=3;
}
else if(j>=97 && j<=119) //Letter a-w
{
j+=3;
}
c = (char) j;
str += c;
}
return str;
}
}
答案 0 :(得分:0)
将DocumentListener
添加到第一个文本区域。当事件触发时,您可以翻译并更新第二个文本区域。
有关详细信息,请参阅How to Write a DocumentListener。
不要忘记处理文本区域中的文本删除。