Você está na página 1de 3

package caesarcipher;

import
import
import
public

javax.swing.*;
java.awt.*;
java.awt.event.*;
class caesarcipher extends JFrame implements ActionListener {

private static
private static
private static
private static
private static
private static
private static
1, 25, 1) );
private static
private static
private static
private static
private static

JLabel msgLabel = new JLabel("Message: ");


JLabel keyLabel = new JLabel("Key: ");
JLabel actionLabel = new JLabel("Action: ");
JLabel resultLabel = new JLabel("Result: ");
JTextField msgTextField = new JTextField(20);
JTextField resultTextField = new JTextField(20);
JSpinner keySpinner = new JSpinner( new SpinnerNumberModel(3,
JRadioButton encryptRadio = new JRadioButton("Encrypt");
JRadioButton decryptRadio = new JRadioButton("Decrypt");
JButton actionButton = new JButton("Encrypt Message");
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();

public static void main(String[] args) {


new caesarcipher();
}
public caesarcipher() {
this.setSize(310, 192);
this.setTitle("Caesar Cipher");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
panel.setLayout(new GridBagLayout());
addComponent(panel, msgLabel, 0, 0, 1, 1, GridBagConstraints.LINE_START)
;
addComponent(panel, msgTextField, 1, 0, 2, 1, GridBagConstraints.LINE_ST
ART);
addComponent(panel, keyLabel, 0, 1, 1, 1, GridBagConstraints.LINE_START)
;
addComponent(panel, keySpinner, 1, 1, 1, 1, GridBagConstraints.LINE_STAR
T);
addComponent(panel, actionLabel, 0, 2, 1, 1, GridBagConstraints.LINE_STA
RT);
group.add(encryptRadio);
group.add(decryptRadio);
addComponent(panel, encryptRadio, 1, 2, 1, 1, GridBagConstraints.LINE_ST
ART);
addComponent(panel, decryptRadio, 2, 2, 1, 1, GridBagConstraints.LINE_ST
ART);
encryptRadio.setSelected(true);
encryptRadio.addActionListener(this);
decryptRadio.addActionListener(this);
addComponent(panel, resultLabel, 0, 3, 1, 1, GridBagConstraints.LINE_STA
RT);
addComponent(panel, resultTextField, 1, 3, 2, 1, GridBagConstraints.LINE
_START);

resultTextField.setEditable(false);
addComponent(panel, actionButton, 1, 4, 1, 1, GridBagConstraints.CENTER)
;
actionButton.addActionListener(this);
this.add(panel);
this.setVisible(true);
}
private void addComponent(JPanel p, JComponent c, int x, int y, int width, i
nt height, int align) {
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new Insets(5, 5, 5, 5);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}
private void encryptMessage(String msg, int k) {
String result = "";
resultTextField.setText("");
for (int i = 0; i < msg.length(); i++)
result += encryptChar(msg.charAt(i), k);
resultTextField.setText(result);
}
private char encryptChar(char c, int k) {
if (Character.isLetter(c))
return (char) ('A' + (c - 'A' + k) % 26);
else
return c;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == encryptRadio)
actionButton.setText("Encrypt Message");
if (e.getSource() == decryptRadio)
actionButton.setText("Decrypt Message");
if (e.getSource() == actionButton) {
String str = msgTextField.getText();
int k = (Integer) keySpinner.getValue();
int key = 0;
String message = "";
if (str.equals("")) {
JOptionPane.showMessageDialog(null, "Please enter a message!", "Error!",
JOptionPane.ERROR_MESSAGE);
msgTextField.requestFocus();
return;
}

message = str.toUpperCase();
if (encryptRadio.isSelected())
key = k;
else
key = 26 - k;
encryptMessage(message, key);
}
}
}

Você também pode gostar