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

📄 guiframe.java

📁 java编写的哈夫蔓编码译码器(绝对原创)
💻 JAVA
字号:
package gui;
import huff.HmTree;
import huff.HmTree;
import huff.TreeNode;
import huff.TreeReader;
import huff.TreeWriter;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
public class GUIFrame extends JFrame implements  ActionListener {
			/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<TreeNode> currentTree;
			TextArea box=new TextArea("txtArea",10,60,TextArea.SCROLLBARS_BOTH);
			FileDialog dialog=new FileDialog();
			SaveCodeDialog dialog2=new SaveCodeDialog();
			JButton bt1=new JButton("打开");
			JButton bt2=new JButton("译码");
			JButton bt3=new JButton("保存");
			JButton bt4=new JButton("编码");
			JButton bt5=new JButton("载入树");
			TextArea box2=new TextArea("box2",10,60,TextArea.SCROLLBARS_BOTH);
			JScrollPane sp=new JScrollPane();
			JPanel btPanel=new JPanel();
			JPanel txtPanel1=new JPanel();
			JPanel txtPanel2=new JPanel();
			JLabel txtLabel2=new JLabel("赫夫曼编码:");
			JLabel txtLabel=new JLabel("输入要编码的字符串:");
			public GUIFrame(){
				btPanel.add(bt1);
				btPanel.add(bt2);
				bt2.setEnabled(false);
				btPanel.add(bt3);
				btPanel.add(bt4);
				btPanel.add(bt5);
				btPanel.setLayout(new GridLayout());
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				setDefaultLookAndFeelDecorated(true);
				this.add(btPanel);
				txtPanel1.add(txtLabel,BorderLayout.CENTER);
				txtPanel1.add(box,BorderLayout.NORTH);
				box.setBounds(0,0,500,200);		
				this.add(txtPanel1);	
				txtPanel2.add(txtLabel2);
				txtPanel2.add(box2);		
				this.add(txtPanel2);
			    this.setLayout(null);
				this.setSize(630,520);
				txtPanel1.setBounds(10,70,600,200);
				//txtPanel1.setBackground(Color.BLUE);
				txtPanel2.setBounds(10,270,600,200);
				//txtPanel2.setBackground(Color.RED);
				btPanel.setBounds(10,10,600,50);
				addListener();
			}
		public void addListener(){
			this.bt1.addActionListener(new BtListener1());
			this.bt4.addActionListener(new BtListener4());
			this.bt3.addActionListener(new BtListener3());
			this.bt5.addActionListener(new BtListener5());
			this.bt2.addActionListener(new BtListener2());
		}
		private class BtListener1 implements ActionListener {
		public  void actionPerformed(ActionEvent e){
			GUIFrame.this.dialog.showOpenDialog(GUIFrame.this); 
		   }    		
		}
		private void setText(String str,int n){
			if(n==1){box.setText(str);}
			if(n==2){box2.setText(str);}
		}
		private class BtListener4 implements ActionListener{
			public void actionPerformed(ActionEvent e){
				HmTree tree=new HmTree(box.getText());
				GUIFrame.this.currentTree=tree.getTree();
				bt2.setEnabled(true);
				String code=HmTree.code(tree.getTree(), box.getText());
				box2.setText(code);
			}
		}
		private class BtListener3 implements ActionListener{
			public void actionPerformed(ActionEvent e){
			GUIFrame.this.dialog2.showSaveDialog(GUIFrame.this);	
			}
		}
		private class BtListener2 implements ActionListener{
			public void actionPerformed(ActionEvent e){
			GUIFrame.this.box.setText(HmTree.deCode(GUIFrame.this.currentTree, box2.getText()));	
			}
		}
		private class BtListener5 implements ActionListener{
			public void actionPerformed(ActionEvent e){
	         new LoadTree().showOpenDialog(GUIFrame.this);        			
				}
			}
		@SuppressWarnings("serial")
		
		private class LoadTree extends JFileChooser{			
			private static final long serialVersionUID = 1L;
			private File fileopen;
			 TreeReader tr=new TreeReader();
		       LoadTree(){  	   
		    	  addChoosableFileFilter(new Filter());
		       }
			   public void approveSelection(){
				   if(getSelectedFile().getName().endsWith(".hmt")){
				   System.out.println("opentree");
		           fileopen=getSelectedFile();		            
		           GUIFrame.this.currentTree=tr.getList(fileopen.getAbsolutePath());
                   bt2.setEnabled(true);		           
		           super.approveSelection();
				   }
		       }
			   public String getText(TextReader tr,File file){
				   return tr.read(file);
			   }
			   private class Filter extends FileFilter{
					public boolean accept(File file){
						return(file.getName().endsWith(".hmt"));
					}
					public String getDescription(){
						return "郝夫曼树文件(*.hmt)";
					}
		       }	   
		}     
		private class SaveCodeDialog extends JFileChooser{
			private static final long serialVersionUID = 1L;
			CodeSaver saver;
			public SaveCodeDialog(){
				addChoosableFileFilter(new Filter());
			}
			public void approveSelection(){
				File file=getSelectedFile();
				saver=new CodeSaver(file);
				saver.write(box2.getText());
				StringBuilder sb=new StringBuilder(file.getAbsolutePath());
				sb.delete(sb.length()-4, sb.length());
				System.out.println("treefile name: "+sb.toString());			
				TreeWriter tr=new TreeWriter(sb.toString()+".hmt");
				tr.saveTreeToFlie(GUIFrame.this.currentTree);
				super.approveSelection();
				}
		
				
			private class Filter extends FileFilter{
				public boolean accept(File file){
					return(file.getName().endsWith(".cod"));
				}
				public String getDescription(){
					return "郝夫曼编码文件(*.cod)";
				}
			}
		}
		private class FileDialog extends JFileChooser{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			private File fileopen;
		       String txt;
		       OpenTextReader tr=new OpenTextReader();
		       FileDialog(){  	   
		    	   addChoosableFileFilter(new Filter());
		       }
			   public void approveSelection(){
				   System.out.println("open");
		          fileopen=getSelectedFile();
		          if(fileopen.getName().endsWith(".txt")){
		           GUIFrame.this.setText(getText(tr,fileopen),1);
		          }
		          if(fileopen.getName().endsWith(".cod")){
		        	  GUIFrame.this.setText(getText(tr,fileopen),2); 
		          }
		        	  
		           super.approveSelection();        
		       }
			   public String getText(TextReader tr,File file){
				   return tr.read(file);
			   }
		      
		      private class Filter extends FileFilter         
		       {
			        public boolean accept(File file){
			          return(file.getName().endsWith(".txt") || file.isDirectory()||file.getName().endsWith("hfm")||file.getName().endsWith("cod"));
			        }
			        public String getDescription()
			        {
			         return("支持的文件:TXT Files(*.txt), Huffman Code(*.cod), Huffman Tree(*.hmt)");                
				        }
			       }
			}
		public void actionPerformed(ActionEvent e){
	              		
		}
			
			}

⌨️ 快捷键说明

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