package wb;
import java.awt.BorderLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
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.JTextField;
public class Wb extends JFrame{
private JTextArea jta=new JTextArea();
private JMenuBar mb=new JMenuBar();
private String url = null;//文件路径
private String str=null;
private StringSelection ss=null;
private Clipboard clipboard=new Clipboard(str);
private Transferable transferable=null;
private DataFlavor dataflavor=null;
private JMenu file=new JMenu("文件");
private JMenuItem fileNew=new JMenuItem("新建");
private JMenuItem fileOpen=new JMenuItem("打开");
private JMenuItem fileSave=new JMenuItem("保存");
private JMenuItem exit=new JMenuItem("退出");
private JMenu edit=new JMenu("编辑");
private JMenuItem copy=new JMenuItem("复制");
private JMenuItem paste=new JMenuItem("粘贴");
private JMenuItem search=new JMenuItem("查找");
private JMenuItem replace=new JMenuItem("替换");
private JMenu help=new JMenu("帮助");
private JMenuItem aboutWb=new JMenuItem("关于");
private JFileChooser jfc=new JFileChooser();
File f=new File("D:\\Documents\\java.txt");
BorderLayout bl=new BorderLayout();
public Wb(){
super("文本编辑器");
init();
}
public void init(){
file.add(fileNew);
file.add(fileOpen);
file.add(fileSave);
file.add(exit);
mb.add(file);
edit.add(copy);
edit.add(paste);
edit.add(search);
edit.add(replace);
mb.add(edit);
help.add(aboutWb);
mb.add(help);
add(mb);
add(mb, BorderLayout.NORTH);
add(jta, BorderLayout.CENTER);
setSize(400,400);
setVisible(true);
operate();
}
public void operate(){
fileNew.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jta.setText(null);
}
});
fileOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(f!=null){
jfc.setSelectedFile(f);
int a=jfc.showOpenDialog(Wb.this);
if(a==jfc.APPROVE_OPTION){
f=jfc.getSelectedFile();
try {
FileReader fr=new FileReader(f);
int length=(int) f.length();
char[]reader=new char[length];
fr.read(reader, 0, length);
jta.setText(new String(reader));
str=f.getName();
str=str.substring(0,str.length()-4);
setTitle();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
fileSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(f!=null){
jfc.setSelectedFile(f);
int b=jfc.showSaveDialog(Wb.this);
if(b==JFileChooser.APPROVE_OPTION){
f=jfc.getSelectedFile();
try {
FileWriter fw=new FileWriter(f);
fw.write(jta.getText());
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
copy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str=jta.getSelectedText();
ss=new StringSelection(str);
clipboard.setContents(ss, null);
}
});
paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
transferable=clipboard.getContents(this);
dataflavor=DataFlavor.stringFlavor;
if (transferable.isDataFlavorSupported(dataflavor)) {
int start=jta.getSelectionStart();
int end=jta.getSelectionEnd();
jta.replaceRange( null,start,end);
int pos=jta.getCaretPosition();
try {
String str=(String)transferable.getTransferData(dataflavor);
jta.insert(str, pos);
}catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JPanel searchPanel=new JPanel();
JLabel searchLabel=new JLabel("查找内容:");
JTextField searchjtf=new JTextField(10);
searchPanel.add(searchLabel);
searchPanel.add(searchjtf);
JOptionPane.showMessageDialog(null,searchPanel);
/*弹出要求用户提供值或向其发出通知的标准对话框*/
String text=jta.getText();
int fromIndex=jta.getCaretPosition();
//取得当前的光标位置
int lastfromIndex=text.indexOf(searchjtf.getText(),0);
//获得查找后的位置
jta.setCaretPosition(lastfromIndex);
jta.moveCaretPosition(lastfromIndex+searchjtf.getText().length());
}
});
replace.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JPanel replacePanel=new JPanel();
JLabel replaceLabel=new JLabel("要替换的内容");
JTextField inputText=new JTextField(10);
JLabel swapLabel=new JLabel("替换为:");
JTextField changetoText=new JTextField(10);
replacePanel.add(replaceLabel);
replacePanel.add(inputText);
replacePanel.add(swapLabel);
replacePanel.add(changetoText);
JOptionPane.showMessageDialog(null,replacePanel);
String text=jta.getText();//获得整个文本内容
String changeText=text.replaceFirst(inputText.getText(),changetoText.getText());//获得替换后的内容
jta.setText(changeText);
}
});
aboutWb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JPanel jp=new JPanel();
JTextArea aboutWbjta=new JTextArea(5,5);
aboutWbjta.setText("作者:阿伟;日期:2013.4.4");
jp.add(aboutWbjta);
JOptionPane.showMessageDialog(null,aboutWbjta);
/*aboutWbjta.setVisible(true);
aboutWbjta.show();*/
}
});
}
public void setTitle() {
this.setTitle(str+" - 记事本");
}
public static void main(String[] args) {
Wb wb=new Wb();
}
}
高级语言案例分析_文本编辑器
最新推荐文章于 2024-04-27 20:20:22 发布