您的位置:首页 > 编程语言 > Java开发

java小小工具第三弹 文本编辑器

2016-03-02 23:19 573 查看
/*

* 本版主要实现是io流的简单操作

* BufferedReader的运用

* 文本筛选器FileFilter的简单使用

*/



package cn.hncu.mytoolkit;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;

public class MyNoteBook extends JFrame{
private JMenu[] menu;
private JMenuItem[][] menuItem;
private JTextArea txt;
private ActionListener actionLister=new ActionListener() {
private final String line=System.getProperty("line.separator");
private File nowfile;
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("New")){
nowfile=null;
txt.setText("");
}else if(e.getActionCommand().equals("Open")){
openFile();
}else if(e.getActionCommand().equals("Save")){
writeFile();
}else if(e.getActionCommand().equals("SaveAs")){
SaveFile();
}else if(e.getActionCommand().equals("Exit")){
//System.exit(0);
dispose();
}else if(e.getActionCommand().equals("Cut")){
txt.cut();
}else if(e.getActionCommand().equals("Copy")){
txt.copy();
}else if(e.getActionCommand().equals("Paste")){
txt.paste();
}else if(e.getActionCommand().equals("SetFont")){
setFont();
}else if(e.getActionCommand().equals("setColor")){
Color c=JColorChooser.showDialog(null, "颜色选择", Color.black);
txt.setForeground(c);
}
}
//存储文件
private void SaveFile() {
JFileChooser fileCh=new JFileChooser("D:\\");
fileCh.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if(fileCh.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
nowfile=fileCh.getSelectedFile();
writeFile();
}
}
//写文件
private void writeFile() {
if(nowfile==null){
SaveFile();
return;
}
FileWriter fwrite=null;
BufferedWriter bwrite=null;
try {
fwrite=new FileWriter(nowfile);
bwrite=new BufferedWriter(fwrite);
bwrite.write(txt.getText());
bwrite.flush();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "写入失败");
}finally{
if(bwrite!=null){
try {
bwrite.close();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "写入文件缓冲流关闭失败");
}
}
if(fwrite!=null){
try {
fwrite.close();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "写入文件流关闭失败");
}
}
}
}
//打开文件
private void openFile() {
JFileChooser fileCh=new JFileChooser("D:\\");
fileCh.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileCh.setFileFilter(new myFileFilter("*.java","*.txt" ,".java文件 .txt文件"));
FileReader fread=null;
BufferedReader bread=null;
try {
int option=fileCh.showOpenDialog(null);
if(option==JFileChooser.APPROVE_OPTION){
nowfile=fileCh.getSelectedFile();
fread=new FileReader(nowfile);
bread=new BufferedReader(fread);
String s=bread.readLine();
txt.setText("");
while(s!=null){//文件结束返回null
txt.append(s+line);
s=bread.readLine();
}
}
} catch (HeadlessException e1) {
JOptionPane.showMessageDialog(null, "操作失败");
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(null, "文件打开失败");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "文件读取失败");
}finally{
if(bread!=null){
try {
bread.close();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "读取文件缓冲流关闭失败");
}
}
if(fread!=null){
try {
fread.close();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "读取文件流关闭失败");
}
}
}
}
//文件筛选格式.java .txt
class myFileFilter extends FileFilter{
private String prefix="";
private String suffix="";
private String prefix2="";
private String suffix2="";
private String fileDescription="";

public myFileFilter(String fileType,String fileType2, String fileDescription) {
this.fileDescription=fileDescription;

fileType=fileType.toLowerCase();
int index=fileType.indexOf('*');
if(index>0)
prefix=fileType.substring(0,index);
index=fileType.indexOf('.');
if (index>0) {
suffix = fileType.substring(index + 1);
if (suffix.equals("*"))
suffix = "";
}

fileType2=fileType2.toLowerCase();
index=fileType2.indexOf('*');
if(index>0)
prefix2=fileType2.substring(0,index);
index=fileType2.indexOf('.');
if (index>0) {
suffix2= fileType2.substring(index + 1);
if (suffix2.equals("*"))
suffix2 = "";
}
}
@Override
public boolean accept(File f) {
String name=f.getName();
if(name.startsWith(prefix)&&name.endsWith(suffix))
return true;
if(name.startsWith(prefix2)&&name.endsWith(suffix2))
return true;
return false;
}

@Override
public String getDescription() {
return fileDescription;
}
}
};

public MyNoteBook(){
super("个人简易记事本");
setBounds(200, 80, 600, 600);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//菜单
JMenuBar bar=new JMenuBar();
String[] strOne={"文件","编辑","格式"};
String[][] strTwo={{"新建(Z)","打开(O)...","保存(S)","另存为(A)...","退出(X)"},
{"剪切(T)","复制(C)","粘贴(V)"},
{"字体设置(F)...","颜色设置(Y)..."}};
String[][] strTwoCom={{"New","Open","Save","SaveAs","Exit"},
{"Cut","Copy","Paste"},
{"SetFont","setColor"}};
menu=new JMenu[strOne.length];
for (int i = 0; i < menu.length; i++) {
menu[i]=new JMenu(strOne[i]);
bar.add(menu[i]);
}
menuItem=new JMenuItem[menu.length][];
for (int i = 0; i < menuItem.length; i++) {
menuItem[i]=new JMenuItem[strTwo[i].length];
for (int j = 0; j < menuItem[i].length; j++) {
if(i==0&&j==4)
menu[i].addSeparator();
menuItem[i][j]=new JMenuItem(strTwo[i][j]);
menuItem[i][j].setActionCommand(strTwoCom[i][j]);
menuItem[i][j].addActionListener(actionLister);
menu[i].add(menuItem[i][j]);
}
}
setJMenuBar(bar);

txt=new JTextArea();
getContentPane().add(new JScrollPane(txt));

setVisible(true);
}

//字体颜色设置
private void setFont() {
final Dialog diaFont=new Dialog(this, "字体设置", true);
diaFont.setBounds(this.getX()+40, this.getY()+120, 400, 200);
diaFont.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
diaFont.dispose();
}
});
diaFont.setLayout(null);

final JLabel txtlab=new JLabel("AaBbCc");
txtlab.setHorizontalAlignment(JLabel.CENTER);
txtlab.setBorder(BorderFactory.createBevelBorder(1));
txtlab.setBounds(200, 80, 80, 60);
txtlab.setFont(new Font(txt.getFont().getName(),txt.getFont().getStyle(),txt.getFont().getSize()));
diaFont.add(txtlab);

final JComboBox[] fontBox=new JComboBox[3];
String[][] fontStr=new String[3][];
fontStr[0]=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
fontStr[1]=new String[]{"常规","粗体","斜体","粗体+斜体"};
fontStr[2]=new String[]{"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
for (int i = 0; i < fontBox.length; i++) {
fontBox[i]=new JComboBox(fontStr[i]);
//匿名内部类,只能访问外部final变量
fontBox[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name=(String)fontBox[0].getSelectedItem();
String type=(String)fontBox[1].getSelectedItem();
int style=Font.PLAIN;
if(type.equals("粗体"))
style=Font.BOLD;
else if(type.equals("斜体"))
style=Font.ITALIC;
else if(type.equals("粗体+斜体"))
style=Font.ITALIC+Font.BOLD;
int size=Integer.parseInt((String)fontBox[2].getSelectedItem());
txtlab.setFont(new Font(name, style, size));
}
});
diaFont.add(fontBox[i]);
}
fontBox[0].setBounds(20, 40, 160, 20);
fontBox[1].setBounds(200, 40, 80, 20);
fontBox[2].setBounds(300, 40, 80, 20);

JButton txtbtn=new JButton("确定");
txtbtn.setMargin(new Insets(0, 0, 0, 0));
txtbtn.setBounds(340, 155, 40, 30);
txtbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txt.setFont(new Font(txtlab.getFont().getName(), txtlab.getFont().getStyle(), txtlab.getFont().getSize()));
diaFont.dispose();
}
});
diaFont.add(txtbtn);

diaFont.setVisible(true);
}
public static void main(String[] args) {
new MyNoteBook();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: