📄 encoderdecoder.java
字号:
package encoder_decoder;
/* Imports java.nio package classes. */
import java.nio.*;
import java.nio.charset.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.*;
import java.nio.channels.FileChannel;
import java.nio.charset.UnsupportedCharsetException;
/* Imports java.io package classes. */
import java.io.*;
import java.io.IOException;
/* Imports javax.swing package classes. */
import javax.swing.*;
import javax.swing.JOptionPane;
/* Imports java.awt package classes. */
import java.awt.*;
/* Imports java.awt.event package classes. */
import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
/*
class EncoderDecoder - This class creates a GUI for the Encoder/Decoder application.
This class also provides the methods to encode the text and decode
the encoded text into a readable format.
Fields:
pan1 - Creates a panel that can contain a text box and two buttons.
titleLabel - Creates an Encoder/Decoder Application label.
selectLabel - Creates a select file label.
encodeLabel - Creates an encoded text label.
decodeLabel - Creates a decoded text label.
selectText - Creates a select file text field.
encodeArea - Creates an Encoded Text text area.
decodeArea - Creates a Decoded Text text area.
encodePane - Creates a scroll pane for the Encoded Text text area.
decodePane - Creates a scroll pane for the Decoded Text text area.
browseButton - Creates a Browse button.
encodeButton - Creates an Encode button.
selectButton - Creates a Select Encoding Scheme button.
resetButton - Creates a Reset button.
decodeButton - Creates a Decode button.
closeButton - Creates a Close button.
file_text - Represents a string that stores the contents of a file.
file - Represents a file.
jfc - Creates a file chooser dialog box.
fin - Creates a file input stream.
fchan - Creates a file channel.
buff_in - Stores the content of the opened files.
buff_out - Stores the content of the encoded file that needs to be further
encrypted in the hexadecimal format.
bbuf - Stores the encoded bytes.
cbuf - Stores the decoded bytes.
fsize - Contains the size of the file.
decoder - Creates a decoder that decodes the encoded text.
encoder - Creates an encoder that encodes the text.
charset - Creates a charset object.
encScheme - Represents an object of the EncodingSchemes class.
Methods:
keyTyped() - This method is invoked when an end user types any text in the Text
to Encode text area.
actionPerformed() - This method is invoked when an end user clicks the any
button of the Encoder/Decoder application.
open() - This method is invoked to open an existing text file that the end
user wants to encode.
encode() - This method is invoked to encode the text.
decode() - This method is invoked to decode the encoded text.
main() - This method creates the main window of the application and displays it.
*/
public class EncoderDecoder extends JDialog implements ActionListener, KeyListener
{
/* Declares the object of the JPanel class. */
JPanel pan1;
/* Declares the objects of the JLabel class. */
JLabel titleLabel;
JLabel textLabel;
JLabel selectLabel;
JLabel encodeLabel;
JLabel decodeLabel;
/* Declares the object of the JTextField class. */
JTextField selectText;
JTextField schemeText;
/* Declares the objects of the JTextArea class. */
JTextArea encodeArea;
JTextArea decodeArea;
JTextArea textArea;
/* Declares the objects of the JScrollPane class. */
JScrollPane encodePane;
JScrollPane decodePane;
JScrollPane textPane;
/* Declares the objects of the JButton class. */
JButton browseButton;
JButton encodeButton;
JButton selectButton;
JButton resetButton;
JButton decodeButton;
JButton closeButton;
/* Declares the object of the GridBagLayout class. */
GridBagLayout gbl;
/* Declares the object of the GridBagConstraints class. */
GridBagConstraints gbc;
/* Declares the objects of the String class. */
String str;
String file_text;
/* Declares the object of the File class. */
File file;
/* Declares the object of the JFileChooser class. */
JFileChooser jfc;
/* Declares the object of the FileInputStream class. */
FileInputStream fin;
/* Declares the object of the FileChannel class. */
FileChannel fchan;
/* Declares the objects of the ByteBuffer class. */
ByteBuffer buff_in;
ByteBuffer buff_out;
ByteBuffer bbuf;
/* Declares the object of the CharBuffer class. */
CharBuffer cbuf;
/* Declares the object of the CharsetDecoder class. */
CharsetDecoder decoder;
/* Declares the object of the CharsetEncoder class. */
CharsetEncoder encoder;
/* Declares the object of the Charset class. */
Charset charset;
/* Declares the object of the EncodingSchemes class. */
public EncodingSchemes encScheme;
long fsize;
/* Defines the default constructor of the EncoderDecoder class. */
public EncoderDecoder()
{
/* Sets the title of the Encoder/Decoder application. */
setTitle("Encoder / Decoder Application");
/* Sets the size of the Encoder/Decoder application. */
setSize(550,640);
/* Sets the visibility of the Encoder/Decoder application to true. */
setVisible(true);
/* Sets the re-sizability of the Encoder/Decoder application to false. */
setResizable(false);
/* Initializes the object of the GridBagLayout class. */
gbl = new GridBagLayout();
/* Sets the Layout */
getContentPane().setLayout(gbl);
/* Creates an object of the GridBagConstraints class. */
gbc = new GridBagConstraints();
/* Initializes the titleLabel object and adds it to the 0, 0, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 5;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
titleLabel = new JLabel(" Encoder/Decoder Application");
titleLabel.setFont(new Font("Verdana", Font.BOLD, 20));
getContentPane().add(titleLabel, gbc);
/* Initializes and adds a separator to the 0, 1, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the selectLabel object and adds it to the 0, 2, 1, 1 position with WEST alignment. */
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
selectLabel = new JLabel("Select File to Encode:");
getContentPane().add(selectLabel, gbc);
/* Initializes and adds a separator to the 0, 3, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the selectText text field and adds it to the 0, 4, 1, 1 position with WEST alignment. */
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
selectText = new JTextField(30);
selectText.setFont(new Font("幼圆", Font.PLAIN, 12));
getContentPane().add(selectText, gbc);
/* Initializes the browse object and adds it to the 1, 4, 1, 1 position with CENTER alignment. */
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
browseButton = new JButton(" Browse ");
browseButton.addActionListener(this);
getContentPane().add(browseButton, gbc);
/* Initializes the reset object and adds it to the 2, 4, 1, 1 position with EAST alignment. */
gbc.gridx = 2;
gbc.gridy = 4;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.EAST;
resetButton = new JButton(" Reset ");
resetButton.addActionListener(this);
getContentPane().add(resetButton, gbc);
/* Initializes and adds a separator to the 0, 5, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the selectText and adds it to the 0, 6, 1, 1 position with WEST alignment. */
gbc.gridx = 0;
gbc.gridy = 6;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
schemeText = new JTextField(15);
schemeText.setEditable(false);
schemeText.setText(EncodingSchemes.encodingScheme);
schemeText.setFont(new Font("Verdana", Font.PLAIN, 12));
getContentPane().add(schemeText, gbc);
/* Initializes the selectButton object and adds it to the 1, 6, 2, 1 position with WEST alignment. */
gbc.gridx = 1;
gbc.gridy = 6;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
selectButton = new JButton(" Select Encoding Scheme ");
selectButton.addActionListener(this);
getContentPane().add(selectButton, gbc);
/* Initializes and adds a separator to the 0, 7, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 7;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the encodeLabel object and adds it to the 0, 8, 1, 1 position with WEST alignment. */
gbc.gridx = 0;
gbc.gridy = 8;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
textLabel = new JLabel("Text to Encode:");
getContentPane().add(textLabel, gbc);
/* Initializes and adds a separator to the 0, 9, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 9;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the textArea and textPane objects. Next, this textArea adds to the textPane
and adds it to the 0, 10, 3, 1 position with WEST alignment. */
gbc.gridx = 0;
gbc.gridy = 10;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
textArea = new JTextArea(7, 44);
textArea.addKeyListener(this);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(new Font("Verdana", Font.PLAIN, 12));
textPane = new JScrollPane(textArea);
getContentPane().add(textPane, gbc);
/* Initializes and adds a separator to the 0, 11, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 11;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the encodeButton object and adds to it to the 1, 12, 2, 1 position with WEST alignment. */
gbc.gridx = 1;
gbc.gridy = 12;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.EAST;
encodeButton = new JButton(" Encode ");
encodeButton.setEnabled(false);
encodeButton.addActionListener(this);
getContentPane().add(encodeButton, gbc);
/* Initializes and adds a separator to the 0, 13, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 13;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the encodeLabel object and adds it to the 0, 14, 1, 1 position with WEST alignment. */
gbc.gridx = 0;
gbc.gridy = 14;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
encodeLabel = new JLabel("Encoded Text:");
getContentPane().add(encodeLabel, gbc);
/* Initializes and adds a separator to the 0, 15, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 15;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the encodeArea and encodePane objects. Next, this encodeArea adds to the
encodePane and adds it to the 0, 16, 3, 1 position with WEST alignment. */
gbc.gridx = 0;
gbc.gridy = 16;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
encodeArea = new JTextArea(7, 44);
encodeArea.setEditable(false);
encodeArea.addKeyListener(this);
encodeArea.setLineWrap(true);
encodeArea.setWrapStyleWord(true);
encodeArea.setFont(new Font("Verdana", Font.PLAIN, 12));
encodePane = new JScrollPane(encodeArea);
getContentPane().add(encodePane, gbc);
/* Initializes and adds a separator to the 0, 17, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 17;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the decodeButton object and adds to it to the 0, 18, 2, 1 position with EAST alignment. */
gbc.gridx = 1;
gbc.gridy = 18;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.EAST;
decodeButton = new JButton(" Decode ");
decodeButton.setEnabled(false);
decodeButton.addActionListener(this);
getContentPane().add(decodeButton, gbc);
/* Initializes and adds a separator to the 0, 19, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 19;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the decodeLabel object and adds it to the 0, 20, 1, 1 position with WEST alignment. */
gbc.gridx = 0;
gbc.gridy = 20;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.WEST;
decodeLabel = new JLabel("Decoded Text:");
getContentPane().add(decodeLabel, gbc);
/* Initializes and adds a separator to the 0, 21, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 21;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the decodeArea and decodePane objects. Next, this decodeArea adds to the
decodePane and adds it to the 0, 22, 3, 1 position with WEST alignment. */
gbc.gridx = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -