目录
目标
利用Java实现一个简单的word功能,其中包括新建文件,保存文件,打开文件,更改颜色等等功能。
功能框架
- 文件功能:新建文件,打开文件,保存文件,退出文件
- 编辑功能:复制文件,剪切文件,粘贴文件,更改颜色
- 帮助:关于(拥有相关说明)
源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class TextEditorFrame<EditName> extends JFrame {
File file=null;
/**默认颜色为黑色*/
Color color=Color.black;
TextDAL dal=null;
TextEditorFrame(TextDAL dal){
this.dal=dal;
//文本部分
initTextPane();
//菜单部分
initMenu();
//对话框部分
initAboutDialog();
//工具条部分
initToolBar();
}
void initTextPane(){//将文本框放入又滚动对象,并加入到Frame中
getContentPane().add(new JScrollPane(text));
}
JTextPane text=new JTextPane();
/**文件选择框*/
JFileChooser fileChooser=new JFileChooser();
/**颜色选择对话框*/
JColorChooser colorChooser=new JColorChooser();
/**关于对话框*/
JDialog about=new JDialog();
/**菜单*/
JMenuBar menuBar=new JMenuBar();
/**一级菜单包括以下三个选项*/
JMenu[] menus=new JMenu[]{new JMenu("文件"),new JMenu("编辑"),new JMenu("帮助")};
/**设置菜单的子选项*/
JMenuItem[][] menuItem =new JMenuItem[][]{{
new JMenuItem("新建文件"),
new JMenuItem("打开文件"),
new JMenuItem("保存文件"),
new JMenuItem("退出文件")},
{new JMenuItem("复制文件"),
new JMenuItem("剪切文件"),
new JMenuItem("粘贴文件"),
new JMenuItem("颜色")},
{new JMenuItem("关于")}
};
/**初始化菜单,将的三个一级菜单选项加入到menuBar中,并且给每一个子菜单添加监听事件*/
void initMenu(){
for(int i=0;i<menus.length;i++) {
menuBar.add(menus[i]);
for (int j = 0; j < menuItem[i].length; j++) {
menus[i].add(menuItem[i][j]);
menuItem[i][j].addActionListener(action);
}
}
//将menuBar加入到面板中
this.setJMenuBar(menuBar);
}
/**定义action监听事件*/
ActionListener action=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JMenuItem mi = (JMenuItem) e.getSource();
String id = mi.getText();
if (id.equals("新建文件")) {text.setText("");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.showOpenDialog(TextEditorFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
saveFile();
}}
else if (id.equals("退出文件")) {System.exit(0);}
else if (id.equals("剪切文件")) {text.cut();}
else if (id.equals("复制文件")) {text.copy();}
else if (id.equals("粘贴文件")) {text.paste();}
else if (id.equals("颜色")) {
color = JColorChooser.showDialog(TextEditorFrame.this, "", color);
text.setForeground(color);}
else if (id.equals("关于")) {
about.setSize(100, 50);
about.setVisible(true);
}
}
};
/**定义保存文件的函数*/
void saveFile(){
String content=text.getText();
dal.save(file,content);
}
/**定义打开文件的函数*/
void openFile(){
String content=dal.read(file);text.setText(content);
}
void initAboutDialog(){
about.getContentPane().add(new JLabel("<html><body>简单的文本编辑器1.0<br>CSDN作者:偷掉月亮的阿硕<br>本片文章代码改编自Java程序设计唐大仕老师的代码,如果侵犯您的权益请联系<body></html>"));
//更改窗口为模式对话框
about.setModal(true);
about.setSize(100,50);
}
/**定义工具栏,并且可以添加相应的图片*/
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"))
};
/**定义工具按钮的相关功能,加入到组件中*/
void initToolBar(){
for (int i=0;i<buttons.length;i++){
toolBar.add(buttons[i]);
}
buttons[0].setToolTipText("复制文件");
buttons[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text.copy();
}
});
buttons[1].setToolTipText("剪切文件");
buttons[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text.cut();
}
});
buttons[2].setToolTipText("粘贴文件");
buttons[2].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text.paste();
}
});
this.getContentPane().add(toolBar,BorderLayout.NORTH);
//当且仅当鼠标悬停在该按钮时,才会显示出该按钮的边框
toolBar.setRollover(true);
}
}
class TextEditorApp2{
public static void main(String[] args){
//分发线程,保证不会发生死锁
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TextDAL dal=new FileTextDAL();
TextEditorFrame f=new TextEditorFrame(dal);
f.setTitle("简单的编辑器");
f.setSize(800,600);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
interface TextDAL{
String read(File file);
void save(File file,String text);
}
/**logger是一个日志文件,记录相应的操作*/
class FileTextDAL implements TextDAL{
@Override
public String read(File file) {
logger.log(Level.INFO,"read","reab..."+file.getPath());
try{
FileReader fr=new FileReader(file);
int len=(int)file.length();
char[] buffer=new char[len];
fr.read(buffer,0,len);
fr.close();
return new String(buffer);
}catch (Exception e){
e.printStackTrace();
logger.log(Level.SEVERE,null,e);
}
return "";
}
@Override
public void save(File file, String text) {
logger.log(Level.INFO,"save","save..."+file.getPath());
try{
FileWriter fw=new FileWriter(file);
fw.write(text);
fw.close();
}catch (Exception ex){
ex.printStackTrace();
logger.log(Level.SEVERE,null,ex);
}
}
Logger logger= Logger.getLogger(FileTextDAL.class.getName());
{
try{
FileHandler handler=new FileHandler("TextEditorApp2.log");
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
}catch (IOException ex){}
}
}
效果展示:
更多的效果请大家复制源码去体验
说明
- 代码中设置了日志,在哪里看到日志? 答:日志在运行结果框中查看,并没有把日志写入文件中。如下图:
- 复制,剪切,粘贴按钮在哪里? 答:代码中设置了插入图片,大家可以根据设置的图片自行添加,而将鼠标悬停之后可以发现这三个按钮。如下图:
- 作者使用的是Idea,Eclipse也可以直接复制粘贴运行,其他软件并没有进行尝试。