/*
* 文 件 名: EasyView.java
*/
package view.main;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
public class EasyView
{
private JFrame frmEasyview;
private JTextPane textPane;
private static final String KEYWORD_PUBLIC="public"; //文本中的关键字,会显示为红色
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
EasyView window = new EasyView();
window.frmEasyview.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public EasyView()
{
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frmEasyview = new JFrame();
frmEasyview.getContentPane().setBackground(Color.WHITE);
frmEasyview.setForeground(Color.BLACK);
frmEasyview.setTitle("EasyView");
frmEasyview.setBounds(100, 100, 673, 492);
frmEasyview.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmEasyview.getContentPane().setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
frmEasyview.getContentPane().add(menuBar,BorderLayout.NORTH);
//打开菜单
JMenu mnNewMenu = new JMenu("\u6587\u4EF6");
mnNewMenu.setFont(new Font("微软雅黑", Font.PLAIN, 12));
mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
menuBar.add(mnNewMenu);
MyMenuItemsListener mmil=new MyMenuItemsListener();
JMenuItem mntmo = new JMenuItem("\u6253\u5F00(O)");
mntmo.setFont(new Font("微软雅黑", Font.PLAIN, 12));
//设置不同的actioncommand来监听不同的action
mntmo.setActionCommand("open");
//设置快捷键
mntmo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
//设置助记键
mntmo.setMnemonic(KeyEvent.VK_O);
mntmo.addActionListener( mmil );
mnNewMenu.add(mntmo);
JMenuItem mntms = new JMenuItem("\u4FDD\u5B58(S)");
mntms.setFont(new Font("微软雅黑", Font.PLAIN, 12));
mntms.addActionListener( mmil );
//设置不同的actioncommand来监听不同的action
mntms.setActionCommand("save");
//设置快捷键
mntms.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
//设置助记键
mntms.setMnemonic(KeyEvent.VK_S);
mnNewMenu.add(mntms);
//帮助菜单
JMenu mnNewMenu_1 = new JMenu("\u5E2E\u52A9");
mnNewMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
menuBar.add(mnNewMenu_1);
JMenuItem menuItem_2 = new JMenuItem("\u5173\u4E8E");
mnNewMenu_1.add(menuItem_2);
//使用txt面板,可以对区域文本进行格式设置
textPane = new JTextPane();
//添加滚动条
JScrollPane scroll =new JScrollPane(textPane);
frmEasyview.getContentPane().add(scroll, BorderLayout.CENTER);
}
/**
*
* 内部类-实现监听
*
*/
class MyMenuItemsListener implements ActionListener
{
File openFile;
@Override
public void actionPerformed(ActionEvent e)
{
//打开新文件
if( "open".equals(e.getActionCommand()) )
{
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY );
jfc.showOpenDialog((JMenuItem)e.getSource());
openFile=jfc.getSelectedFile();
if(null==openFile) return;
//不考虑字符编码问题,读取txt文本
if( openFile.getName().endsWith("txt") )
{
StyledDocument doc_public;
String str;
String _2str[];
StringBuilder paneSb=new StringBuilder();
try
{
InputStreamReader in = new InputStreamReader(new FileInputStream(openFile));
int len=0;
char [] ar = new char[10240];
StringBuilder sb =new StringBuilder();
while((len=in.read(ar))!=-1)
{
sb.append(new String(ar,0,len) );
}
in.close();
str=sb.toString();
doc_public=textPane.getStyledDocument();
Style _public=doc_public.addStyle("_public", null);
StyleConstants.setForeground(_public, Color.RED);
while(true)
{
_2str = str.split("\\bpublic\\b",2); // //b是单词边界匹配这样不会匹配到publicxx,每次分割成两组,插入第一组字符串
if(_2str.length==2)
{
textPane.getStyledDocument().insertString(paneSb.length(), _2str[0], null);
paneSb.append(_2str[0]);
textPane.getStyledDocument().insertString(paneSb.length(), KEYWORD_PUBLIC, _public);
paneSb.append( KEYWORD_PUBLIC );
str=_2str[1];
}
else // str中不再含有'public'
{
textPane.getStyledDocument().insertString(paneSb.length(), _2str[0], null);
break;
}
}
}catch (FileNotFoundException e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage());
}
catch (IOException e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage());
}
catch (BadLocationException e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
else
{
JOptionPane.showMessageDialog((JMenuItem)e.getSource(), "文件不合法!");
}
}
else if( "save".equals(e.getActionCommand()) ) //保存修改的文件
{
OutputStreamWriter out;
StyledDocument styleDoc;
try
{
out = new OutputStreamWriter(new FileOutputStream(openFile));
styleDoc=textPane.getStyledDocument();
out.write(styleDoc.getText(0, styleDoc.getLength()));
out.flush();
out.close();
}catch (FileNotFoundException e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage());
}
catch (IOException e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage());
}
catch (BadLocationException e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
}
}
JAVA_简单的关键字文本编辑器
最新推荐文章于 2021-02-12 21:20:42 发布