1. 首页
  2. 文档大全

Eclipse开发记事本程序.doc

上传者:蓝天 2022-06-19 21:43:28上传 DOC文件 84 KB
Eclipse开发记事本程序
1、新建项目,新建 Visual Class,名称输入 TextEditorApp, Style 选 Swing 下的 Frame,选中 public static void main(String[] args),完成。
2、 进入JFrame中心面板,右击鼠标,设置中心面板的Layout为null布局。
3、 在 palette 的 Swing Containers 下选择 JScrollPane,建立滚动面板, 选择JtextArea建立多行文本,重命名为textArea。
4、 建立菜单栏jJMenuBar,插入四个菜单JMenu,分别改名为文件、编辑、 工具、帮助;在“文件”下插入菜单项新建、打开、保存、退出;在“编辑”下 插入菜单项复制、剪切、粘贴、全选、颜色;在“工具”下插入菜单项计算器、 记事本;在“帮助”插入菜单项关于。
5、 给“新建”菜单添加actionPerformed事件,在对应的代码处写入:
textArea. setText(null);
TextEditorApp. this. setTitle (z/无标题-记事本〃);
6、 给"打开”菜单添加actionPerformed事件,在对应的代码处写入:
File fl;
JFileChooser jfcl = new JFileChooser();
int numl 二 jfcl. showOpenDialog(TextEditorApp. this);
if (numl==JFileChooser. APPROVE_OPTIO^ {
try(
fl = jfcl. getSelectedFile();
TextEditorApp. this. setTitle (fl. getName ());
FileReader fr 二 new FileReader(fl);
BufferedReader br = new BufferedReader(fr);
String str;
while ((str = br. readLineO) !=null) { textArea. setText(str);
}
fr. close ();
br. close ();
}catch(FileNotFoundException el)(
el. printStackTrace ();
}catch(lOException e2){
e2. printStackTrace ();
}
}
7、 给"保存”菜单添加actionPerformed事件,在对应的代码处写入:
File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 二 jfc2. showSaveDialog(TextEditorApp. this);
if (num2==JFileChooser. APPROVE_OPTIO^) (
f2=jfc2. getSelectedFile();
TextEditorApp. this. setTitle(f2. getName());
try{
FileWriter fw = new FileWr

Eclipse开发记事本程序


文档来源:https://www.taodocs.com/p-690299610.html

文档标签:

下载地址