⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 Huffman编码的java实现。含实验报告。
💻 JAVA
字号:
package gilyou;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.JCheckBox;



public class Main extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;  //  @jve:decl-index=0:visual-constraint="101,79"

	static JButton encodeButton = null;

	static JButton decodeButton = null;

	static JButton exitButton = null;
	
	static JButton aboutButton = null;
	
	private JLabel welcomeLabel = null;
	
	private String aboutMe = "\n" +  "Author:  古流枫叶" + "\n\n" + "   本编码译码器为通用编码器" + "\n\n\n   版权所有,请勿使用盗版" + "\n\n\n  CopyRight Gilyou All Rights Reserved";  //  @jve:decl-index=0:

	static JTextArea infoTextArea = null;
	
	static JButton backButton = null;
	
	static JCheckBox binCheckBox = null;
	
	private boolean outBin = false;
	/**
	 * This is the default constructor
	 */
	public Main() {
		super();
		initialize();
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				onExitClicked();
			}
		});
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width-getSize().width)/4,(Toolkit.getDefaultToolkit().getScreenSize().height-getSize().height)/4,577, 385);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setSize(new Dimension(604, 435));
		this.setMaximumSize(new Dimension(7, 26));	
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Gilyou System");
		this.setVisible(true);
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			welcomeLabel = new JLabel();
			welcomeLabel.setBounds(new Rectangle(25, 27, 543, 46));
			welcomeLabel.setFont(new Font("\u5b8b\u4f53", Font.BOLD, 14));
			welcomeLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			welcomeLabel.setHorizontalTextPosition(SwingConstants.CENTER);
			welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);
			welcomeLabel.setForeground(Color.blue);
			welcomeLabel.setToolTipText("<html><font color=green>Gilyou作品  *^_^* </font></html>");
			welcomeLabel.setBackground(Color.green);
			welcomeLabel.setText("欢迎使用Gilyou系列简易Huffman编码译码器");
			welcomeLabel.setBorder(new EtchedBorder(9));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(Color.magenta);
			jContentPane.add(getEncodeButton(), null);
			jContentPane.add(getDecodeButton(), null);
			jContentPane.add(getExitButton(), null);
			jContentPane.add(getAboutButton(), null);
			jContentPane.add(welcomeLabel, null);
			jContentPane.add(getInfoTextArea(), null);
			jContentPane.add(getBackButton(), null);
			jContentPane.add(getBinCheckBox(), null);
		}
		return jContentPane;
	}
	/**
	 * This method initializes encodeButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getEncodeButton() {
		if (encodeButton == null) {
			encodeButton = new JButton();
			encodeButton.setMnemonic('E');
			encodeButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK,false),"onEncode");
			encodeButton.getActionMap().put("onEncode", onEncode);
			encodeButton.setBounds(new Rectangle(24, 346, 77, 29));
			encodeButton.setText("编码(E)");
			encodeButton.setToolTipText("快捷键E");
			encodeButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				    onEncodeClicked();
				}
			});
		}
		return encodeButton;
	}
	/**
	 * This method initializes decodeButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDecodeButton() {
		if (decodeButton == null) {
			decodeButton = new JButton();
			decodeButton.setMnemonic('D');
			decodeButton.setBounds(new Rectangle(136, 347, 77, 29));
			decodeButton.setText("译码(D)");
			decodeButton.setToolTipText("快捷键D");
			decodeButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK,false), "onDecode");
			decodeButton.getActionMap().put("onDecode", onDecode);
			decodeButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					onDecodeClicked();
				}
			});
		}
		return decodeButton;
	}
	/**
	 * This method initializes exitButton	
	 * 
	 * @return javax.swing.JButton	
	 */
	private JButton getExitButton() {
		if (exitButton == null) {
			exitButton = new JButton();
			exitButton.setBounds(new Rectangle(487, 346, 82, 30));
			exitButton.setText("退出(Q)");
			exitButton.setMnemonic('Q');
			exitButton.setToolTipText("快捷键Q");
			exitButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK,false),"onExit");
			exitButton.getActionMap().put("onExit", onExit);
			exitButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					onExitClicked();
				}
			});
		}
		return exitButton;
	}
	private AbstractAction onExit = new AbstractAction(){
		private static final long serialVersionUID = -7523450228866757720L;  //  @jve:decl-index=0:
		public void actionPerformed(ActionEvent e){
			onExitClicked();
		}	 
	};
	private AbstractAction onEncode = new AbstractAction(){
		private static final long serialVersionUID = 1L;  //  @jve:decl-index=0:

		public void actionPerformed(ActionEvent arg0) {
			onEncodeClicked();		
		}
		
	};
	private void onEncodeClicked(){
		JFileChooser fileChooser = new JFileChooser();
	    String fileName = null;
	    fileChooser.setCurrentDirectory(new File("."));
	    fileChooser.setBounds(new Rectangle(70, 53, 37, 26));
        if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
	    	fileName = fileChooser.getSelectedFile().getAbsolutePath();
	        EncodeThread encodeThread = new EncodeThread(fileName,fileChooser.getSelectedFile().getPath(),outBin);
	        encodeThread.start();
	    }
	}
	private AbstractAction onDecode = new AbstractAction(){
		private static final long serialVersionUID = -6314760646737837532L;  //  @jve:decl-index=0:
		public void actionPerformed(ActionEvent e){
			onDecodeClicked();
		}
	};
	private void onDecodeClicked(){
		final JFileChooser fileChooser = new JFileChooser();
		final String fileName;
		fileChooser.setCurrentDirectory(new File("."));
		fileChooser.setAcceptAllFileFilterUsed(false);
		fileChooser.setSize(new Dimension(30, 36));
		fileChooser.enableInputMethods(true);
		fileChooser.createToolTip();
		fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
			@Override
			public boolean accept(File file) {
				return file.getName().toLowerCase().endsWith(".cod") || file.isDirectory();
			}

			@Override
			public String getDescription() {
				return "Huffman Code";
			}			
		});
		
		if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
			fileName = fileChooser.getSelectedFile().getAbsolutePath();
			DecodeThread decodeThread = new DecodeThread(fileName, fileChooser.getSelectedFile().getPath());
			decodeThread.start();
			}
	}	
	private void onExitClicked() {
		if(JOptionPane.showConfirmDialog(this,"确定退出?","确定",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE)==JOptionPane.YES_OPTION)
			   System.exit(0);
	}
	/**
	 * This method initializes aboutButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAboutButton() {
		if (aboutButton == null) {
			aboutButton = new JButton();
			aboutButton.setBounds(new Rectangle(366, 346, 83, 30));
			aboutButton.setText("关于(A)");
			aboutButton.setMnemonic('A');
			aboutButton.setToolTipText("快捷键A");
			aboutButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK,false), "onAbout");
			aboutButton.getActionMap().put("onAbout", onAbout);
			aboutButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					showAbout();
				}
			});
		}
		return aboutButton;
	}
	AbstractAction onAbout = new AbstractAction(){
		/**
		 * 
		 */
		private static final long serialVersionUID = -6314760646737837532L;
		public void actionPerformed(ActionEvent e){
			showAbout();
		}
	};
	private void showAbout(){
		 JOptionPane.showMessageDialog(this, "古流枫叶作品\n      !^_^!\n简易编码译码器");
	 }
	/**
	 * This method initializes infoTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getInfoTextArea() {
		if (infoTextArea == null) {
			infoTextArea = new JTextArea();
			infoTextArea.setBounds(new Rectangle(25, 83, 542, 247));
			infoTextArea.setEditable(false);
			infoTextArea.setForeground(Color.green);
			infoTextArea.setToolTipText("相关信息");
			infoTextArea.setText(aboutMe);
			infoTextArea.setFont(new Font("",Font.BOLD,16));
			infoTextArea.setBackground(Color.WHITE);
			infoTextArea.setAutoscrolls(true);
			infoTextArea.setAlignmentX(infoTextArea.getSize().width/2);
			infoTextArea.setAlignmentX(infoTextArea.getSize().height/2);
			infoTextArea.setLineWrap(true);
		}
		return infoTextArea;
	}
	/**
	 * This method initializes backButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBackButton() {
		if (backButton == null) {
			backButton = new JButton();
			backButton.setBounds(new Rectangle(255, 346, 80, 30));
			backButton.setText("返回(B)");
			backButton.setMnemonic('B');
			backButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK, false), "onBack");
			backButton.getActionMap().put("onBack", onBack);
			backButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					onBack();
				}
			});
		}
		return backButton;
	}
	private AbstractAction onBack = new AbstractAction(){
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;  //  @jve:decl-index=0:
		public void actionPerformed(ActionEvent e){
			onBack();
		}
	};	
	private void onBack() {
		Main.infoTextArea.setText(aboutMe);
	}

	/**
	 * This method initializes binCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getBinCheckBox() {
		if (binCheckBox == null) {
			binCheckBox = new JCheckBox();
			binCheckBox.setBounds(new Rectangle(446, 378, 127, 21));
			binCheckBox.setOpaque(false);
			binCheckBox.setText("输出二进制代码");
			binCheckBox.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					if(binCheckBox.isSelected())
					   outBin = true;
					else
						outBin = false;
				}				
			});
		}
		return binCheckBox;
	}
}  //  @jve:decl-index=0:visual-constraint="73,13"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -