java實現多文檔文本編輯器
編輯器界面比較簡單,但功能較為全面,可以保存,另存,打開文件,復制,粘貼,撤銷,剪切,字體設置,部分文字的字體設置,部分文字的顏色設置,字數統計,查找,替換等功能。
package 編輯器;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.io.*;
import javax.swing.event.*;
import sun.audio.*;
public class FontDialog extends JDialog implements ActionListener,ItemListener
{ static final int OK=
1,NO=
0;
public int message=-
1;
private JButton OK_but,NO_but;
private Choice cho01,cho02,cho03,cho04;
public String str01,str02;
public FontDialog(JFrame f,String str,
boolean b){
super(f,str,b);Container cont = getContentPane();Font []font = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();String []fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); cho01 =
new Choice();
for(
int i = fontNames.length-
1;i>=
0;i--){cho01.add(fontNames[i]);}cho02 =
new Choice();cho02.setEnabled(
true);
for(
int s =
10;s<
73;s=s+
2){cho02.add(Integer.toString(s));}cho02.select(
5);cho01.addItemListener(
this);cho02.addItemListener(
this);cont.setLayout(
new FlowLayout(FlowLayout.LEFT));JLabel jll =
new JLabel(
"字體",JLabel.LEFT);cont.add(jll);cont.add(cho01);jll =
new JLabel(
"大小",JLabel.LEFT);cont.add(jll);cont.add(cho02);OK_but=
new JButton(
"確定");OK_but.addActionListener(
this);NO_but=
new JButton(
"取消");NO_but.addActionListener(
this);cont.add(OK_but);cont.add(NO_but);setBounds(
400,
200,
380,
120);setResizable(
false); setVisible(
true);}
public void actionPerformed(ActionEvent ev){
if(ev.getSource()==OK_but){message=OK;setVisible(
false);}
if(ev.getSource()==NO_but){ message=NO;setVisible(
false);setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);}}
public void itemStateChanged(ItemEvent e){str01 = cho01.getItem(cho01.getSelectedIndex());str02 = cho02.getItem(cho02.getSelectedIndex());}
}
package 編輯器;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;
import copy.TextEditBox;
import copy1.FontDialog;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JMenu;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.Icon;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JToolBar;
public class MainView extends JFrame implements ActionListener,FocusListener,
MouseListener,CaretListener,UndoableEditListener {private JPanel contentPane;
private UndoManager manager;
/*** Create the frame.*/private JComboBox com1,com2;String filename;
private int key, j,count;
private JLabel lblStatus;
private File file01,file02;
private JFileChooser jfc;
private JTextPane jta[]=
new JTextPane[
20],jep;
private int close_i =
0,i =
0,k=
0,TabNum;
private JScrollPane jsp[]=
new JScrollPane[
20];
private JTabbedPane tab1,tab;
private JButton b3,b1,b6,button_1,button3,button9,button_2,button_3,button_4,button_5,button_7,button8,btn1,btn2;
private JMenuItem m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,mnNew3,mnNew4,mnNew2,item,item1;
private JToolBar toolBar;
private JTextField tf_search;
private JButton button_next;
private JMenuItem menuItem_3;SimpleAttributeSet set=
new SimpleAttributeSet();SimpleAttributeSet set1=
new SimpleAttributeSet();
int start,length,start1,length1;
public MainView() {setTitle(
"多頁面文本編輯器");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(
100,
100,
780,
509);JMenuBar menuBar =
new JMenuBar();setJMenuBar(menuBar);JMenu menu =
new JMenu(
"文件");menu.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
16));menuBar.add(menu);mnNew2 =
new JMenuItem(
"\u65B0\u5EFA");mnNew2.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menu.add(mnNew2);mnNew2.addActionListener(
this);m1 =
new JMenuItem(
"\u6253\u5F00");m1.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menu.add(m1);m1.addActionListener(
this);m2 =
new JMenuItem(
"\u4FDD\u5B58");m2.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menu.add(m2);m2.addActionListener(
this);menuItem_3 =
new JMenuItem(
"\u53E6\u5B58\u4E3A");menuItem_3.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menuItem_3.addActionListener(
this);menu.add(menuItem_3);mnNew3 =
new JMenuItem(
"\u5173\u95ED\u5F53\u524D\u9875");mnNew3.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menu.add(mnNew3);item =
new JMenuItem(
"\u5173\u95ED\u5168\u90E8\u9875");item.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menu.add(item);item.addActionListener(
this);mnNew3.addActionListener(
this);JMenu mnNewMenu =
new JMenu(
"編輯");mnNewMenu.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
16));menuBar.add(mnNewMenu);m6 =
new JMenuItem(
"\u64A4\u9500");m6.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));mnNewMenu.add(m6);m6.addActionListener(
this);mnNew4 =
new JMenuItem(
"\u526A\u5207");mnNew4.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));mnNewMenu.add(mnNew4);mnNew4.addActionListener(
this);m3 =
new JMenuItem(
"\u590D\u5236");m3.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));mnNewMenu.add(m3);m3.addActionListener(
this);m4 =
new JMenuItem(
"\u7C98\u8D34");m4.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));mnNewMenu.add(m4);m4.addActionListener(
this);m7 =
new JMenuItem(
"\u67E5\u627E");m7.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));mnNewMenu.add(m7);item1 =
new JMenuItem(
"\u5168\u9009");item1.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));item1.addActionListener(
this);mnNewMenu.add(item1);m7.addActionListener(
this);JMenu mnNewMenu_1 =
new JMenu(
"格式");mnNewMenu_1.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
16));menuBar.add(mnNewMenu_1);m8 =
new JMenuItem(
"\u5B57\u4F53");m8.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));mnNewMenu_1.add(m8);m8.addActionListener(
this);m9 =
new JMenuItem(
"\u5B57\u4F53\u989C\u8272");m9.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));mnNewMenu_1.add(m9);m9.addActionListener(
this);m10 =
new JMenuItem(
"\u80CC\u666F\u989C\u8272");m10.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));mnNewMenu_1.add(m10);JMenu menu_1 =
new JMenu(
"\u5DE5\u5177");menu_1.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
16));menuBar.add(menu_1);JMenuItem menuItem =
new JMenuItem(
"\u5B57\u6570\u7EDF\u8BA1");menuItem.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menu_1.add(menuItem);menuItem.addActionListener(
new ActionListener() {
@Overridepublic void actionPerformed(ActionEvent arg0) {
int count=
0;
for(
int i=
0;i<jep.getText().length();i++){
if(!jep.getText().substring(i,i+
1).equals(
" ")){ count++; } } JOptionPane.showMessageDialog(MainView.
this,
"文本框中一共有"+count+
"個字符!"); }});JMenuItem menuItem_1 =
new JMenuItem(
"\u67E5\u627E");menuItem_1.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menu_1.add(menuItem_1);menuItem_1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {k=
0;
new MessageJDialog(); button_next.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) { String str_search=tf_search.getText();
int len = str_search.length();
for (
int i = key; i < jep.getText().length() - len +
1; i++) { String str_record = jep.getText().substring(i, i + len);
if (str_record.equals(str_search)) { key = i +
1; jep.requestFocus(); jep.select(i, i + len);
return; } } }});}});JMenuItem menuItem_2 =
new JMenuItem(
"\u66FF\u6362");menuItem_2.setFont(
new Font(
"Microsoft YaHei UI", Font.PLAIN,
14));menu_1.add(menuItem_2);menuItem_2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) { String str_replace=JOptionPane.showInputDialog(MainView.
this,
"請輸入你要替換的字符串:" ); String str_replacelater=JOptionPane.showInputDialog(MainView.
this,
"請輸入你要用來替換的內容:" );
int len=str_replace.length();
for(
int i=
0;i<jep.getText().length()-len+
1;i++){ String str_record=jep.getText().substring(i, i+len);
if(str_record.equals(str_replace)){ Document doc=jep.getDocument();
try {doc.remove(i, len);doc.insertString(i, str_replacelater,set);}
catch (BadLocationException e1) {e1.printStackTrace();}} } } });m10.addActionListener(
this);contentPane =
new JPanel();contentPane.setLayout(
null);setContentPane(contentPane);ImageIcon image=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\save.gif");image.setImage(image.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image1=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\open.gif");image1.setImage(image1.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image11=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\cut.gif");image11.setImage(image11.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image2=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\copy.gif");image2.setImage(image2.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image3=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\Font.png");image3.setImage(image3.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image4=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\FontColor.png");image4.setImage(image4.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image5=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\加粗.png");image5.setImage(image5.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image6=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\new.gif");image6.setImage(image6.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image7=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\斜體.png");image7.setImage(image7.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));ImageIcon image8=
new ImageIcon(
"D:\\eclipse-workspace\\04107204劉聰\\下劃線.png");image8.setImage(image8.getImage().getScaledInstance(
25,
25, Image.SCALE_DEFAULT));TabNum=
0;
for(
int i=TabNum;i>
0;i--){ jta[TabNum-
1]=
new JTextPane();jsp[TabNum-
1]=
new JScrollPane(jta[TabNum-
1]); jta[TabNum-
1].getDocument().addUndoableEditListener(manager);jta[TabNum-
1].addCaretListener(
this);jsp[TabNum-
1].setViewportView(jta[TabNum-
1]);
if(TabNum!=
0){tab.addTab(
"無標題"+i,jsp[TabNum-
1]);} }tab =
new JTabbedPane(JTabbedPane.TOP);tab.setBounds(
24,
46,
693,
390);contentPane.add(tab);GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontsName = ge.getAvailableFontFamilyNames(); String sizestr[] = {
"12",
"14",
"16",
"18",
"20",
"22" ,
"24",
"26",
"28",
"30",
"32",
"34",
"36",
"38",
"40",
"42",
"44",
"46",
"48",
"50",
"52",
"54",
"56",
"58",
"60",
"62",
"64",
"66",
"68",
"70"}; toolBar =
new JToolBar();toolBar.setBounds(
0,
0,
774,
25);contentPane.add(toolBar);b6 =
new JButton((Icon)
null);toolBar.add(b6);b6.setIcon(image6);b1 =
new JButton();toolBar.add(b1);b1.setIcon(image1);b3 =
new JButton();toolBar.add(b3);b3.setIcon(image);button3 =
new JButton((Icon)
null);toolBar.add(button3);button3.setIcon(image11);button9 =
new JButton((Icon)
null);toolBar.add(button9);button9.setIcon(image2);JButton button5 =
new JButton((Icon)
null);toolBar.add(button5);button5.setIcon(image5);button5.addActionListener(
new ActionListener() {
@Overridepublic void actionPerformed(ActionEvent e) {String fontname = (String)com1.getSelectedItem();
int size =Integer.parseInt((String)com2.getSelectedItem()); Font font = jep.getFont();
int style = font.getStyle(); style = style ^
1; jep.setFont(
new Font(fontname, style, size)); } });com1 =
new JComboBox(fontsName);toolBar.add(com1);com2 =
new JComboBox(sizestr); toolBar.add(com2);com2.setEditable(
true);com1.addActionListener(
new ActionListener() {
@Overridepublic void actionPerformed(ActionEvent arg0) {String fontname = (String)com1.getSelectedItem();Font font = jep.getFont();
int style = font.getStyle();
int size = Integer.parseInt((String)com2.getSelectedItem()); jep.setFont(
new Font(fontname, style, size)); }});com2.addActionListener(
new ActionListener() {
@Overridepublic void actionPerformed(ActionEvent e) {String fontname = (String)com1.getSelectedItem();
int size = Integer.parseInt((String)com2.getSelectedItem()); Font font = jep.getFont();
int style = font.getStyle(); jep.setFont(
new Font(fontname, style, size)); } });JButton button_7_1 =
new JButton((Icon)
null);toolBar.add(button_7_1);button_7_1.setIcon(image7);button_7_1.addActionListener(
new ActionListener() {
@Overridepublic void actionPerformed(ActionEvent e) {String fontname = (String)com1.getSelectedItem();Font font = jep.getFont();
int style = font.getStyle();
int size = font.getSize(); style = style ^
2; jep.setFont(
new Font(fontname, style, size)); } });btn1 =
new JButton();toolBar.add(btn1);btn1.setFont(
new Font(
"宋體", Font.PLAIN,
12));btn1.setText(
"A+");btn1.addActionListener(
new ActionListener() {
@Overridepublic void actionPerformed(ActionEvent e) {String fontname = (String)com1.getSelectedItem();
int size = Integer.parseInt((String)com2.getSelectedItem())+
2; com2.setSelectedItem(String.valueOf(size));Font font = jep.getFont();
int style = font.getStyle(); jep.setFont(
new Font(fontname, style, size)); }});JButton btn2_1 =
new JButton((Icon)
null);toolBar.add(btn2_1);btn2_1.setText(
"A-");btn2_1.setFont(
new Font(
"宋體", Font.PLAIN,
12));btn2_1.addActionListener(
new ActionListener() {
@Overridepublic void actionPerformed(ActionEvent e) {String fontname = (String)com1.getSelectedItem();
int size = Integer.parseInt((String)com2.getSelectedItem())-
2;com2.setSelectedItem(String.valueOf(size));Font font = jep.getFont();
int style = font.getStyle(); jep.setFont(
new Font(fontname, style, size)); }});button8 =
new JButton((Icon)
null);toolBar.add(button8);button8.setIcon(image4);button8.addActionListener(
this);button_7_1.addActionListener(
this);button5.addActionListener(
this);button9.addActionListener(
this);button3.addActionListener(
this);b3.addActionListener(
this);b1.addActionListener(
this);b6.addActionListener(
this);manager=
new UndoManager();tab.addMouseListener(
new MouseAdapter() {
public void mouseClicked(MouseEvent e) {p(e);}
public void p(MouseEvent e) {
for(
int i=
0;i<=TabNum;i++) {Rectangle rect=tab.getBoundsAt(i);
if(rect.contains(e.getX(),e.getY())) {count=i;}}}});}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1||e.getSource()==m1) {jta[TabNum]=
new JTextPane();jta[TabNum].addFocusListener(
this); jta[TabNum].getDocument().addUndoableEditListener(manager);jta[TabNum].addCaretListener(
this); dakai(jta[TabNum]);jsp[TabNum]=
new JScrollPane(jta[TabNum],JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); tab.addTab(filename,jta[TabNum]);jep=jta[
0];tab.setVisible(
true);j=TabNum-
1;}
else if(e.getSource()==b6||e.getSource()==mnNew2) {TabNum++;jta[TabNum-
1]=
new JTextPane();jta[TabNum-
1].addFocusListener(
this);jsp[TabNum-
1]=
new JScrollPane(jta[TabNum-
1],JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jta[TabNum-
1].getDocument().addUndoableEditListener(manager);jta[TabNum-
1].addCaretListener(
this);jsp[TabNum-
1].setViewportView(jta[TabNum-
1]); tab.addTab(
"無標題"+TabNum,jsp[TabNum-
1]);jep=jta[
0]; tab.setVisible(
true); jta[TabNum-
1].setFont(
new Font((String) com1.getSelectedItem(),Font.PLAIN,Integer.parseInt((String)com2.getSelectedItem())));}
else if(e.getSource()==b3||e.getSource()==m2) {baocun(jep);}
else if(e.getSource()==menuItem_3){lingcun(jep);}
else if(e.getSource()==mnNew3) {close(jep);}
else if(e.getSource()==item) {tab.setVisible(
false);}
else if(e.getSource()==m3||e.getSource()==button9) {jep.copy();}
else if(e.getSource()==m4) {jep.paste();}
else if(e.getSource()==item1) {jep.selectAll();}
else if(e.getSource()==mnNew4||e.getSource()==button3) {jep.cut();}
else if(e.getSource()==m8) {
try {wenzi(jep);}
catch (BadLocationException e1) {e1.printStackTrace();}}
else if(e.getSource()==m6) {Undo();}
else if(e.getSource()==m9||e.getSource()==button8) {
try {color(jep);}
catch (BadLocationException e1) {e1.printStackTrace();}}
else if(e.getSource()==m10) {
try {backcolor(jep);}
catch (BadLocationException e1) {e1.printStackTrace();}}}
public void wenzi(JTextPane jep2)
throws BadLocationException{ FontDialog fdg =
new FontDialog(
this,
"文字設置",
true);start=jep2.getSelectionStart();length= jep2.getSelectionEnd()-start;String ss=jep2.getSelectedText();
if(fdg.message==
1){ StyleConstants.setFontSize(set, Integer.parseInt(fdg.str02));StyleConstants.setFontFamily(set, fdg.str01);Document doc=jep2.getDocument();doc.remove(start, length);doc.insertString(start, ss, set);} }
public void backcolor(JTextPane jep2)
throws BadLocationException {Color newColor=JColorChooser.showDialog(
this,
"文字顏色選擇器",m9.getBackground());
int start=jep2.getSelectionStart();
int length= jep2.getSelectionEnd()-start;String ss=jep2.getSelectedText();StyleConstants.setBackground(set, newColor);Document doc=jep2.getDocument();doc.remove(start, length);doc.insertString(start, ss, set);}
public void color(JTextPane jep2)
throws BadLocationException {Color newColor=JColorChooser.showDialog(
this,
"文字顏色選擇器",m9.getBackground());start1=jep2.getSelectionStart();length1= jep2.getSelectionEnd()-start1;String ss=jep2.getSelectedText();
if(start1==start&&length==length1) {StyleConstants.setForeground(set, newColor);Document doc=jep2.getDocument();doc.remove(start, length);doc.insertString(start, ss, set);}
else {Font font=jep2.getFont();StyleConstants.setFontFamily(set1, font.getFontName());StyleConstants.setFontSize(set1, font.getSize());StyleConstants.setForeground(set1, newColor);Document doc=jep2.getDocument();doc.remove(start, length);doc.insertString(start, ss, set1);}}
private void dakai(JTextPane jta2)//打開{ jfc =
new JFileChooser();
int se = jfc.showOpenDialog(
this);
if(se==JFileChooser.APPROVE_OPTION){
try{file01=jfc.getSelectedFile();filename =file01.getName(); FileReader fr =
new FileReader(file01);BufferedReader rd =
new BufferedReader(fr);String s =
null;Byte []buf =
new Byte[
64];String open =
null;
while((s=rd.readLine())!=
null){jta2.replaceSelection(s+
"\n");}}
catch(IOException exx){exx.printStackTrace();}}}
private void lingcun(JTextPane jep2)//另存{jfc =
new JFileChooser();
int se = jfc.showSaveDialog(
this);
if(se==JFileChooser.APPROVE_OPTION){
try{String sr_s = jfc.getSelectedFile().getAbsolutePath();FileOutputStream fos =
new FileOutputStream(
new File(sr_s));OutputStreamWriter osw =
new OutputStreamWriter(fos);String saveString=jep2.getText().replaceAll(
"\n",
"\r\n"); osw.write(saveString);osw.flush();osw.close(); }
catch(IOException es){es.printStackTrace();} }}
private void baocun(JTextPane jep2)//保存{
try{String sr_s = jfc.getSelectedFile().getAbsolutePath();FileOutputStream fos =
new FileOutputStream(
new File(sr_s));OutputStreamWriter osw =
new OutputStreamWriter(fos);String saveString=jep2.getText().replaceAll(
"\n",
"\r\n"); osw.write(saveString);osw.flush();osw.close(); }
catch(IOException es){es.printStackTrace();} }
public void close(JTextPane jep2) {tab.remove(count);}
private void Undo(){
try{manager.undo();}
catch(CannotRedoException e1){} }
public static void main(String []args) {MainView v=
new MainView();v.setVisible(
true);}
@Overridepublic void undoableEditHappened(UndoableEditEvent arg0) {}
@Overridepublic void caretUpdate(CaretEvent arg0) {}
@Overridepublic void mouseClicked(MouseEvent arg0) {}
@Overridepublic void mouseEntered(MouseEvent arg0) {}
@Overridepublic void mouseExited(MouseEvent arg0) {}
@Overridepublic void mousePressed(MouseEvent arg0) {}
@Overridepublic void mouseReleased(MouseEvent arg0) {}
@Overridepublic void focusGained(FocusEvent et) {
if(et.getSource()==jta[
0])jep=jta[
0];
if(et.getSource()==jta[
1])jep=jta[
1];
if(et.getSource()==jta[
2])jep=jta[
2];
if(et.getSource()==jta[
3])jep=jta[
3];
if(et.getSource()==jta[
4])jep=jta[
4];
if(et.getSource()==jta[
5])jep=jta[
5];
if(et.getSource()==jta[
6])jep=jta[
6];
if(et.getSource()==jta[
7])jep=jta[
7];
if(et.getSource()==jta[
8])jep=jta[
8];
if(et.getSource()==jta[
9])jep=jta[
9]; }
private class MessageJDialog extends JDialog { private JLabel lable_tip;
private JPanel panel_next =
new JPanel();
private JPanel panel_search =
new JPanel();
private JPanel panel_tip =
new JPanel();
public MessageJDialog() {
super(MainView.
this,
"查找");
this.setSize(
300,
170);
this.setLocation(MainView.
this.getX() +
200, MainView.
this.getY() +
200); getContentPane().setLayout(
new GridLayout(
3,
1)); ImageIcon imageIcon =
new ImageIcon(
"img/search.png"); lable_tip =
new JLabel(
"請輸入你要查找的字符串:", imageIcon, JLabel.LEFT); panel_tip.add(lable_tip); getContentPane().add(panel_tip); tf_search =
new JTextField(
20); panel_search.add(tf_search); getContentPane().add(panel_search); button_next =
new JButton(
"查找下一個"); panel_next.add(button_next); getContentPane().add(panel_next);
this.setVisible(
true); } }
public void focusLost(FocusEvent arg0) {}
}
總結
以上是生活随笔為你收集整理的Java实现多文档文本编辑器的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。