【发布时间】:2014-12-11 06:33:47
【问题描述】:
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.DefaultFormatter;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
public class CipherGUI{
public static void main(String args[]){
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {}
JFrame cipherGUIFrame = new CipherGUIFrame();
cipherGUIFrame.setVisible(true);
}
}
class CipherGUIFrame extends JFrame {
public boolean decrypt=true;
public CipherGUIFrame() {
super("Caesar Cipher GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 600);
JTextArea area1 = new JTextArea();
JTextArea area2 = new JTextArea();
JSpinner myspinner=new JSpinner();
JPanel mainframe = new JPanel();
JToggleButton mybutton=new JToggleButton("ENCRYPT");
mainframe.setLayout(new BoxLayout(mainframe, BoxLayout.Y_AXIS));
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
p1.setBorder(BorderFactory.createTitledBorder("Cleartext"));
p2.setBorder(BorderFactory.createTitledBorder("KEY"));
p3.setLayout(new BoxLayout(p3, BoxLayout.X_AXIS));
p3.setBorder(BorderFactory.createTitledBorder("Ciphertext"));
p1.add(area1);
p2.add(myspinner);
p2.add(mybutton);
p3.add(area2);
mainframe.add(p1);
mainframe.add(p2);
mainframe.add(p3);
this.add(mainframe);
ActionListener togglelistener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
decrypt=selected;
System.out.println("Selected (True or False?): " + selected + "\n");
}
};
mybutton.addActionListener(togglelistener);
//the following chunk of code modifies the component to update when spinner is changed automatically
JComponent comp = myspinner.getEditor();
JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);
myspinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("Key spinner changed to: " + myspinner.getValue());
}
});
DocumentListener documentListener = new DocumentListener() {
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
DocumentEvent.EventType type = documentEvent.getType();
String typeString = null;
if (type.equals(DocumentEvent.EventType.CHANGE)) {
typeString = "(CHANGED KEY) ";
} else if (type.equals(DocumentEvent.EventType.INSERT)) {
typeString = "(PRESSED KEY) ";
} else if (type.equals(DocumentEvent.EventType.REMOVE)) {
typeString = "(DELETED KEY) ";
}
System.out.print("Type : " + typeString);
Document source = documentEvent.getDocument();
int length = source.getLength();
System.out.println("Current size: " + length);
String contents=source.getText(0, length);
System.out.println(contents);
}
};
area1.getDocument().addDocumentListener(documentListener);
area2.getDocument().addDocumentListener(documentListener);
}
public String decipher(String istring, int key){
String decrypted = "";
for(int i=0; i<istring.length(); i++){
int c = istring.charAt(i);
if (Character.isUpperCase(c)){
c = c - (key % 26);
if (c < 'A')
c = c + 26;
}
else if (Character.isLowerCase(c)){
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return(decrypted);
}
public String encipher(String istring, int key){
String encrypted = "";
for(int i=0; i<istring.length(); i++){
int c = istring.charAt(i);
if (Character.isUpperCase(c)){
c = c - (key % 26);
if (c < 'A')
c = c + 26;
}
else if (Character.isLowerCase(c)){
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
encrypted += (char) c;
}
return (encrypted);
}
}
这是我当前的代码。如您所见,我使用了两个文本区域——一个用于加密,另一个用于解密。我希望一个在另一个更新后立即更新,反之亦然。我遇到问题(并卡在这里调试)的行是
String contents=source.getText(0, length);
线。我正在努力做到这一点,以便在更改文本区域的内容时可以读取它,以便能够通过加密(或解密)方法将其发送并将其输出到另一个文本区域(我知道该怎么做)。 我遇到的问题是在 TextArea 更改时获取它的内容。有任何想法吗? (忽略 KEY CHANGED 输出,那些是用于测试的......)
【问题讨论】:
-
1- 谨防在
DocumentListener中修改Documents,Document不喜欢这样。 2- 从DocumentEvent中提取有问题的Document并从中提取文本...
标签: java jtextarea documentlistener