简易记事本实现

/*
未能实现像双击一样打开一个文件。。。。。

* /
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.border.Border;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.filechooser.FileFilter;

public class test extends JFrame {
private JTextArea text = new JTextArea();//创建文本区
private String copyText;
private boolean isCopy;
private JMenuBar bar = new JMenuBar();
JFileChooser fileChooser1 = new JFileChooser(new File("."));//文件操作
JFileChooser fileChooser2 = new JFileChooser(new File("."));//文件操作
private JPopupMenu popupMenu = new JPopupMenu();//弹出式菜单
private JMenuItem copy = new JMenuItem("复制",new ImageIcon("image1.gif"));
private JMenuItem paste = new JMenuItem("粘贴",new ImageIcon("image2.gif"));
private JMenu file = new JMenu("文件");
private JMenu help = new JMenu("帮助");
private JMenuItem open = new JMenuItem("打开文件");
private JMenuItem save = new JMenuItem("保存文件");
private JMenuItem reset = new JMenuItem("清除文本区");
private JMenuItem color = new JMenuItem("选择文本框背景色");
test(){
super("记事本");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setLayout(new BorderLayout());
text.setLineWrap(true);//自动换行;
JScrollPane editor = new JScrollPane(text);
editor.setBorder(BorderFactory.createLineBorder(Color.RED));//设置文本区边框
file.setMnemonic('w');
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));//为菜单项添加快捷键;
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
file.add(open);file.add(save);//将菜单项添加到菜单栏
help.add(reset);help.add(color);
bar.add(file);bar.add(help);
getContentPane().add(editor,BorderLayout.CENTER);
setJMenuBar(bar);
/*文件过滤器*/
popupMenu.add(copy);
popupMenu.addSeparator();
popupMenu.add(paste);
myFileFilter txtfilter = new myFileFilter("txt 文件 (*.txt)",".txt");//增加两个只对保存文件是有用
myFileFilter wordfilter = new myFileFilter("java 文件(*.java)",".java");
fileChooser2.addChoosableFileFilter(txtfilter);//为文件对话框添加自定义的扩展名过滤器:
fileChooser2.addChoosableFileFilter(wordfilter);
fileChooser1.addChoosableFileFilter(txtfilter);//为文件对话框添加自定义的扩展名过滤器:
fileChooser1.addChoosableFileFilter(wordfilter);
/*添加各响应*/
text.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
showPopupMenu(e);
}
public void mouseReleased(MouseEvent e){
showPopupMenu(e);
}


});
copy.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
copyText = text.getSelectedText();// TODO Auto-generated method stub

}

});
paste.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
text.replaceRange(copyText, text.getText().indexOf(text.getSelectedText()),
text.getText().indexOf(text.getSelectedText())+text.getSelectedText().length());


}

});
reset.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
text.setText(null);// TODO Auto-generated method stub

}

});
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
openFile();// TODO Auto-generated method stub
}
});
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveFile();
}
});
color.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Color selectColor = JColorChooser.showDialog(null, "选择背景颜色",text.getBackground());// TODO Auto-generated method stub
if(selectColor!=null){
text.setBackground(selectColor);
}
}

});
addWindowListener(new WindowAdapter(){//关闭窗口时的响应事件;
public void windowClosing(WindowEvent e) {
int option=JOptionPane.showConfirmDialog(null, "是否保存文件?","保存文件对话框" ,
JOptionPane.YES_NO_CANCEL_OPTION);// TODO Auto-generated method stub

if(option==JOptionPane.OK_OPTION)
saveFile();
else if(option==JOptionPane.CANCEL_OPTION){
try {
e.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();// TODO Auto-generated catch block

}
}
}

@Override
public void windowClosed(WindowEvent e) {
System.exit(0);// TODO Auto-generated method stub

}

});
}
private void showPopupMenu(MouseEvent e) {
if(e.isPopupTrigger()){//是否触发弹出菜单事件
popupMenu.show(e.getComponent(), e.getX(), e.getY());// TODO Auto-generated method stub
}

}
private void open(File file){// 像双击一样打开一个文件??????????????????????????
Runtime runtime = Runtime.getRuntime();
// System.out.println(file.getAbsolutePath());
try {
runtime.exec("cmd /c start"+ file.getAbsolutePath() );
}catch(Exception e){}//如果要打开的是txt文件,则想双击一样打开
}
private void openFile(){//打开一个文件
if(fileChooser1.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
myFileFilter filter = (myFileFilter) fileChooser1.getFileFilter();//获得选择的过滤器
String str = filter.getEnds();//获得过滤得到的文件类型名;
File file = fileChooser1.getSelectedFile();//获得文件名;
if(file.getName().toLowerCase().endsWith(".jpg")){
open(file);
}
else {
// File newFile;
// if(file.getAbsolutePath().toLowerCase().endsWith(str.toLowerCase()))
// newFile = file;
// else newFile = new File(file.getAbsolutePath()+str);
openFile(file);
}
}
else {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void openFile(File file){//打开一个文件
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));//创建一个内部缓冲区数组并将其存储在 buf 中
byte[] b = new byte[in.available()];//b的长度是多少,每次就读取多少个字节
in.read(b,0,b.length);//从此字节输入流中给定偏移量处开始将各字节读取到指定的 byte 数组中。
text.append(new String(b,0,b.length));
in.close();
}catch(IOException e){}
}
private void saveFile() {//保存文件
if(fileChooser2.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
myFileFilter filter = (myFileFilter) fileChooser2.getFileFilter();//获得选择的过滤器;
File file = fileChooser2.getSelectedFile();//获得文件名;
String str = filter.getEnds();//获得过滤得到的文件类型名;
File newFile;
if(file.getAbsolutePath().toUpperCase().endsWith(str.toUpperCase()))
newFile = file;
else newFile = new File(file.getAbsolutePath()+str);
saveFile(newFile);
} else
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void saveFile(File file){//保存文件
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] b = text.getText().getBytes();
out.write(b,0,b.length);
out.close();
}catch(IOException e){}
}
public static void main(String[]args){
test txt = new test();
txt.setVisible(true);
}
}
/*文件过滤器*
*/
import java.io.File;

import javax.swing.filechooser.FileFilter;

public class myFileFilter extends FileFilter {
private String Description;
private String ends;
myFileFilter(String Description,String ends){
this.Description = Description;
this.ends = ends;

}
@Override
public boolean accept(File f) {
if(f.isDirectory()) return true;// TODO Auto-generated method stub
String fileString = f.getName();
if(fileString.toUpperCase().endsWith(ends.toUpperCase())) return true;//过滤掉其他文件类型
return false;
}

@Override
public String getDescription() {
// TODO Auto-generated method stub
return this.Description;
}
public String getEnds(){
return this.ends;

}
}

你可能感兴趣的:(java,swing,F#)