本文目录一览:
求一个用JAVA写的简单的记事本源代码程序 要有运行结果的截图和源代码,在线等
这个是我以前写着玩的一个例子,可以运行起来看看,有保存,撤销,复制,剪切功能,自己研究研究
package test;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MyNote {
private JFrame frame = new JFrame("记事本");
private JTextArea text = new JTextArea();
private static boolean flag = false; // 判断是否保存
public MyNote() {
JMenuBar bar = new JMenuBar();
JMenu edit = new JMenu("check");
JMenu check = new JMenu("edit");
JMenu help = new JMenu("help");
JMenuItem term = new JMenuItem("copy");
JMenuItem term1 = new JMenuItem("paste");
JMenuItem term2 = new JMenuItem("cut");
JMenuItem term3 = new JMenuItem("backout");
JMenuItem term4 = new JMenuItem("import");
JMenuItem term5 = new JMenuItem("open");
JMenuItem term6 = new JMenuItem("exit");
JMenuItem term7 = new JMenuItem("save to");
JMenuItem term8 = new JMenuItem("about");
JMenuItem term9 = new JMenuItem("save");
JMenuItem term10 = new JMenuItem("new");
Font font = new Font(null, JFrame.ABORT, 24);
text.setFont(font);
JScrollPane scroll = new JScrollPane(text);
frame.setJMenuBar(bar);
bar.add(edit);
bar.add(check);
bar.add(help);
edit.add(term7);
edit.add(term4);
edit.add(term5);
edit.add(term6);
check.add(term);
check.add(term1);
check.add(term2);
check.add(term3);
help.add(term8);
check.add(term9);
edit.add(term10);
frame.add(scroll);
text.setVisible(false);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLocation(500, 500);
// 事件注册
MenuListener m = new MenuListener();
term5.addActionListener(m);
NewListener n = new NewListener();
term4.addActionListener(n);
SaveListener s = new SaveListener();
term7.addActionListener(s);
CopyListener c = new CopyListener();
term.addActionListener(c);
PasteListener p = new PasteListener();
term1.addActionListener(p);
CutListener c1 = new CutListener();
term2.addActionListener(c1);
HelpListener h = new HelpListener();
term8.addActionListener(h);
ExitListener e = new ExitListener();
term6.addActionListener(e);
CloseListener c2 = new CloseListener();
frame.addWindowListener(c2);
BackOutListener b = new BackOutListener();
term3.addActionListener(b);
SaveActionListener s1 = new SaveActionListener();
term9.addActionListener(s1);
NewFileListener n1 = new NewFileListener();
term10.addActionListener(n1);
}
private class MenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.setVisible(true);
}
}
// 打开新文件
private class NewListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.setText("");
JFileChooser fileChooser = new JFileChooser();
int r = fileChooser.showOpenDialog(frame);
if (r == JFileChooser.APPROVE_OPTION) {
try {
File file = fileChooser.getSelectedFile();
FileReader fr = new FileReader(file);
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
text.append(new String(buf, 0, len));
}
fr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 另存为
private class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser filechoose = new JFileChooser();
int r = filechoose.showSaveDialog(frame);
if (r == JFileChooser.APPROVE_OPTION) {
File file = filechoose.getSelectedFile();
try {
FileWriter writer = new FileWriter(file);
writer.write((String) text.getText());
writer.close();
// 下面方法也可以
/*
* PrintWriter print=new PrintWriter(file);
* print.write(text.getText()); print.close();
*/
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 复制
private class CopyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.copy();
}
}
// 粘贴
private class PasteListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.paste();
}
}
// 剪切
private class CutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.cut();
}
}
// 关于主题
private class HelpListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "汪雄辉的无敌记事本");
}
}
private class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "谢谢!");
try {
Thread.sleep(2000);
System.exit(0);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
private class CloseListener implements WindowListener {
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
// 关闭窗口
public void windowClosing(WindowEvent e) {
StringBuffer sb = new StringBuffer();
try {
FileReader fr = new FileReader("未命名文件.txt");
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
sb.append(new String(buf, 0, len));
}
fr.close();
} catch (Exception e1) {
e1.getStackTrace();
}
String s = sb.toString();
if (flag == false || !(text.getText().equals(s))) {
int r = JOptionPane.showConfirmDialog(frame, "你还没有保存,要保存吗?");
if (r == JOptionPane.OK_OPTION) {
JFileChooser filechoose = new JFileChooser();
int r1 = filechoose.showSaveDialog(frame);
if (r1 == JFileChooser.APPROVE_OPTION) {
File file = filechoose.getSelectedFile();
try {
FileWriter writer = new FileWriter(file);
writer.write((String) text.getText());
writer.close();
System.exit(0);
// 下面方法也可以
/*
* PrintWriter print=new PrintWriter(file);
* print.write(text.getText()); print.close();
*/
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
}
} else if (r == JOptionPane.NO_OPTION) {
System.exit(0);
} else {
}
} else
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}
// 撤销
private class BackOutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (flag == false)
text.setText("");
else {
try {
FileReader fr = new FileReader("未命名文件.txt");
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
text.setText(new String(buf, 0, len));
}
fr.close();
} catch (IOException e1) {
e1.getStackTrace();
}
}
}
}
// 保存文件
private class SaveActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = true;
FileWriter writer;
try {
writer = new FileWriter("未命名文件.txt");
writer.write((String) text.getText());
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class NewFileListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
new MyNote();
}
}
public static void main(String[] args) {
new MyNote();
}
}
请问这个漂亮的网络记事本的源代码哪有下载?
html
head
meta http-equiv="Content-Language" content="zh-cn"
meta http-equiv="Content-Type" content="text/html; charset=gb2312"
title欢迎使用中华信鸽网-网络记事本-网络记事/title
meta name="keywords" content="记事,记事,记事,记事,记事,免费,免费,免费,免费,记事,心情记事,网络记事,免费记事,隐私记事,交换记事,粉可爱留言本、记事本申请"
link href="css.css" rel="stylesheet" type="text/css"
script src="img/alt.js"/script
script language="JavaScript"
function CheckForm()
{
if (document.regform.username.value.length == 0) {
alert("请填写用户名.");
document.regform.username.focus();
return false;
}
if (document.regform.password.value.length == 0) {
alert("请输入你的密码.");
document.regform.password.focus();
return false;
}
}
/script
/head
body background="diary/index_bg.jpg" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0"
table border="0" width="985" id="table1" cellspacing="0" cellpadding="0"
tr
td width="359"
img border="0" src="diary/diary_1_1.jpg" width="359" height="79"/td
td width="305"
img border="0" src="diary/diary_1_2.jpg" width="305" height="79"/td
td width="321"
img border="0" src="diary/diary_1_3.jpg" width="321" height="79"/td
/tr
/table
table border="0" width="985" id="table2" cellspacing="0" cellpadding="0"
tr
td valign="top" width="71" height="0" rowspan="2"
img border="0" src="diary/diary_1.jpg" width="71" height="370"/td
td width="288" background="diary/diary_2.jpg" rowspan="2"
img border="0" src="diary/loing_img.jpg" width="275" height="355"/td
td width="21" rowspan="2"
img border="0" src="diary/diary_3.jpg" width="21" height="370"/td
td width="284" background="diary/diary_4.jpg" rowspan="2"
table border="0" width="100%" id="table6" cellspacing="0" cellpadding="0" height="100%"
tr
td height="40" /td
/tr
tr
td
div align="center"
table border="0" width="245" id="table7" cellspacing="0" cellpadding="0" background="diary/loing.gif" height="274"
tr
td height="84" width="245" colspan="2" /td
/tr
tr
td height="19" width="245" colspan="2"
p align="center" img border="0" src="diary/denlu%20.gif" width="103" height="16"/td
/tr
tr
td height="19" width="245" colspan="2" /td
/tr
form language="javascript" name="regform" method="post" action="login.asp?ation=log" onSubmit="return CheckForm()"
tr
td width="71" height="19"
p align="right"font color="#FF0000"账号:/font/td
td width="174" height="19" input name="username" type="text" id="username3" size="18" maxlength="12"/td
/tr
tr
td width="71" height="42"
p align="right"font color="#FF0000"密码:/font/td
td width="174" height="42"input name="password" type="password" id="password" size="18" maxlength="12"/td
/tr
tr
td width="245" colspan="2" height="39"
p align="center" input type="submit" name="Submit" value=" 提 交 "
font color="#00ACF0"ua href="reg1.asp"
font color="#00ACF0"a href="reg1.asp"
font color="#00ACF0"注册/font/a/font/a/ua href="reg1.asp"font color="#00ACF0"申请/font/a/font/td
/tr
tr
td width="245" colspan="2" /td
/tr
/table
/div
/td
/tr
tr
td /td
/tr
/table
/td
td width="77" rowspan="2"
img border="0" src="diary/diary_5_1.jpg" width="77" height="370"/td
td width="194" height="87" background="diary/diary_5_2.jpg"
font color="#660033"对不起,你还没有登陆,请登陆!/fontpfont color="#660033"
忘记密码请联系管理员br
/font/td
td width="50" rowspan="2"
img border="0" src="diary/diary_5_4.jpg" width="50" height="370"/td
/tr
tr
td width="194" height="283"
img border="0" src="diary/diary_5_3.jpg" width="194" height="283"/td
/tr
/table
table border="0" width="100%" id="table3" cellspacing="0" cellpadding="0"
tr
tdimg border="0" src="diary/diary_dow.jpg" width="985" height="71"/td
/tr
/table
table border="0" width="985" id="table4" cellspacing="0" cellpadding="0"
tr
td width="271"
img border="0" src="diary/diary_end_1.jpg" width="271" height="80"/td
td width="518" background="diary/diary_end_2.jpg"
table border="0" width="100%" id="table5" cellspacing="0" cellpadding="0" height="49"
tr
td
span class="navi"font color="#703F15"a href="xgsb.asp"font color="#703F15"strong首页/strong/font/a/font/spanfont color="#703F15"b a href="NewList.asp"font color="#703F15"最新记事/font/a /bspan style="font-weight: 700"a href="goodList.asp"font color="#703F15"佳作推荐/font/a/spanb a href="Newbook.asp"font color="#703F15"最新记事本/font/a a href="BookOrder.asp"font color="#703F15"记事本排行/font/a a href="search.asp"font color="#703F15"记事搜索/font/a a href="mybook.asp"font color="#703F15"我的记事/font/a
a href="info.asp?userid=" font color="#703F15"记事本管理/font/a/b/font
/td
/tr
tr
td height="19" /td
/tr
/table
/td
td width="197"
img border="0" src="diary/diary_end_3.jpg" width="197" height="80"/td
/tr
/table
/body
/html
谁有记事本的源码
用VB做一个记事本实在不很复杂,我们完全可以通过向导来很方便地做出来。但本文只打算讨论用手动方法制作记事本,旨在向VB初学者展示:学VB原来是如此容易! 通过阅读、研究本文并按本文所述进行尝试,初学者将学到很多东西,如怎样使用RichText控件来打开和保存文件,怎样制作菜单、工具栏和状态栏以及如何对其编写代码等。 第一章 让我们的记事本马上运行 急于求成是初学者共有的心愿。那好,请按如下三个步骤做,我们的愿望立即就可以实现! 步骤一:绘制界面。 新建一个标准EXE工程,将其Caption属性改为“超级记事本”,点击Icon属性给它找个合适的Icon图标。单击菜单“工程”-“部件”,在弹出的“部件”对话框里找到Microsoft RichText Box 6.0和公共对话框Microsoft Common Dialog 6.0并选中它们,单击“确定”按钮。这时左边的工具栏上出现了我们刚才新添的两个控件了。在窗体上绘制RichText Box和Commn Dialog,其中RichText Box的大小和位置可不用理睬,我们将在代码中处理它,当然,有必要把它的ScrollBar属性设为2-rtfVertical,这样在打开和编辑文件时垂直滚动条才可用。 步骤二:编辑菜单。 按Ctrl+E调出菜单编辑器,我们来做如下几个菜单: 一.文件菜单: 文件(第一层) mnuFile 新建(第二层) mnuNew 打开(第二层) mnuOpen 保存(第二层) mnuSave - (第二层) mnuFileSep (分隔线) 退出(第二层) mnuExit 二.编辑菜单: 编辑(第一层) mnuEdit 复制(第二层) mnuCopy 剪切(第二层) mnuCut 粘贴(第二层) mnuPaste - (第二层) mnuEditSep (分隔线) 全选(第二层) mnuSelecAll 三.搜索菜单: 搜索(第一层) mnuSearch 查找(第二层) mnuFind 查找下一个(第二层) mnuFindOn 四.帮助菜单: 帮助(第一层) mnuHelp 使用说明(第二层) mnuUsage 关于(第二层) mnuAbout (注:各菜单项的快捷键请自行设置) 好了,其它的菜单项以后再根据需要添加。现在进入: 步骤三:编写代码。 '声明查找变量 Dim sFind As String '声明文件类型 Dim FileType, FiType As String '初始化程序 Private Sub Form_Load() '设置程序启动时的大小 Me.Height = 6000 Me.Width = 9000 End Sub '设置编辑框的位置和大小 Private Sub Form_Resize() On Error Resume Next '出错处理 RichTextBox1.Top=20 RichTextBox1.Left=20 RichTextBox1.Height = ScaleHeight-40 RichTextBox1.Width = ScaleWidth-40 End Sub '新建文件 Private Sub mnuNew_Click() RichTextBox1.Text = "" '清空文本框 FileName = "未命名" Me.Caption = FileName End Sub '打开文件 Private Sub mnuOpen_Click() CommonDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*" CommonDialog1.ShowOpen RichTextBox1.Text = "" '清空文本框 FileName = CommonDialog1.FileName RichTextBox1.LoadFile FileName Me.Caption = "超级记事本:" FileName End Sub '保存文件 Private Sub mnuSave_Click() CommonDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*" CommonDialog1.ShowSave FileType = CommonDialog1.FileTitle FiType = LCase(Right(FileType, 3)) FileName = CommonDialog1.FileName Select Case FiType Case "txt" RichTextBox1.SaveFile FileName, rtfText Case "rtf" RichTextBox1.SaveFile FileName, rtfRTF Case "*.*" RichTextBox1.SaveFile FileName End Select Me.Caption = "超级记事本:" FileName End Sub '退出 Private Sub mnuExit_Click() End End Sub '复制 Private Sub mnuCopy_Click() Clipboard.Clear Clipboard.SetText RichTextBox1.SelText End Sub '剪切 Private Sub mnuCut_Click() Clipboard.Clear Clipboard.SetText RichTextBox1.SelText RichTextBox1.SelText = "" End Sub '全选 Private Sub mnuSelectAll_Click() RichTextBox1.SelStart = 0 RichTextBox1.SelLength = Len(RichTextBox1.Text) End Sub '粘贴 Private Sub mnuPaste_Click() RichTextBox1.SelText = Clipboard.GetText End Sub '查找 Private Sub mnuFind_Click() sFind = InputBox("请输入要查找的字、词:", "查找内容", sFind) RichTextBox1.Find sFind End Sub '继续查找 Private Sub mnuFindOn_Click() RichTextBox1.SelStart = RichTextBox1.SelStart + RichTextBox1.SelLength + 1 RichTextBox1.Find sFind, , Len(RichTextBox1) End Sub '使用说明 Private Sub mnuReadme_Click() On Error GoTo handler RichTextBox1.LoadFile "Readme.txt", rtfText '请写好Readme.txt文件并存入程序所在文件夹中 Me.Caption = "超级记事本:" "使用说明" Exit Sub handler: MsgBox "使用说明文档可能已经被移除,请与作者联系。", vbOKOnly, " 错误信息" End Sub '关于 Private Sub mnuAbout_Click() MsgBox "超级记事本 Ver1.0 版权所有(C) 2001 土人",vbOKOnly,"关于" End Sub '设置弹出式菜单(即在编辑框中单击鼠标右键时弹出的动态菜单) Private Sub RichTextBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu mnuEdit, vbPopupMenuLeftAlign Else Exit Sub End If End Sub '防止在切换输入法时字体自变(感谢王必成先生提供此方案) Private Sub RichTextBox1_KeyUp(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeySpace Then RichTextBox1.SelFontName = CommonDialog1.FontName End If End Sub 至此,我们的记事本可以编译使用了。点击菜单“文件”-“生成XXX.EXE”,回到桌面运行我们的记事本看看,是不是颇有成就感? 当然,这样的记事本还比较粗糙,我们还需要做些工作,请看下一章。 第二章 美化程序界面 多数字处理软件都有工具栏和状态栏。工具栏和状态栏除了能美化我们的程序使其更具有专业性质外,还给用户带来操作上的便利。现在我们就来做一做这两样东西。 一.工具栏 (一)制作工具栏 单击“工程”-“部件”,选中Microsoft Windows Common Control 6.0并确定。这时,我们要用到的控件就出现在左边的工具栏上了。 要做工具栏,首先需要一个叫ImageList的控件来装载图像。在程序界面上添加它,然后右键单击此控件,左键单击“属性”,弹出“属性页”对话框的“图像”,再单击“插入图片”就可以一次性装载图片了(如不满意,以后还可以添加)。图片可在C:\Microsoft Visual Studio\Common\Graphics\Bitmaps\TlBr_W95下选择(这里假设你的VB安装在C盘下)。注意了:在插入图片时给每一张图片注明关键字,以便在引用图片时不至于混乱。如插入“新建”的图片,我们在“关键字”栏注明“New”。 图片有了,接下来在程序界面添加工具栏(ToolBar)。添加后工具栏就出现在菜单下面,右键单击它,选择“属性”,在弹出的“属性页”对话框中的“通用”项作些设置,主要如下两项: 1.“图像列表”:选择ImageList1 2.“样式”:根据喜爱选择1-trbStandard或者2-trbFlat 继续点击“属性页”的“按钮”选项,插入若干按钮。按钮有多种样式,请根据需要设置。这里请一定注意:每一个与用户操作有关的按钮都必须注明关键字、装载图片,如“新建”按钮,在“关键字”项注明“新建”,在“图像”项键入“New”(即ImageList1中的图片关键字),需要的话还可以在“工具提示文本”项填入适当的提示语。 (二)编写工具栏的按钮代码 工具栏按钮的代码编辑很简单,可以按照下面的格式去编写: Private Sub ToolBar1_ButtonClick(ByVal Button As MSComctlLib.Button) On Error Resume Next '出错处理 Select Case Button.Key '按关键字选择 Case "新建" mnuNew_Click '等于菜单项“新建”被单击 Case "打开" '等于菜单项“打开”被单击 mnuOpen_Click '......(继续编写其它按钮的代码) End Select End Sub 完成后试运行一下我们的程序,我们发现,有了工具栏之后,程序变得漂亮多了,只是有一个问题:打开一个较长的文档后,编辑框的下拉滚动条向上的箭头不见了。原因是:工具条占用一定的空间。解决方法:将“设置编辑框的位置和大小”中的RichTextBox1.Top = 20 和 RichTextBox1.Height = Me.ScaleHeight - 40分别改为RichTextBox1.Top = 380,RichTextBox1.Height = Me.ScaleHeight - 400即可。 二.状态栏 (一)制作状态栏 状态栏的英文名字叫StatusBar,在窗体上添加它后会默认出现在窗体的最下方。用鼠标右键点击它,调出“属性页”对话框,单击“窗格”项,插入一些窗格,可以将各个窗格的“样式”设置为: 0-sbrText 显示文本,需编写代码 1-sbrCaps 显示大小写状态,无需编程 2-sbrNum 显示NumLock键开关状态,无需编程 3-sbrIns 显示Insert键状态,无需编程 4-sbrScr1 5-sbrTime 显示时间,不编程时时间不会随系统时钟变化 6-sbrDate 显示日期,无需编程 注意:加进状态栏后需将Form_Resze中的RichTextBox1.Height = Me.ScaleHeight - 400改为RichTextBox1.Height = Me.ScaleHeight - 600。 (二)状态栏根据其“样式”属性决定用不用编写代码(如上文所述)。下面举些例子,读者可以认真揣摩,从而达到举一反三的效果。 例一:用户选取了“新建”后,让第一个窗格显示:“目前状态:正在打开文件《……》”。请将下面代码写进“打开”菜单里面: StatusBar1.Panels(1).Text = "目前状态:正在打开文件" "《" CommonDialog1.FileTitle "》" 例二:让第三个窗格显示时间并让时间跟随系统时钟变化。 首先,给程序加一个Timer控件,将其Interval属性设为1000。然后: 在Form_Load过程加入:StatusBar1.Panels(1).Text = Time;然后给Timer控件编写代码: Private Sub Timer1_Timer() If StatusBar1.Panels(3).Text CStr(Time) Then StatusBar1.Panels(3).Text = Time End If End Sub 例三:当编辑框的文本发生变化时让第一个窗格显示:“正在编辑文档:文件名”。 Private Sub RichTextBox1_Chang() StatusBar1.Panels(1).Text = "正在编辑文档:" CommonDialog1.FileName End Sub 辛苦了那么久,我们现在已经拥有一个象模象样的记事本了。这个记事本由于用了RichText控件,理论上它能打开和编辑任意大的文档,使用起来的确比Windows自带的记事本方便得多。当然,还有一些其它的功能需要添加和完善,这就靠你慢慢去完成了。 回答完毕! 转来的,不过很详细....