2024年7月3日发(作者:)
Mini记事本源程序
目录
1.程序入口 ........................................................................................................ 1
2.工具栏按钮类 ........................................................................................ 1
3.字体对话框类 ......................................................................................... 2
4.记事本核心类 ............................................................................................... 6
5.在项目中创建属性文件ties ................................................................. 21
说明:本程序是学习Swing、文件流的练习程序,部分代码没有
没有实现,发现后自己补充;如果测试出bug,自行改正。
1.程序入口
package notepad;
public class NotePad {
public static void main(String[] args) {
FormMain win = new FormMain("Mini记事本");
ible(true);
}
}
2.工具栏按钮类
package notepad;
import ;
import con;
import n;
/**
* @author 孙丰伟 E‐mail: sunfengweimail@
* @version 创建时间:May 3, 2008 12:49:50 PM
* @see
*/
public class ToolBarButton extends JButton {
public ToolBarButton(Icon icon) {
super(icon);
setVerticalTextPosition(BOTTOM);
setHorizontalTextPosition(CENTER);
}
public ToolBarButton(String imageFile) {
this(new ImageIcon(imageFile)); // this
调用构造方法
}
} // end class ToolBarButton
3.字体对话框类
package notepad;
import Layout;
import ner;
import yout;
import ;
import csEnvironment;
import Event;
import Listener;
import ent;
import stener;
import .*;
/******************************************
* 类 名: JFontChooser
* 作 者:孙丰伟 E‐mail: sunfengweimail@
* 时 间:2008‐4‐24
* 描 述:实现简单的字体设置
******************************************/
public class JFontChooser extends JDialog implements ActionListener {
private Font font;
private Container contentPane;
private JButton btnOK,btnCancel;
private JComboBox comboFont,comboSize,comboStyle;
private JTextField txtFontName,txtFontSize,txtFontStyle;
private Box baseBoxV,fontBox,boxV1,boxV2,boxV3;
private JPanel btnPanel,labPanel;
private JLabel labFont,labStyle,labSize,labDemo;
public JFontChooser(JFrame frame,String title,boolean flag,Font font) {
super(frame,title,flag);
=font;
rm();
}
private void initForm() {
tPane = tentPane();
//
// 取系统中支持字体
//
GraphicsEnvironment gl = alGraphicsEnvironment();
String[] fontName = ilableFontFamilyNames();
comboFont=new JComboBox(fontName);
labFont=new JLabel("字体:");
labStyle=new JLabel("样式:");
labSize=new JLabel("大小:");
txtFontName=new JTextField(ily());
txtFontSize=new JTextField(f(e()));
txtFontStyle=new JTextField(f(le()));
labDemo=new JLabel("Hello,这是字体样例!");
t(font);
izontalAlignment();
String[] fontStyle = { "常规", "粗体", "斜体"};
comboStyle=new JComboBox(fontStyle);
//大小
comboSize=new JComboBox();
for(int size=8;size<=72;size++)
{
m(size);
}
//
// 为组合框设置事件
//
HandleItemListener itemListener=new HandleItemListener();
mListener(itemListener);
mListener(itemListener);
mListener(itemListener);
boxV1=VerticalBox();
(labFont);
(txtFontName);
(comboFont);
fontBox=HorizontalBox();
(boxV1);
boxV2=VerticalBox();
(labStyle);
(txtFontStyle);
(comboStyle);
boxV3=VerticalBox();
(labSize);
(txtFontSize);
(comboSize);
(boxV2);
(boxV3);
baseBoxV=VerticalBox();
(fontBox);
(VerticalStrut(20));
labPanel=new JPanel();
FlowLayout flow=new FlowLayout();
gnment();
out(flow);
(labDemo);
(labPanel);
(baseBoxV);
btnPanel=new JPanel();
btnOK=new JButton("确定");
btnCancel=new JButton("取消");
ionListener(this);
ionListener(this);
(btnOK);
(btnCancel);
(btnPanel, );
ationRelativeTo(null);
izable(false);
e(450, 200);
aultCloseOperation(E_ON_CLOSE);
ible(true);
}
class HandleItemListener implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto‐generated method stub
if(rce()==comboFont)
{
t(ectedItem().toString());
}
if(rce()==comboStyle)
{
t(ectedItem().toString());
}
if(rce()==comboSize)
{
t(ectedItem().toString());
}
//setTitle(f(ectedIndex()));
font=font=new
Font(t(),ectedIndex(),nt(
ext()));
t(font);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto‐generated method stub
n(ectedItem().toString());
n(nt(ectedItem().toString()));
if(rce()==btnOK)
{
//font=new
Font(ectedItem().toString(),ectedIndex(),nt(
ectedItem().toString()));
e();
}
else
{
font=null;
e();
}
}
public static void main(String[] args)
{
Font font=new Font("新宋体",,20);
JFontChooser fontChooser=new JFontChooser(null,"设置字体
",true,font);
}
public Font getNoteFont() {
return font;
}
}
4.记事本核心类
/******************************************
* 文件名:
* 作 者:孙丰伟
* 时 间:2008‐4‐24
* 描 述:保存FormMain类,是记事本程序的核心类
* 测 试:本程序使用Elipse编写,JDK1.6测试运行
******************************************/
package notepad;
import .*;
import .*;
import ;
import ntEvent;
import ntListener;
import leEditEvent;
import leEditListener;
import RedoException;
import UndoException;
import nager;
import ooser.*;
import .*;
import .*;
import ties;
/**
*
* 枚举类型: 用于标识视图菜单
*
*/
enum VIEW{ //视图菜单
STATUS, //状态栏
FOREGROUND, //前景颜色
BACKGROUND
//背景颜色
}
/**
*
* 枚举类型: 用于标识文件菜单
*
*/
enum FILE{ //用于文件菜单
NEW, //新建文件
OPEN, //打开文件
SAVE, //保存文件
SAVEAS, //另存为
EXIT //退出
}
/**
*
* 枚举类型: 用于标识编辑菜单
*
*/
enum EDIT //用于编辑菜单
{
COPY, //复制
CUT, //剪切
PASTE, //粘贴
DELETE, //删除
UNDO, //撤销
REDO //恢复
}
/******************************************
* 类 名: FormMain
* 作 者:孙丰伟
* 时 间:2008‐4‐24
* 描 述:记事本程序
* 内部类:
* 1. TxtNoteActionListener:
编辑菜单监听
* 2. FileActionListener:文件菜单监听
* 3. ViewActionListener:视图菜单监听
* 方 法:
* 1. initialize() 初始窗体
* 2. saveFile() 保存文件
* 3. openFile() 打开文件
******************************************/
class FormMain extends JFrame {
private Container contentPane; // 内容面板
private JMenuBar mnuBar; // 菜单容器
private JMenu mnuFile, mnuEdit,mnuFormat, mnuView; //
主菜单
private JMenuItem mnuFileOpen, mnuFileSaveAs, mnuFileSave, mnuFileNew,
mnuFileExit; //文件菜单项
private JMenuItem mnuEditCut, mnuEditPaste, mnuEditCopy,
mnuEditDelete,mnuEditUndo, mnuEditRedo; //编辑菜单项
private JMenuItem mnuPCut, mnuPPaste, mnuPCopy, mnuPDelete; //编辑菜单项
____右键弹出菜单
private JCheckBoxMenuItem mnuViewStatus; // 带复选的菜单,
控制状态栏是否出现
private JMenuItem mnuViewForeColor, mnuViewBackColor; //前景与背景颜
色项
private JMenuItem mnuFormatFont; // 字体菜单项
private JPopupMenu popupMenu;
// 右键菜单
private JToolBar toolBar; // 工具栏
private ToolBarButton toolNew, toolOpen, toolSave, toolExit; //工具栏按钮
private JTextArea txtNote; // 记事本文本域
private JLabel labStatus; // 状态栏
private final UndoManager undo = new UndoManager(); // 用于撤销
private JFileChooser fileChooser; // 文件对话框
private boolean isChange = false; // 文件内容是否改变
private String fileName = null; // 保存文件名
private Font font; // 文本域字体
private FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件",
"txt"); //设置文件过滤器
private Properties properties;
FormMain(String
title) {
super(title);
initialize();
}
private void initialize() {
/* 程序窗口居中 */
ationRelativeTo(this);
e(600, 500);
endedState(ZED_BOTH);
aultCloseOperation(_ON_CLOSE);
// JTextArea 事件监听器
TxtNoteActionListener txtListener = new TxtNoteActionListener();
// 文件监听器
FileActionListener fileListener = new FileActionListener();
//
// 设置工具栏
//
toolBar = new JToolBar("
记事本");
toolSave = new ToolBarButton(new ImageIcon(""));
toolOpen = new ToolBarButton(new ImageIcon(""));
t("保存文件");
t("打开文件");
ionCommand("保存");
ionCommand("打开");
ionListener(fileListener);
ionListener(fileListener);
(toolOpen);
arator();
(toolSave);
arator();
Border toolBorder = BevelBorder(1);
der(toolBorder);
// ‐‐‐‐‐设置工具栏结束‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
//
// 设置状态栏
//
labStatus = new JLabel("作者:孙丰伟");
der(BevelBorder(1));
(tus, );
//
// 设置菜单
//
JMenuBar mnuBar = new JMenuBar();
nuBar(mnuBar);
//
// 文件菜单
//
mnuFile
= new JMenu("文件(F)");
Font mnuFont = new Font("新宋体", 0, 12);
t(mnuFont);
monic('F');
mnuFileNew = new JMenuItem("新建(N)");
mnuFileOpen = new JMenuItem("打开(O)");
mnuFileSave = new JMenuItem("保存(S)");
mnuFileSaveAs = new JMenuItem("另存为(A)");
mnuFileExit = new JMenuItem("退出(E)");
monic('N');
monic('O');
monic('S');
monic('E');
monic('A');
elerator(Stroke(_N,
_MASK));
elerator(Stroke(_O,
_MASK));
elerator(Stroke(_S,
_MASK));
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionListener(fileListener);
ionListener(fileListener);
ionListener(fileListener);
ionListener(fileListener);
ionListener(fileListener);
(mnuFileNew);
(mnuFileOpen);
(mnuFileSave);
arator();
(mnuFileExit);
(mnuFile);
// 文件菜单设置结束‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
//
// 编辑菜单
//
mnuEdit = new JMenu("编辑(E)");
t(mnuFont);
monic('E');
mnuEditCopy = new
JMenuItem("复制(C)");
mnuEditCut = new JMenuItem("剪切(X)");
mnuEditPaste = new JMenuItem("粘贴(V)");
mnuEditDelete = new JMenuItem("删除(L)");
mnuEditUndo = new JMenuItem("撤销(U)");
mnuEditRedo = new JMenuItem("恢复(R)");
monic('C');
monic('X');
monic('V');
monic('L');
monic('U');
monic('R');
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionListener(txtListener);
ionListener(txtListener);
ionListener(txtListener);
ionListener(txtListener);
ionListener(txtListener);
ionListener(txtListener);
elerator(Stroke(_C,
_MASK));
elerator(Stroke(_X,
_MASK));
elerator(Stroke(_V,
_MASK));
elerator(Stroke(_L,
_MASK));
elerator(Stroke(_Z,
_MASK));
elerator(Stroke(_Y,
_MASK));
(mnuEditUndo);
(mnuEditRedo);
arator();
(mnuEditCopy);
(mnuEditCut);
(mnuEditPaste);
arator();
(mnuEditDelete);
(mnuEdit);
// 编辑菜单设置结束‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
//
// 弹出式菜单
//
popupMenu = new JPopupMenu("我的记事本");
mnuPCopy = new JMenuItem("复制(C)");
mnuPCut = new JMenuItem("剪切(X)");
mnuPPaste = new JMenuItem("粘贴(V)");
mnuPDelete = new JMenuItem("删除(L)");
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
elerator(Stroke(_C,
_MASK));
ionListener(txtListener);
ionListener(txtListener);
ionListener(txtListener);
ionListener(txtListener);
monic('C');
monic('X');
monic('V');
monic('L');
(mnuPCopy);
(mnuPCut);
(mnuPPaste);
arator();
(mnuPDelete);
// 弹出式菜单设置结束‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
//
// 格式菜单
//
mnuFormat=new JMenu("格式(O)");
monic('O');
mnuFormatFont=new JMenuItem("字体(F)");
monic('F');
//
// 字体菜单事件,调用字体窗口JFontChooser
//
ionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
Font tempFont=new Font(ily(),le(),e());
JFontChooser fontChooser=new JFontChooser(,"设置字
体",true,tempFont);
if(eFont()!=null)
{
font=eFont();
t(font);
//
// 将字体写回属性文件
//
properties=new Properties();
perty("fontName", ily());
perty("fontStyle",
f(le()));
perty("fontSize", f(e()));
try {
(new FileWriter("ties"), null);
} catch (IOException e1) {
// TODO Auto
‐generated catch block
tackTrace();
}
}
}
});
(mnuFormatFont);
(mnuFormat);
//
// 视图菜单
//
ViewActionListener viewAction = new ViewActionListener();
mnuView = new JMenu("视图(V)");
monic('V');
(mnuView);
mnuViewStatus = new JCheckBoxMenuItem("状态栏", true);
mnuViewForeColor = new JMenuItem("前景颜色"); // 设置文本颜色
mnuViewBackColor = new
JMenuItem("背景颜色"); // 设置背景颜色
ionCommand(ng());
ionCommand(ng());
ionCommand(ng());
ionListener(viewAction);
ionListener(viewAction);
ionListener(viewAction);
(mnuViewStatus);
arator();
(mnuViewForeColor);
(mnuViewBackColor);
txtNote = new JTextArea();
//
// 设置字体
//
properties=new Properties();
try {
//(new
FileInputStream(tThread().getContextClassLoader().getResource("/
operties").getFile()));
(new FileReader("ties"));
String fontName=perty("fontName");
n(fontName);
String fontStyle=perty("fontStyle");
String fontSize=perty("fontSize");
font=new
Font(fontName,nt(fontStyle),nt(fontSize));
} catch (FileNotFoundException e) {
// TODO Auto
‐generated catch block
tackTrace();
} catch (IOException e) {
// TODO Auto‐generated catch block
tackTrace();
}
t(font);
//
// 为文本域设置鼠标事件,实现右键菜单
//
seListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (ifiers() == 3_MASK) {
(txtNote, (), ());
}
}
});
}
//
// 为文本域设置文档撤销事件
//
ument().addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
// TODO
t(t());
}
});
//
// 为文本域设置文档操作事件
//
ument().addDocumentListener(new DocumentListener() {
});
JScrollPane scoll = new JScrollPane(txtNote);
contentPane = tentPane();
(scoll);
te();
(toolBar, );
//文件对话框默认为当前项目路径
fileChooser = new JFileChooser(".");
public void insertUpdate(DocumentEvent e) {
// TODO 自动生成方法存根
isChange = true;
}
public void removeUpdate(DocumentEvent e) {
// TODO 自动生成方法存根
isChange = true;
}
public void changedUpdate(DocumentEvent e) {
// TODO 自动生成方法存根
isChange = true;
}
class ViewActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
switch(f(ionCommand()))
{
case STATUS:
ible(cted());
break;
case FOREGROUND:
eground(alog(null, "前景颜色",
));
break;
case BACKGROUND:
kground(alog(null, "背景颜色",
));
break;
}
} // end of method
} // end of class ViewActionListener
class TxtNoteActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
switch(f(ionCommand()))
{
case COPY:
();
break;
case CUT:
();
break;
case PASTE:
();
break;
case DELETE:
eRange("", ectionStart(),
ectionEnd());
break;
case UNDO:
try {
if (o()) ();
} catch (CannotUndoException ex) {
ssageDialog(contentPane, "无法执行撤销操
作", "撤销异常", G_MESSAGE);
} // end catch
break;
case REDO:
try {
if (o()) ();
} catch (CannotRedoException ex) {
ssageDialog(contentPane, "无法执行恢复操
作", "恢复异常", G_MESSAGE);
} // end catch
break;
} //end of switch
} //end method
} //
end of class TxtNoteAcitonListener
/**
* 说明: 实现File相关菜单的事件类
*/
class FileActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
switch(f(ionCommand().toString()))
{
case NEW:
t("");
break;
case OPEN:
openFile();
break;
case SAVE:
eFilter(filter);
veDialog(contentPane);
fileName = ectedFile().getAbsolutePath();
//
// 如果没有写扩展名,则自动加扩展名.txt
//
if (f('.') == ‐1)
fileName += ".txt";
saveFile();
break;
case SAVEAS:
//veDialog(contentPane);
break;
case EXIT:
(0);
break;
}
}
} // end class FileActionListener
/********************************************************
* 函数名: saveFile
* 功能描述: 使用写入文件字符流FileWriter实现记事本内容保存
* 输入参数: 无
* 返 回 值: 无
* 作 者: 孙丰伟
* 日 期: 2008‐4‐8
* 修 改 人
:
* 日 期:
********************************************************/
private void saveFile() {
try {
// ssageDialog(null, fileName);
FileWriter writer = new FileWriter(fileName);
(t());
();
} catch (IOException e) {
// TODO Auto‐generated catch block
tackTrace();
}
}
/********************************************************
* 函数名: openFile
* 功能描述:
使用读取文件字符流FileReasder实现打开文件
* 输入参数: 无
* 返 回 值: 无
* 作 者: 孙丰伟
* 日 期: 2008‐4‐8
* 修 改 人:
* 日 期:
********************************************************/
private void openFile() {
//
// 在打开新文件时,先判断当前编辑文档是否保存过,如果没有保存则先对当
前文档执行保存操作,再打开新文件
//
if (isChange) {
int n = nfirmDialog(contentPane, "当前文件内容已经改
变,你是否保存文件?", "系统提示", _NO_CANCEL_OPTION);
if (n == _OPTION) // 保存
{
saveFile();
} else if (n == _OPTION) // 取消
{
return;
}
}
eFilter(filter);
if (enDialog(contentPane) ==
E_OPTION)
{
File file = ectedFile();
try {
FileReader reader = new FileReader(file);
BufferedReader breader = new BufferedReader(reader);
String s = null;
t("");
while ((s = ne()) != null) {
(s + "n");
}
fileName = olutePath();
setTitle(fileName + " ‐‐‐ 记事本");
();
();
isChange = false;
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
tackTrace();
} catch (IOException es) {
// TODO 自动生成 catch 块
tackTrace();
}
}// end of if
} // end of openFile
}
5.
在项目中创建属性文件ties
内容:
#Mon May 05 17:57:49 CST 2008
fontName=隶书
fontSize=14
fontStyle=0
发布者:admin,转转请注明出处:http://www.yc00.com/news/1719939144a2759329.html
评论列表(0条)