《java轻松学》凯撒加密

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;


public class SecretMessageGUI extends JFrame {
    private JTextField txtKey;
    private JTextArea txtIn;
    private JTextArea txtOut;
    private JSlider slider;

    public String encode(String message, int keyVal){
        String output = "";

        char key = (char)keyVal;
        for ( int x = 0; x <= message.length()-1; x++)
        {
            char input = message.charAt(x);
            if(input >= 'A' && input <= 'Z')
            {
                input += key;
                if(input > 'Z')
                    input -= 26;
                if(input < 'A')
                    input += 26;
            }
            else if(input >= 'a' && input <= 'z')
            {
                input += key;
                if(input > 'z')
                    input -= 26;
                if(input < 'a')
                    input += 26;
            }
            else if(input >= '0' && input <= '9')
            {
                input += (keyVal % 10);
                if(input > '9')
                    input -= 10;
                if(input < '0')
                    input += 10;
            }
            output += input;
        }

        return output;
    }

    public SecretMessageGUI() {
        getContentPane().setBackground(Color.GREEN);
        setTitle("SecretMessage App");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        txtIn = new JTextArea();
        txtIn.setLineWrap(true);
        txtIn.setWrapStyleWord(true);
        txtIn.setFont(new Font("Lucida Console", Font.BOLD, 18));
        txtIn.setBounds(14, 13, 554, 136);
        getContentPane().add(txtIn);

        txtOut = new JTextArea();
        txtOut.setLineWrap(true);
        txtOut.setWrapStyleWord(true);
        txtOut.setFont(new Font("Lucida Console", Font.BOLD, 18));
        txtOut.setBounds(14, 203, 554, 136);
        getContentPane().add(txtOut);

        txtKey = new JTextField();
        txtKey.setBounds(309, 164, 44, 24);
        getContentPane().add(txtKey);
        txtKey.setColumns(10);

        JLabel lblNewLabel = new JLabel("Key:");
        lblNewLabel.setBounds(263, 167, 32, 18);
        getContentPane().add(lblNewLabel);

        JButton btnNewButton = new JButton("Encode/Decode");
        btnNewButton.setBackground(Color.GREEN);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try
                {
                    String message = txtIn.getText();
                    int key = Integer.parseInt(txtKey.getText());
                    String output = encode(message, key);
                    txtOut.setText(output);
                }catch(Exception ex){
                    JOptionPane.showMessageDialog(null,
                            "Please enter  a whole number value for the encryption key.");
                    txtKey.requestFocus();
                    txtKey.selectAll();
                }
            }
        });
        btnNewButton.setBounds(391, 163, 154, 27);
        getContentPane().add(btnNewButton);

        slider = new JSlider();
        slider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                txtKey.setText(""+slider.getValue());
                String message = txtIn.getText();
                int key = Integer.parseInt(txtKey.getText());
                String output = encode(message, key);
                txtOut.setText(output);

            }
        });
        slider.setMajorTickSpacing(13);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setMinimum(-26);
        slider.setMaximum(26);
        slider.setPaintLabels(true);
        slider.setBackground(Color.CYAN);
        slider.setBounds(42, 153, 200, 45);
        getContentPane().add(slider);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SecretMessageGUI theApp = new SecretMessageGUI();
        theApp.setSize(new java.awt.Dimension(600,400));
        theApp.setVisible(true);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值