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

📄 fileoperator.java

📁 这是一个用java编写类似于notepad文件编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		JMenuItem helpItem = new JMenuItem("帮助");
		aboutmenu.add(helpItem);
		helpItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (dialog2 == null) //first run
					dialog2 = new HelpDialog(AppFrame.this);
				dialog2.show();
			}
		});
		JMenuItem aboutItem = new JMenuItem("关于本软件");
		aboutmenu.add(aboutItem);
		aboutItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (dialog == null) //first run
					dialog = new AboutDialog(AppFrame.this);
				dialog.show();
			}
		});

	}

}

class ViewFrame extends JFrame {
	JTextArea textarea;
	String directory;
	JFileChooser chooser;

	public ViewFrame() {
		this(null, null);
	}

	public ViewFrame(String filename) {
		this(null, filename);
	}

	public ViewFrame(String directory, String filename) {
		super("查看文件");
		setSize(200, 320);

		//	创建一个用来显示文件内容的TextArea区域
		textarea = new JTextArea("", 24, 80);
		textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
		textarea.setEditable(false);
		Container contentPane = getContentPane();
		contentPane.add("Center", textarea);

		// 创建一个包含两个按钮控件的面板
		JPanel p = new JPanel();
		p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
		contentPane.add(p, "South");
		Font font = new Font("SansSerif", Font.BOLD, 14);
		JButton openfile = new JButton("打开文件");
		JButton close = new JButton("关闭");
		openfile.addActionListener(new OpenAction(this, directory));

		openfile.setActionCommand("open");
		openfile.setFont(font);
		close.addActionListener(new OpenAction(this, directory));
		close.setActionCommand("close");
		close.setFont(font);
		p.add(openfile);
		p.add(close);
		this.pack();

		// 指明目录
		if (directory == null) {
			File f;
			if ((filename != null) && (f = new File(filename)).isAbsolute()) {
				directory = f.getParent();
				filename = f.getName();
			} else
				directory = System.getProperty("user.dir");
		}
		this.directory = directory; // 记住目录 
		setFile(directory, filename); // 载入并显示文件

	}

	public void setFile(String directory, String filename) {
		if ((filename == null) || (filename.length() == 0))
			return;
		File f;
		FileReader in = null;
		// 读取并且显示文件内容  Since we're reading text, we
		// 因为是在读取文本,所以使用FileReader,而不使用FileInputStream.
		try {
			f = new File(directory, filename); // 创建一个file对象
			in = new FileReader(f); // 和一个用来读取它的字符流
			char[] buffer = new char[4096]; // 每次读取4K字符
			int len; // 每次读入的字符数
			textarea.setText(""); // 清除文本区域
			while ((len = in.read(buffer)) != -1) { // 读取一批字符
				String s = new String(buffer, 0, len); // 转化为一个字符串
				textarea.append(s); // 显示文本
			}
			this.setTitle("FileViewer: " + filename); // 设置窗口标题
			textarea.setCaretPosition(0); // 设置文件起始位置
		}
		// 显示错误信息
		catch (IOException e) {
			textarea.setText(e.getClass().getName() + ": " + e.getMessage());
			this.setTitle("FileViewer: " + filename + ": I/O Exception");
		}
		// 关闭输入流
		finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				try {
					AppFrame.WriteError(e);
				} catch (IOException err) {

				}
			}
		}
	}

	class OpenAction implements ActionListener {
		JFrame frame;
		String directory;
		public OpenAction(JFrame aframe, String dir) {
			frame = aframe;
			directory = dir;
		}
		public void actionPerformed(ActionEvent e) {
			String cmd = e.getActionCommand();
			if (cmd.equals("open")) { // 如果用户单击了“打开”按钮
				// 创建一个文件对话框,提示输入新的文件
				FileDialog f =
					new FileDialog(frame, "Open File", FileDialog.LOAD);
				f.setDirectory(directory); // 设置默认目录
				// 显示对话框并等待用户的输入
				f.show();
				directory = f.getDirectory(); // 记住新的默认目录
				setFile(directory, f.getFile()); // 载入并显示选择
				f.dispose(); // 关闭对话框
			} else if (cmd.equals("close")) // 如果用户单击了“关闭”按钮
				frame.dispose(); //关闭窗口
		}
	}
}

class AboutDialog extends JDialog {

	public AboutDialog(JFrame owner) {
		super(owner, "About Dialog", true);
		Container contentPane = getContentPane();

		//add HTML label to center
		contentPane.add(
			new JLabel(
				"<HTML><H1><I>FileOperator</I></H1><HR>"
					+ " Made By Song !<HTML>"),
			BorderLayout.CENTER);

		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		JPanel panel = new JPanel();
		panel.add(ok);
		contentPane.add(panel, BorderLayout.SOUTH);
		setSize(250, 150);
	}
}
class HelpDialog extends JDialog {

	public HelpDialog(JFrame owner) {
		super(owner, "Help Dialog", true);
		Container contentPane = getContentPane();

		//add HTML label to center
		contentPane.add(
			new JLabel(
				"<HTML><H1>本软件可以实现文件的基本删除,复制,压缩和查看等基本操作<HR>" +
				"同时也可以对java所支持的图片(.gif,.jpg,.jpeg)查看。欢迎使用:)<</H1><HR>"
					+ " Made By Song !<HTML>"),
			BorderLayout.CENTER);

		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		JPanel panel = new JPanel();
		panel.add(ok);
		contentPane.add(panel, BorderLayout.SOUTH);
		setSize(500,300);
	}
}


class ExtensionFileFilter extends FileFilter {
	public void addExtension(String extension) {
		if (!extension.startsWith("."))
			extension = "." + extension;
		extensions.add(extension.toLowerCase());
	}

	public void setDescription(String aDes) {
		description = aDes;
	}
	public String getDescription() {
		return description;
	}

	public boolean accept(File f) {
		if (f.isDirectory())
			return true;
		String name = f.getName().toLowerCase();

		for (int i = 0; i < extensions.size(); i++)
			if (name.endsWith((String) extensions.get(i)))
				return true;
		return false;
	}
	private String description = "";
	private ArrayList extensions = new ArrayList();
}

class FileIconView extends FileView {

	private FileFilter filter;
	private Icon icon;
	public Icon getIcon(File f) {
		if (!f.isDirectory() && filter.accept(f))
			return icon;
		else
			return null;
	}
}

class ImagePreviewer extends JLabel {
	public ImagePreviewer(JFileChooser chooser) {
		setPreferredSize(new Dimension(100, 100));
		setBorder(BorderFactory.createEtchedBorder());

		chooser.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent event) {
				if (event.getPropertyName()
					== JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
					File f = (File) event.getNewValue();
					ImageIcon icon = new ImageIcon(f.getPath());

					if (icon.getIconWidth() > getWidth())
						icon =
							new ImageIcon(
								icon.getImage().getScaledInstance(
									getWidth(),
									-1,
									Image.SCALE_DEFAULT));
					setIcon(icon);
				}
			}
		});
	}
}

class ImageViewerFrame extends JFrame {

	JLabel label;
	JFileChooser chooser;
	int dWIDTH = 300;
	int dHEIGHT = 400;
	AboutDialog dialog;
	
	public ImageViewerFrame() {
		setTitle("ImageViewer");
		setSize(dWIDTH, dHEIGHT);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		JMenu menu = new JMenu("File");
		menuBar.add(menu);

		JPanel myPanel = new JPanel();
		Container contentPane = getContentPane();
		contentPane.add(myPanel);
		myPanel.setBackground(Color.yellow);

		//用一个LABEL来显示图片
		label = new JLabel();

		myPanel.add(label);

		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("Open");
		menu.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);

				}
			}
		});

		JMenuItem exitItem = new JMenuItem("Exit");
		menu.add(exitItem);
		exitItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		JMenuItem aboutItem = new JMenuItem("About");
		menu.add(aboutItem);
		aboutItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (dialog == null) //first run
					dialog = new AboutDialog(ImageViewerFrame.this);
				dialog.show();
			}
		});
	}

}
 

⌨️ 快捷键说明

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