📄 fileoperator.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;
/*
* Created on 2004-3-13
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class FileOperator {
public static void main(String args[]) throws IOException {
Toolkit kit = Toolkit.getDefaultToolkit();
AppFrame frame = new AppFrame();
frame.setIconImage(kit.getImage("app.gif"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class AppFrame extends JFrame {
JTextArea textarea;
String directory; // 在FileDialog中显示的默认目录
JFileChooser chooser;
JLabel label;
int dWIDTH = 400, dHEIGHT = 300;
AboutDialog dialog;
HelpDialog dialog2;
static void WriteError(IOException e) throws IOException {
File log = new File("error.log");
FileWriter fw = new FileWriter(log);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(e.toString());
}
void CopyFile() throws IOException {
JFileChooser chooser = new JFileChooser();
String from = new String();
String to = new String();
int r1 = chooser.showOpenDialog(null);
//if file selected,set it as icon of the label
if (r1 == JFileChooser.APPROVE_OPTION) {
from = chooser.getSelectedFile().getPath();
}
File f1 = new File(from);
boolean isfile1 = f1.isFile(), retry;
retry = false;
if (!isfile1) {
JOptionPane.showMessageDialog(this, "复制失败!");
retry = true;
}
if (!retry) {
int r2 = chooser.showSaveDialog(null);
if (r2 == JFileChooser.APPROVE_OPTION) {
to = chooser.getSelectedFile().getPath();
}
}
FileCopy.copy(from, to);
}
void DeleteFile() throws IOException {
JFileChooser chooser = new JFileChooser();
String filename = new String();
int r1 = chooser.showOpenDialog(null);
//if file selected,set it as icon of the label
if (r1 == JFileChooser.APPROVE_OPTION) {
filename = chooser.getSelectedFile().getPath();
}
File f = new File(filename);
boolean isfile = f.isFile();
if (!isfile) {
JOptionPane.showMessageDialog(this, "删除失败!");
}
Delete.delete(filename);
}
void ZipFile() throws IOException {
JFileChooser chooser = new JFileChooser();
String from = new String();
int r1 = chooser.showOpenDialog(null);
//if file selected,set it as icon of the label
if (r1 == JFileChooser.APPROVE_OPTION) {
from = chooser.getSelectedFile().getPath();
}
String to = new String();
File f1 = new File(from);
boolean isfile1 = f1.isFile(), isdrectory = f1.isDirectory(), retry;
retry = false;
if (!isfile1 && !isdrectory) {
JOptionPane.showMessageDialog(this, "压缩失败!");
retry = true;
//ZipFile();
}
if (!retry) {
int r2 = chooser.showSaveDialog(null);
if (r2 == JFileChooser.APPROVE_OPTION) {
to = chooser.getSelectedFile().getPath();
}
if (isdrectory)
to = from + ".zip";
else
to = from + ".gz";
if (new File(to).exists()) {
JOptionPane.showMessageDialog(new AppFrame(), "有同名文件!");
retry = true;
ZipFile();
}
Compress.gzipFile(from, to);
}
Compress.gzipFile(from, to);
}
void ViewFile() {
ViewFrame frame = new ViewFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
void SetFile(int width,int height)throws IOException {
/*SetFrame frame = new SetFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
*/
dWIDTH=width;
dHEIGHT=height;
FileWriter fw=new FileWriter(fi);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(new Integer(width).toString());
bw.newLine();
bw.write(new Integer(height).toString());
bw.close();
this.setSize(dWIDTH,dHEIGHT);
}
final File fi=new File("config.conf");
public AppFrame() throws IOException {
super("FileOperator-made By SongLei(020439)");
try {
FileReader fin = new FileReader(fi);
BufferedReader br = new BufferedReader(fin);
dWIDTH = Integer.parseInt(br.readLine());
dHEIGHT = Integer.parseInt(br.readLine());
} catch (IOException e) {
}
catch(NumberFormatException e2){
dWIDTH=700;
dHEIGHT=500;
}
setSize(dWIDTH, dHEIGHT);
//建立菜单
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu filemenu = new JMenu("文件");
menuBar.add(filemenu);
JMenu picmenu = new JMenu("图象");
menuBar.add(picmenu);
JMenu setmenu = new JMenu("设置");
menuBar.add(setmenu);
JMenu aboutmenu = new JMenu("关于");
menuBar.add(aboutmenu);
JPanel myPanel = new JPanel();
myPanel.setLayout(null);
Container contentPane = getContentPane();
contentPane.add(myPanel);
myPanel.setBackground(Color.pink);
//用一个LABEL来显示图片
label = new JLabel();
label.setBounds(10,10,100,100);
myPanel.add(label);
JLabel label2 = new JLabel();
myPanel.add(label2, BorderLayout.CENTER);
ImageIcon im = new ImageIcon("1.gif");
label2.setIcon(im);
label2.setText("Welcome to my App");
label2.setBounds(10,200,800,35);
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
//accept all image files ending with .jpg .jpeg .gif
final ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension("jpg");
filter.addExtension("jpeg");
filter.addExtension("gif");
filter.setDescription("Image files");
chooser.setFileFilter(filter);
chooser.setAccessory(new ImagePreviewer(chooser));
JMenuItem openItem = new JMenuItem("打开", new ImageIcon("open.gif"));
picmenu.add(openItem);
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int r = chooser.showOpenDialog(null);
//if file selected,set it as icon of the label
if (r == JFileChooser.APPROVE_OPTION) {
String name = chooser.getSelectedFile().getPath();
ImageIcon im = new ImageIcon(name);
im.getImage().getScaledInstance(
im.getIconWidth() * 2,
im.getIconHeight() * 2,
Image.SCALE_DEFAULT);
label.setIcon(im);
label.setText(chooser.getSelectedFile().getName());
}
}
});
JMenuItem copyItem = new JMenuItem("复制", new ImageIcon("copy.gif"));
filemenu.add(copyItem);
copyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
CopyFile();
} catch (IOException err) {
}
}
});
JMenuItem deleteItem = new JMenuItem("删除", new ImageIcon("delete.gif"));
filemenu.add(deleteItem);
deleteItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
DeleteFile();
} catch (IOException err) {
}
}
});
JMenuItem zipItem = new JMenuItem("压缩", new ImageIcon("zip.gif"));
filemenu.add(zipItem);
zipItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ZipFile();
} catch (IOException err) {
}
}
});
JMenuItem lookItem = new JMenuItem("查看", new ImageIcon("view.gif"));
filemenu.add(lookItem);
lookItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ViewFile();
}
});
JMenuItem exitItem = new JMenuItem("Exit", new ImageIcon("exit.gif"));
filemenu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JMenuItem setItem1 = new JMenuItem("设置300*400", new ImageIcon("set.gif"));
setmenu.add(setItem1);
setItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
SetFile(300,400);
}catch(IOException err){}
}
});
JMenuItem setItem2 = new JMenuItem("设置600*800", new ImageIcon("set.gif"));
setmenu.add(setItem2);
setItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
SetFile(600,800);
}catch(IOException err){}
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -