import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
//class
class TextEditorFrame extends JFrame
{
File file = null;
Color color = Color.black;
GraphicsEnvironment getFont = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font []fonts = getFont.getAllFonts();
//1.2
JTextPane text = new JTextPane();
JFileChooser filechooser = new JFileChooser();
JColorChooser colorchooser = new JColorChooser();
JDialog about = new JDialog(this);
JMenuBar menubar = new JMenuBar();
//主窗体初始化
TextEditorFrame()
{
initTextPane();//面板
initMenu();//菜单
initAboutDialog();//关于对话框
initToolBar();//工具栏
}
//面板初始化
void initTextPane()
{
setFont(new Font("Times New Roman",Font.PLAIN,12));
getContentPane().add( new JScrollPane(text));
}
//---------------------------------------------------------------------------
//菜单的定义
//super Menue
JMenu [] menus= new JMenu[]
{
new JMenu("文件"),
new JMenu("编辑"),
new JMenu("工具"),
new JMenu("帮助"),
};
//sub Menue
JMenuItem menuitems [][] =new JMenuItem[][]
{
{
new JMenuItem("新建"),
new JMenuItem("打开..."),
new JMenuItem("保存..."),
new JMenuItem("Exit")
},
{
new JMenuItem("Copy"),
new JMenuItem("Cut"),
new JMenuItem("Paste"),
new JMenuItem("选择全部"),
new JMenuItem("Color...")
},
{
new JMenuItem("MS 记事本"),
new JMenuItem("MS 计算器")
},
{
new JMenuItem("About")
}
};
//-----------------------------------------------------------------------------
//菜单的初始化
void initMenu()
{
for(int i=0;i<menus.length;i++)
{
menubar.add(menus[i]);
for(int j=0;j<menuitems[i].length;j++)
{
menus[i].add(menuitems[i][j]);
menuitems[i][j].addActionListener(action);
}
}
this.setJMenuBar(menubar);
}
//-----------------------------------------------------------------------------
//事件处理
ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JMenuItem mi = (JMenuItem)e.getSource();
String id = mi.getText();
if(id.equals("新建")){
Date date=new Date();
text.setText(date.toString());
file = null;
}else if(id.equals("打开...")){
if(file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser.showOpenDialog(TextEditorFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
openFile();
}
}
else if(id.equals("保存...")){
if(file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser.showSaveDialog(TextEditorFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
saveFile();
}}else if(id.equals("Exit")){
System.exit(0);
}else if(id.equals("Cut")){
text.cut();
}else if(id.equals("Copy")){
text.copy();
}else if(id.equals("Paste")){
text.paste();
}else if(id.equals("选择全部")){
text.selectAll();
}else if(id.equals("Color...")){
color = JColorChooser.showDialog(TextEditorFrame.this,"",color);
text.setForeground(color);
}else if(id.equals("MS 记事本")){
try{
String command = "notepad.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}else if(id.equals("MS 计算器")){
try{
String command = "calc.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}else if(id.equals("About")){
about.setSize(250,150);
about.show();
}
}
};
//-------------------------------------------------------------------------------
//保存文件
void saveFile()
{
try{
FileWriter fw = new FileWriter(file);
fw.write(text.getText());
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//---------------------------------------------------------------
//打开文件
void openFile()
{
try{
FileReader fr = new FileReader(file);
int len = (int) file.length();
char [] buffer = new char[len];
fr.read(buffer,0,len);
fr.close();
text.setText(new String(buffer));
}
catch(Exception e)
{
e.printStackTrace();
}
}
//--------------------------------------------------------------
//关于对话框
void initAboutDialog()
{
about.setTitle("关于... ");
about.getContentPane().setBackground(Color.green );
about.getContentPane().add(new JLabel(" 简易文字编辑器!作者:孙志峰"));
about.setModal(true);
}
//---------------------------------------------------------------
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
JToolBar toolbar = new JToolBar();
JButton [] buttons = new JButton[]
{
new JButton("",new ImageIcon("copy.jpg")),
new JButton("",new ImageIcon("cut.jpg")),
new JButton("",new ImageIcon("paste.jpg")),
new JButton("",new ImageIcon("MS1.jpg")),
new JButton("",new ImageIcon("MS2.jpg")),
};
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//-------------------------------------------------------------------------------------------------------------------
//工具栏
void initToolBar()
{
for(int i=0; i<buttons.length;i++)
toolbar.add(buttons[i]);
buttons[0].setToolTipText("copy");
buttons[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.copy();
}
});
buttons[1].setToolTipText("cut");
buttons[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.cut();
}
});
buttons[2].setToolTipText("paste");
buttons[2].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.paste();
}
});
buttons[3].setToolTipText("MS 记事本");
buttons[3].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
String command = "notepad.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{
}
}
});
buttons[4].setToolTipText("MS 计算器");
buttons[4].addActionListener(new ActionListener()
没有合适的资源?快使用搜索试试~ 我知道了~
java-editor.zip_文本编辑_文本编辑器java
共18个文件
class:9个
jpg:6个
java:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 192 浏览量
2022-09-21
21:25:59
上传
评论
收藏 171KB ZIP 举报
温馨提示
Java编写的文本编辑器,实现了文本编辑器的基本功能,可以参考
资源推荐
资源详情
资源评论
收起资源包目录
java-editor.zip (18个子文件)
MS1.JPG 1KB
TextEditorFrame$3.class 609B
images
winter.jpg 14KB
Thumbs.db 11KB
winter.bmp 147KB
TextEditorFrame$4.class 611B
MS2.JPG 1KB
cut.JPG 737B
TextEditorFrame$6.class 716B
TextEditorFrame.class 5KB
copy.JPG 1KB
TextEditorApp.class 670B
TextEditorFrame$1.class 3KB
TextEditorFrame$2.class 610B
paste.JPG 1KB
TextEditorFrame$5.class 719B
JSplashWindowEx.class 2KB
TextEditorApp.java 10KB
共 18 条
- 1
资源评论
寒泊
- 粉丝: 74
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功