?? huffgui.java
字號:
package Huffman;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class HuffGUI extends JFrame{
private File infile,outfile;
private JLabel inLabel,outLabel;
private JTextField inField,outField;
private JButton openButton,enterButton;
private ButtonGroup radioGroup;
private JRadioButton compressButton,decompressButton;
private Boolean compress=true;
public HuffGUI ()
{
super("Huffman");
JPanel inPanel=new JPanel();
JPanel outPanel=new JPanel();
JPanel processPanel=new JPanel();
Box box = Box.createVerticalBox();
inLabel=new JLabel("Input:");
outLabel=new JLabel("Output:");
inField=new JTextField(20);
outField=new JTextField(20);
openButton=new JButton ();
openButton.setText("...");
openButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
openFile();
}
}
);
enterButton=new JButton("Process");
enterButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
process();
}
}
);
inPanel.add(inLabel);
inPanel.add(inField);
inPanel.add(openButton);
outPanel.add(outLabel);
outPanel.add(outField);
compressButton=new JRadioButton("Compress",true);
compressButton.addItemListener(new RadioButtonHandler());
decompressButton=new JRadioButton("Decompress",false);
decompressButton.addItemListener(new RadioButtonHandler());
radioGroup=new ButtonGroup();
radioGroup.add(compressButton);
radioGroup.add(decompressButton);
processPanel.add(compressButton);
processPanel.add(decompressButton);
processPanel.add(enterButton);
box.add(inPanel);
box.add(outPanel);
box.add(processPanel);
this.add(box);
this.setLocation(100,200);
setSize(400,200);
setVisible(true);
}
private class RadioButtonHandler implements ItemListener{
public void itemStateChanged(ItemEvent event)
{
if(event.getSource()==decompressButton)
compress=false;
else
compress=true;
}
}
private void openFile(){
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
return;
infile=fileChooser.getSelectedFile();
if(infile==null||infile.getName().equals(""))
{
JOptionPane.showMessageDialog(this,"Invalid File Name","Invalid File Name",
JOptionPane.ERROR_MESSAGE);
return ;
}
inField.setText(infile.getName());
}
private void process() {
outfile=new File(outField.getText());
if(outfile==null||outfile.getName().equals(""))
{
JOptionPane.showMessageDialog(this,"Invalid File Name","Invalid File Name",
JOptionPane.ERROR_MESSAGE);
return ;
}
if(compress==false)
{
Decompress c=new Decompress(infile,outfile);
c.process();
JOptionPane.showMessageDialog(this,"Decompress OK","Result",
JOptionPane.INFORMATION_MESSAGE);
return;
}
else{
Compress c=new Compress(infile,outfile);
c.process();
JOptionPane.showMessageDialog(this,"Compress OK","Result",
JOptionPane.INFORMATION_MESSAGE);
return;
}
}
public static void main(String [] args)
{
HuffGUI g=new HuffGUI();
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -