记事本代码大全
<html>
<head>
<styletype="text/css">
h2{background:orange}
</style>
</head>
<body>
<h2>个人基金投资者投资情况调查问卷</h2>
<p>(1)您选择投资公募基金的主要原因是:()</p>
<p><inputtype="radio"name="one"value="1"/>公募运作管理规范、信息披露全面,可以方便地查找相关信息,申购、赎回等均很便利</p>
<p><inputtype="radio"name="one"value="2"/>公募基金有比较强大的投研团队</p>
<p><inputtype="radio"name="one"value="3"/>公募基金投资门槛极低,对投资者没有太多的资质及投资金额要求</p>
<p>(2)您选择投资专户一对多基金的主要原因是:()</p>
<p><inputtype="radio"name="two"value="1"/>专户一对多基金管理规范,同公募基金一样有比较强大的投研团队,且投资范围、投资比例较自由</p>
<p><inputtype="radio"name="two"value="2"/>专户一对多产品相对于公募基金有更强的针对性,可以更好地满足投资者要求</p>
<p><inputtype="radio"name="two"value="3"/>其他</p>
<p>(3)您希望获得哪方面的讯息?(多选题)</p>
<p><inputtype="checkbox"name="three"value="1"/>产品设计理念</p>
<p><inputtype="checkbox"name="three"value="2"/>投资思路</p>
<p><inputtype="checkbox"name="three"value="3"/>基金经理介绍</p>
<p><inputtype="checkbox"name="three"value="4"/>购买渠道</p>
<p><inputtype="checkbox"name="three"value="5"/>购买费率</p>
<h2>如您希望获得抽奖机会,请留下如下信息</h2>
<p>姓名:<inputtype="text"name="name"value=""/></p>
<p>手机:<inputtype="text"name="phone"value=""/></p>
<p>住址:<inputtype="text"name="address"value=""/></p>
<p>建议:<textareaname="jy"rows="3"cols="8"></textarea></p>
<p><inputtype="submit"name="submit"value="提交"/><inputtype="reset"name="reset"value="重置"/></p>
</body>
</html>
2. java记事本代码
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.awt.datatransfer.*;
import javax.swing.*;
public class MiniEdit extends JFrame implements ActionListener {
/**
* Method main
*
*
* @param args
*
*/
MenuBar menuBar = new MenuBar();
Menu file = new Menu("File"),
edit = new Menu("Edit"),
help = new Menu("Help");
MenuItem[] menuItem ={
new MenuItem("New"),
new MenuItem("Open"),
new MenuItem("Save"),
new MenuItem("Exit"),
new MenuItem("Select All"),
new MenuItem("Copy"),
new MenuItem("Cut"),
new MenuItem("Paste"),
new MenuItem("Help")
};
TextArea textArea = new TextArea();
String fileName = "NoName";
Toolkit toolKit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolKit.getSystemClipboard();
//opne and close message dialogs
private FileDialog openFileDialog =
new FileDialog(this,"Open File",FileDialog.LOAD);
private FileDialog saveFileDialog =
new FileDialog(this,"Save File",FileDialog.SAVE);
public static void main(String[] args) {
// TODO: Add your code here
MiniEdit MyEdit = new MiniEdit();
MyEdit.show();
}
/**
* Method MiniEdit
*
*
*/
public MiniEdit() {
// TODO: Add your code here
setTitle("MiniEdit");
setFont(new Font("Times New Roman",Font.PLAIN,15));
setBackground(Color.white);
setSize(500,500);
setMenuBar(menuBar);
menuBar.add(file);
menuBar.add(edit);
menuBar.add(help);
for(int i=0;i<4;i++)
{
file.add(menuItem[i]);
edit.add(menuItem[i+4]);
}
help.add(menuItem[8]);
add(textArea);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
e.getWindow().dispose();
System.exit(0);
}
});
//add actionListener
for(int i=0;i<menuItem.length;i++)
{
menuItem[i].addActionListener(this);
}
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
Object eventSource = e.getSource();
if(eventSource == menuItem[0])//newItem
{
textArea.setText("");
}
else if(eventSource == menuItem[1])//OpenItem
{
openFileDialog.show();
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
{
openFile(fileName);
}
}
else if(eventSource ==menuItem[2])//SaveItem
{
saveFileDialog.show();
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
if(fileName !=null)
{
writeFile(fileName);
}
}
else if(eventSource==menuItem[3])//exitItem
{
System.exit(0);
}
else if(eventSource == menuItem[4])//Select All
{
textArea.selectAll();
}
else if(eventSource == menuItem[5])//
{
String text = textArea.getSelectedText();
StringSelection selection= new StringSelection(text);
clipboard.setContents(selection,null);
}
else if(eventSource == menuItem[6])//cut
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
textArea.replaceText("",textArea.getSelectionStart(),
textArea.getSelectionEnd());
}
else if(eventSource == menuItem[7])//Paste
{
Transferable contents = clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception ex){}
textArea.replaceText(text,
textArea.getSelectionStart(),textArea.getSelectionEnd());
}
else if(eventSource == menuItem[8])
{
// JOptionPane.showMessageDialog(null,"This is a MiniEdit.");
}
}
//Read file
public void openFile(String fileName){
try{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content,charsRead,size-charsRead);
readIn.close();
textArea.setText(new String(content,0,charsRead));
}catch(Exception e)
{
System.out.println("Error opening file!");
}
}
//write file
public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter write = new FileWriter(file);
write.write(textArea.getText());
write.close();
}catch(Exception e){
System.out.println("Error closing file!");
就这样交,不会错的
3. 记事本编程用的代码有哪些啊怎么用啊
记事本可以用来编写所有的程序代码,任何语言,但是编写好后编译必须使用与其语言相对应的编译器进行编译,这个记事本没那功能.
还有就是记事本没有那专业软件的纠错/语法检测等功能,用于编写程序比较容易出错,所以一般不用记事本来编写程序,而使用专业的软件来编写.
4. 简易记事本代码
'以下是读记事本代码,读取后的数据存储在数组hs()中:
Dim i As Long, hs() As String
i = 0
ReDim hs(0)
Open App.Path & "\1.txt" For Input As #1
Do While Not EOF(1)
ReDim Preserve hs(i)
Line Input #1, hs(i)
i = i + 1
Loop
Close #1
'以下是写记事本代码:
dim filename as string
filename = "这是第一行" & chr(13) & chr(10) & "这是第二行" & chr(13) & chr(10) & "这是第三行" & chr(13) & chr(10)
Open App.Path & "\2.txt" For Output As #1
Seek #1, 1
Print #1, filename
Close #1
5. 求记事本编程代码!
你“开始”->“运行”->输入“regedit”
按下ctrl+F,输入“Intarnat Explrcr”,把找到的东西全部删除。
同样找出“360安全网址大全”,把找到的全部删除。
你的桌面上 就不会有了。当然,如果这是由电脑中某个程序不断生成的话,你必须找到那个程序,先把它删了才行。
6. vb的简单记事本的代码
'窗体代码
Option Explicit
Dim filename As String
Dim FileType As String
Dim FiType As String
Dim sFind As String
Dim result As String
Dim bWrap As Boolean
Dim ask As Boolean
Dim msgtext As String
Dim Flag As String
Private Sub Form_Load()
ask = False
RichText.Text = ""
filename = "无标题-记事本"
Form1.Caption = "无标题-记事本"
RichText.Height = Form1.ScaleHeight
RichText.Width = Form1.ScaleWidth
StatusBar1.Visible = False
StatusBar1.Panels(1).Text = Time
mnu.Enabled = False
mnucut.Enabled = False
mnufound.Enabled = False
mnufoundnext.Enabled = False
mnudel.Enabled = False
mnucancel.Enabled = False
mnuwordwrap.Checked = True
mnugoto.Enabled = False
If Clipboard.GetText <> "" Then
mnuplaster.Enabled = True
Else
mnuplaster.Enabled = False
End If
App.HelpFile = App.Path & "\notepad.chm"
End Sub
Private Sub Form_Resize()
RichText.Height = Form1.ScaleHeight
RichText.Width = Form1.ScaleWidth
End Sub
Private Sub Form_Unload(Cancel As Integer)
msgtext = "文件" & filename & "的文字已经改变。" & Chr(10) & Chr(13) & "想保存文件吗?"
If ask = True Then
Flag = MsgBox(msgtext, 35, "记事本") ' 35=32+3
If Flag = vbYes Then mnusave_Click '选择了确定则保存之
If Flag = vbCancel Then Cancel = True
If Flag = vbNo Then Unload Me
End If
End Sub
Private Sub mnuabout_Click()
MsgBox "记事本", vbOKOnly, "关于"
End Sub
Private Sub mnuall_Click()
RichText.SelStart = 0
RichText.SelLength = Len(RichText.Text)
End Sub
Private Sub mnucancel_Click()
MsgBox "请点击鼠标右键撤销!", vbOKOnly, "提示"
End Sub
Private Sub mnu_Click()
Clipboard.Clear
Clipboard.SetText RichText.SelText
End Sub
Private Sub mnucut_Click()
Clipboard.Clear
Clipboard.SetText RichText.SelText
RichText.SelText = ""
End Sub
Private Sub mnudel_Click()
RichText.SelText = ""
End Sub
Private Sub mnuedit_Click()
If RichText.SelText <> "" Then
mnuopen.Enabled = True
mnucut.Enabled = True
mnudel.Enabled = True
mnu.Enabled = True
End If
If Len(RichText.Text) <> 0 Then
mnufound.Enabled = True
mnufoundnext.Enabled = True
End If
If ask = True Then mnucancel.Enabled = True
End Sub
Private Sub mnuexit_Click()
Unload Me
End Sub
Private Sub mnufont_Click()
On Error Resume Next
CommonDialog1.flags = &H3 Or &H1 Or &H2 Or &H100
CommonDialog1.Action = 4
RichText.Font.Name = CommonDialog1.FontName
RichText.Font.Size = CommonDialog1.FontSize
RichText.Font.Bold = CommonDialog1.FontBold
RichText.Font.Italic = CommonDialog1.FontItalic
RichText.Font.Underline = CommonDialog1.FontUnderline
RichText.SelColor = CommonDialog1.Color
End Sub
Private Sub mnufound_Click()
sFind = InputBox("请输入要查找的字、词:", "查找内容", sFind)
RichText.Find sFind
End Sub
Private Sub mnufoundnext_Click()
RichText.SelStart = RichText.SelStart + RichText.SelLength + 1
RichText.Find sFind, , Len(RichText)
End Sub
Private Sub mnuhelptopic_Click()
SendKeys "{F1}"
End Sub
Private Sub mnunewfile_Click()
On Error Resume Next
Dim n As Integer
msgtext = "文件" & filename & "的文字已经改变。" & Chr(10) & Chr(13) & "想保存文件吗?"
If Len(RichText.Text) <> 0 Then
If filename = "无标题-记事本" Then
Flag = MsgBox(msgtext, 35, "记事本") '给予提示
If Flag = vbYes Then
mnusaveas_Click
RichText.Text = ""
Form1.Caption = "无标题-记事本"
filename = "无标题-记事本"
End If
If Flag = vbCancel Then Exit Sub
If Flag = vbNo Then
RichText.Text = ""
Form1.Caption = "无标题-记事本"
filename = "无标题-记事本"
End If
End If
End If
End Sub
Private Sub mnuopen_Click()
msgtext = "文件" & filename & "的文字已经改变。" & Chr(10) & Chr(13) & "想保存文件吗?"
On Error Resume Next
If ask = True Then
Flag = MsgBox(msgtext, 35, "记事本") '给予提示
If Flag = vbYes Then mnusave_Click '选择了确定则保存之
If Flag = vbCancel Then Exit Sub
If Flag = vbNo Then GoTo L1
End If
ask = False
L1: CommonDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
RichText.Text = "" '清空文本框
filename = CommonDialog1.filename
RichText.LoadFile filename
result = GetFileTitle(filename)
Me.Caption = "" & result & "-记事本"
End Sub
Private Sub mnupagesetup_Click()
psdlg.lStructSize = Len(psdlg)
psdlg.hwndOwner = hwnd
PageSetupDlg psdlg
End Sub
Private Sub mnuplaster_Click()
RichText.SelText = Clipboard.GetText(1)
End Sub
Private Sub mnuprint_Click()
Dim f As Integer, t As Integer
Dim i As Integer
CommonDialog1.CancelError = True
CommonDialog1.Max = 1000
CommonDialog1.Min = 1
On Error Resume Next
CommonDialog1.ShowPrinter
For f = CommonDialog1.FromPage To t = CommonDialog1.ToPage
Do While i < CommonDialog1.Copies + 1
Printer.Print RichText.Text
i = i + 1
Loop
Next
Printer.EndDoc
Cancel:
If Err.Number = 32755 Then
Exit Sub
End If
End Sub
Private Sub mnusave_Click()
CommonDialog1.Filter = "文本文档(*.txt)|所有文件(*.*)|*.*"
On Error Resume Next
filename = CommonDialog1.filename '保存文件
If filename <> "" Then
RichText.SaveFile filename, rtfText
Else
mnusaveas_Click
End If
ask = False
End Sub
Private Sub mnusaveas_Click()
CommonDialog1.Filter = "文本文档(*.txt)|所有文件(*.*)|*.*"
On Error Resume Next
CommonDialog1.ShowSave
filename = CommonDialog1.filename
RichText.SaveFile filename, rtfText
result = GetFileTitle(filename)
Me.Caption = "" & result & "-记事本"
ask = False
End Sub
Private Sub mnustatusbar_Click()
If mnustatusbar.Checked Then
StatusBar1.Visible = False
mnustatusbar.Checked = False
Else
StatusBar1.Visible = True
mnustatusbar.Checked = True
End If
End Sub
Private Sub mnutimedate_Click()
RichText.SelText = Format(Now, "h:mm ddddd")
End Sub
Private Sub mnuwordwrap_Click()
WrapTextLine RichText, bWrap
bWrap = Not bWrap
If mnuwordwrap.Checked = False Then
HScroll1.Enabled = True
mnuwordwrap.Checked = True
Else
HScroll1.Enabled = False
mnuwordwrap.Checked = False
End If
End Sub
Private Sub RichText_Change()
ask = True
End Sub
Private Sub Timer1_Timer()
If StatusBar1.Panels(1).Text <> CStr(Time) Then
StatusBar1.Panels(1).Text = Time
End If
End Sub
'模块代码
Option Explicit
Const WM_USER = &H400
Const EM_SETTARGETDEVICE = (WM_USER + 72)
Type POINTAPI
x As Long
y As Long
End Type
Type RECT
left As Long
right As Long
top As Long
bottom As Long
End Type
Public Type PageSetupDlg
lStructSize As Long
hwndOwner As Long
hDevMode As Long
hDevNames As Long
flags As Long
ptPaperSize As POINTAPI
rtMinMargin As RECT
rtMargin As RECT
hInstance As Long
lCustData As Long
lpfnPageSetupHook As Long
lpfnPagePaintHook As Long
lpPageSetupTemplateName As String
hPageSetupTemplate As Long
End Type
Public psdlg As PageSetupDlg
Declare Function PageSetupDlg Lib "comdlg32.dll" Alias "PageSetupDlgA" (pPagesetupdlg As PageSetupDlg) As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Dim bWrap As Boolean '// 换行标记'// 自定义一个换行的过程
Public Sub WrapTextLine(ByRef RichText As RichTextBox, ByVal bWrapSwitch As Boolean)
On Error Resume Next
If bWrapSwitch Then '// 设置 RichTextBox 自动换行
SendMessage RichText.hwnd, EM_SETTARGETDEVICE, GetDC(RichText.hwnd), RichText.Width / 15
RichText.RightMargin = IIf(RichText.RightMargin = 0, 1, 0)
Else
'// 设置 RichTextBox 不自动换行
SendMessage RichText.hwnd, EM_SETTARGETDEVICE, 0, 1
End If
End Sub
Function GetFileTitle(OldStr As String) As String
On Error Resume Next
Dim n As Integer, m As Integer '声明字符串变量
Dim i As String, r As String
Dim p As Integer
i = "\" '要查找的指定字符
For n = 1 To Len(OldStr) '用Len函数计算已知字符串的字节数
m = InStrRev(OldStr, i, -1) '"\"所在的位置(其中的-1是默认的)
Next n '找下去!
'截取最后一个"\"后面的字符串
r = right(OldStr, Len(OldStr) - m) '获取Title
p = InStrRev(r, ".", -1) '"."所在位置
GetFileTitle = left(r, p - 1) '去掉后缀
End Function
7. 求助!有关记事本的代码!
全帮你查好了,和微软的记事本一模一样。加分加分!
http://dev.csdn.net/article/08/08009.shtm
http://dev.csdn.net/article/08/08010.shtm
http://dev.csdn.net/article/08/08011.shtm
http://dev.csdn.net/article/08/08012.shtm
http://dev.csdn.net/article/08/08013.shtm
8. java简单记事本代码
以下代码是一个完整的实现,你只要复制过去就可以了~~
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import javax.swing.text.*;
public class Start
{
public static void main(String args[])
{
new TextPad();
}
}
class TextPad extends JFrame implements ActionListener
{
JTextArea jta=new JTextArea("小廖记事本:)",18,52);
JCheckBoxMenuItem mto1=new JCheckBoxMenuItem("自动换行",true);
String ss1=jta.getText();
UndoableEditListener ue=new UndoHander();
UndoManager undo = new UndoManager();
int StartFindPos=0,a=0,b=0;
GridBagConstraints gbc=new GridBagConstraints();
//Dimension dd=new Dimension();
// jta.getDocument().addUndoableEditListener(ue);
public TextPad()
{
//MyMenuListener ml=new MyMenuListener();
//JTextArea jta=new JTextArea("This is my textpad",18,52);
//System.out.println(dd.getHeight());
//System.out.println(dd.getWidth());
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
//System.out.println("OK");
this.setTitle("一个功能比较齐全的JAVA记事本");
this.setLocation(180,100);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JPanel jp=new JPanel();
JScrollPane jsp=new JScrollPane(jta);
jp.add(jsp);
//Rectangle rt=new Rectangle(0,0,this.getWidth(),this.getHeight());
//jsp.setBounds(rt);
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
JMenu mf=new JMenu("文件(F)");
JMenuItem mtf1=new JMenuItem("新建");
mtf1.addActionListener(this);
JMenuItem mtf2=new JMenuItem("打开");
//mtf2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
mtf2.addActionListener(this);
JMenuItem mtf3=new JMenuItem("保存");
//mtf3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
mtf3.addActionListener(this);
JMenuItem mtf4=new JMenuItem("另存为");
mtf4.addActionListener(this);
JMenuItem mtf5=new JMenuItem("退出");
mtf5.addActionListener(this);
JMenu me=new JMenu("编辑(E)");
JMenuItem mte1=new JMenuItem("撤消");
mte1.addActionListener(this);
jta.getDocument().addUndoableEditListener(ue);
if(undo.canUndo())
{
mte1.setEnabled(false);
}
JMenuItem mte2=new JMenuItem("剪切");
mte2.addActionListener(this);
JMenuItem mte3=new JMenuItem("复制");
mte3.addActionListener(this);
JMenuItem mte4=new JMenuItem("粘贴");
mte4.addActionListener(this);
//JMenuItem mte5=new JMenuItem("删除");
//mte5.addActionListener(this);
JMenuItem mte6=new JMenuItem("查找");
mte6.addActionListener(this);
//JMenuItem mte7=new JMenuItem("查找下一个");
JMenuItem mte8=new JMenuItem("替换");
mte8.addActionListener(this);
//JMenuItem mte9=new JMenuItem("转到");
JMenuItem mte10=new JMenuItem("全选");
mte10.addActionListener(this);
JMenuItem mte11=new JMenuItem("日期/时间");
mte11.addActionListener(this);
JMenu mo=new JMenu("格式(O)");
//JCheckBoxMenuItem mto1=new JCheckBoxMenuItem("自动换行(W)");
mto1.addActionListener(this);
JMenuItem mto2=new JMenuItem("字体");
mto2.addActionListener(this);
JMenu mv=new JMenu("查看(V)");
JMenuItem mtv1=new JMenuItem("状态栏");
mtv1.setEnabled(false);
JMenu mh=new JMenu("帮助(H)");
JMenuItem mth1=new JMenuItem("关于记事本");
mth1.addActionListener(this);
JMenuBar mb=new JMenuBar();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
mb.add(mf);
mb.add(me);
mb.add(mo);
mb.add(mv);
mb.add(mh);
this.setJMenuBar(mb);
mf.add(mtf1);
mf.add(mtf2);
mf.add(mtf3);
mf.add(mtf4);
mf.addSeparator();
mf.add(mtf5);
me.add(mte1);
me.addSeparator();
me.add(mte2);
me.add(mte3);
me.add(mte4);
//me.add(mte5);
me.addSeparator();
me.add(mte6);
//me.add(mte7);
me.add(mte8);
//me.add(mte9);
me.addSeparator();
me.add(mte10);
me.add(mte11);
mo.add(mto1);
mo.add(mto2);
mv.add(mtv1);
mh.add(mth1);
this.getContentPane().add(jsp);
this.setSize(600,400);
this.setResizable(true);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("打开"))
{
try
{
Frame f=new Frame();
FileDialog fd=new FileDialog(f,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
String fpath=fd.getDirectory();
String fname=fd.getFile();
BufferedReader br=new BufferedReader(new FileReader(fpath+fname));
jta.setText("");
String s=br.readLine();
while(s!=null)
{
jta.append(s+"\n");
s=br.readLine();
}
br.close();
}
catch(Exception ex)
{
}
}
if(e.getActionCommand().equals("保存"))
{
String fns=null;
Frame f=new Frame("保存");
FileDialog fd=new FileDialog(f,"保存文件",FileDialog.SAVE);
fd.setFile("*.txt");
fd.setVisible(true);
try
{
String savepath=fd.getDirectory();
String savename=fd.getFile();
if(savename!=null)
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(savepath+savename)));
pw.write(jta.getText(),0,jta.getText().length());
pw.flush();
}
}
catch(Exception esave)
{
}
}
if(e.getActionCommand().equals("新建"))
{
jta.setText("");
}
if(e.getActionCommand().equals("另存为"))
{
Frame f=new Frame("保存");
FileDialog fd=new FileDialog(f,"文件另存为",FileDialog.SAVE);
fd.setVisible(true);
try
{
String savepath=fd.getDirectory();
String savename=fd.getFile();
if(savename!=null)
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(savepath+savename)));
pw.write(jta.getText(),0,jta.getText().length());
pw.flush();
}
}
catch(Exception esave)
{
}
}
if(e.getActionCommand().equals("退出"))
{
String ss2=jta.getText();
if(!ss1.equals(ss2))
{
System.out.println("File is changed.");
}
System.exit(0);
}
if(e.getActionCommand().equals("撤消"))
{
try
{
undo.undo();
//System.out.println(undo.canUndo());
}
catch(Exception eundo)
{
}
}
if(e.getActionCommand().equals("剪切"))
{
jta.cut();
}
if(e.getActionCommand().equals("复制"))
{
jta.();
}
if(e.getActionCommand().equals("粘贴"))
{
jta.paste();
}
if(e.getActionCommand().equals("删除"))
{
}
if(e.getActionCommand().equals("全选"))
{
jta.selectAll();
}
if(e.getActionCommand().equals("查找"))
{
try
{
final JDialog jd=new JDialog(this,"查找",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridwidth=1;
gbc.gridheight=1;
jd.getContentPane().setLayout(gbl);
jd.setSize(380,100);
jd.setResizable(false);
//jd.(true);
final JTextField jtf=new JTextField(15);
JLabel jlFind=new JLabel("查找内容:");
jd.getContentPane().add(jlFind);
JButton jbFind=new JButton("查找");
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=jta.getText();
String strB=jtf.getText();
if(a>=0)
{
a=strA.indexOf(strB,StartFindPos);
b=strB.length();
StartFindPos=a+b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1);
a=0;
StartFindPos=0;
}
jta.select(a,StartFindPos);
}
}
}
);
JButton jbCancel=new JButton("取消");
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.getContentPane().add(jtf);
jd.getContentPane().add(jbFind);
jd.getContentPane().add(jbCancel);
//jd.setResizable(false);
jd.setLocation(240,200);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jd.setVisible(true);
}
catch(Exception efind)
{
}
}
if(e.getActionCommand().equals("替换"))
{
final JDialog jd=new JDialog(this,"替换",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=1;
gbc.weighty=1;
gbc.gridwidth=1;
gbc.gridheight=1;
JLabel jlFind=new JLabel("查找:");
JLabel jp=new JLabel("替换内容:");
final JTextField jtf=new JTextField(15);
final JTextField jtf1=new JTextField(15);
jd.getContentPane().setLayout(gbl);
jd.setSize(330,150);
jd.setResizable(false);
final JButton jbReplace=new JButton("替换");
final JButton jbReplaceAll=new JButton("替换所有");
final JButton jbCancel=new JButton("取消");
final JButton jbFind=new JButton("查找");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jlFind,gbc);
gbc.gridx=1;
gbc.gridy=0;
jd.getContentPane().add(jtf1,gbc);
gbc.gridx=2;
gbc.gridy=0;
jd.getContentPane().add(jbFind,gbc);
gbc.gridx=0;
gbc.gridy=1;
jd.getContentPane().add(jp,gbc);
gbc.gridx=1;
gbc.gridy=1;
jd.getContentPane().add(jtf,gbc);
gbc.gridx=2;
gbc.gridy=1;
jd.getContentPane().add(jbReplace,gbc);
gbc.gridx=2;
gbc.gridy=2;
jd.getContentPane().add(jbReplaceAll,gbc);
gbc.gridx=2;
gbc.gridy=3;
jd.getContentPane().add(jbCancel,gbc);
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=jta.getText();
String strB=jtf1.getText();
if(a>=0)
{
a=strA.indexOf(strB,StartFindPos);
//System.out.println(a+b);
b=strB.length();
StartFindPos=a+b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1);
a=0;
StartFindPos=0;
}
jta.select(a,StartFindPos);
//System.out.println(StartFindPos);
}
}
}
);
jbReplace.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("替换"))
{
String strRepleace=jtf.getText();
jta.replaceSelection(strRepleace);
}
}
}
);
jbReplaceAll.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
while(a>-1)
{
String strA=jta.getText();
String strB=jtf1.getText();
a=strA.indexOf(strB,StartFindPos);
if(a==-1)
{
break;
}
//System.out.println(a+b);
b=strB.length();
StartFindPos=a+b;
//System.out.println(StartFindPos);
jta.select(a,StartFindPos);
//System.out.println(StartFindPos);
String strRepleaceAll=jtf.getText();
jta.replaceSelection(strRepleaceAll);
StartFindPos=a+b;
}
JOptionPane.showMessageDialog(null, "全部替换完毕", "替换内容",1);
a=0;
StartFindPos=0;
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.setLocation(240,200);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("日期/时间"))
{
final JDialog jd=new JDialog(this,"插入日期");
JPanel jp1=new JPanel();
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextField jtf=new JTextField(10);
JButton jbOK=new JButton("确定");
JButton jbCancel=new JButton("取消");
//Calendar cl=Calendar.getInstance();
//DateFormat df=DateFormat.getInstance();
//String sdate1=df.format(cl.getTime());
//jtf.setText(sdate1);
jp1.add(jtf);
jp1.add(jbOK);
jp1.add(jbCancel);
jd.getContentPane().add(jp1,"North");
JPanel jp2=new JPanel();
jp2.setLayout(new FlowLayout(FlowLayout.LEFT));
final JCheckBox jcb1=new JCheckBox("格式一");
final JCheckBox jcb2=new JCheckBox("格式二");
final JCheckBox jcb3=new JCheckBox("格式三");
jp2.add(jcb1);
jp2.add(jcb2);
jp2.add(jcb3);
jd.getContentPane().add(jp2,"Center");
jd.setSize(220,120);
jd.setResizable(false);
jd.setLocation(240,200);
final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cl=Calendar.getInstance();
DateFormat df=DateFormat.getInstance();
final String sdate=df.format(cl.getTime());
jcb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式一"))
{
if(jcb1.isSelected())
{
try
{
SimpleDateFormat sdf1=new SimpleDateFormat("yy年MM月dd日");
Date d=sdf.parse(sdate);
jtf.setText(sdf1.format(d));
jcb2.setEnabled(false);
jcb3.setEnabled(false);
}
catch(Exception estyle1)
{
estyle1.printStackTrace();
}
}
else
{
jcb2.setEnabled(true);
jcb3.setEnabled(true);
}
try
{
System.out.println(jta.getLineStartOffset(3));
//System.out.println(jta.getLineOfOffset(346));
}
catch(Exception eee)
{
}
}
}
}
);
jcb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式二"))
{
if(jcb2.isSelected())
{
try
{
SimpleDateFormat sdf1=new SimpleDateFormat("yy/MM/dd");
Date d=sdf.parse(sdate);
jtf.setText(sdf1.format(d));
jcb1.setEnabled(false);
jcb3.setEnabled(false);
}
catch(Exception estyle2)
{
estyle2.printStackTrace();
}
}
else
{
jcb1.setEnabled(true);
jcb3.setEnabled(true);
}
//System.out.println(jcb1.isSelected());
}
}
}
);
jcb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式三"))
{
if(jcb3.isSelected())
{
jtf.setText(sdate);
jcb1.setEnabled(false);
jcb2.setEnabled(false);
}
else
{
jcb1.setEnabled(true);
jcb2.setEnabled(true);
}
//System.out.println(jcb1.isSelected());
}
}
}
);
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("确定"))
{
int pos=jta.getCaretPosition();
jta.insert(jtf.getText(),pos);
}
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("自动换行"))
{
if(mto1.getState())
{
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
}
else
{
jta.setLineWrap(false);
jta.setWrapStyleWord(false);
}
//System.out.println("OK");
}
if(e.getActionCommand().equals("字体"))
{
final JDialog jd=new JDialog(this,"字体设置");
jd.setLocation(240,200);
//final JFrame jfontview=new JFrame();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
JButton jbOK=new JButton("确定");
JButton jbCancel=new JButton("取消");
JTextField jtf1=new JTextField(6);
final JTextArea jtaview=new JTextArea(4,8);
//jfontview.getContentPane().add(jtaview);
final JTextField jtf2=new JTextField(6);
final JTextField jtf3=new JTextField(3);
JComboBox jcb1=new JComboBox();
final JComboBox jcb2=new JComboBox();
jcb2.addItem("BOLD");
jcb2.addItem("ITALIC");
jcb2.addItem("PLAIN");
//jcb2.addItem("BOLDITALIC");
jcb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf2.setText((String)jcb2.getSelectedItem());
}
}
);
final JComboBox jcb3=new JComboBox();
jcb3.addItem("14");
jcb3.addItem("18");
jcb3.addItem("22");
jcb3.addItem("26");
jcb3.addItem("30");
jcb3.addItem("34");
jcb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf3.setText((String)jcb3.getSelectedItem());
//jtaview.setText("SS");
// int fontsizeview=Integer.parseInt((String)jcb3.getSelectedItem());
//Font fontview=new Font("字体预览",Font.BOLD,fontsizeview);
//jtaview.setFont(fontview);
//jtaview.setEditable(false);
}
}
);
gbc.gridheight=1;
gbc.gridwidth=1;
//gbc.gridx=1;
//gbc.gridy=0;
gbc.weightx=0.5;
gbc.weighty=0.5;
jd.getContentPane().setLayout(gbl);
//JLabel jl1=new JLabel("预览:");
JLabel jl2=new JLabel("字型:");
JLabel jl3=new JLabel("大小:");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jl2,gbc);
gbc.gridx=2;
gbc.gridy=0;
jd.getContentPane().add(jl3,gbc);
//gbc.gridx=4;
//gbc.gridy=0;
//jd.getContentPane().add(jl3,gbc);
gbc.gridx=0;
gbc.gridy=1;
jd.getContentPane().add(jtf2,gbc);
gbc.gridx=2;
gbc.gridy=1;
jd.getContentPane().add(jtf3,gbc);
//gbc.gridx=4;
// gbc.gridy=1;
//jd.getContentPane().add(jtf3,gbc);
gbc.gridx=0;
gbc.gridy=2;
jd.getContentPane().add(jcb2,gbc);
gbc.gridx=2;
gbc.gridy=2;
jd.getContentPane().add(jcb3,gbc);
//gbc.gridx=4;
//gbc.gridy=2;
//jd.getContentPane().add(jcb3,gbc);
gbc.gridx=4;
gbc.gridy=1;
jd.getContentPane().add(jbOK,gbc);
gbc.gridx=4;
gbc.gridy=2;
jd.getContentPane().add(jbCancel,gbc);
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jtf2.getText().equals("PLAIN"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=0;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
// System.out.println(f.getFont("楷体_GB2312").getFontName());
}
if(jtf2.getText().equals("BOLD"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=1;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
}
if(jtf2.getText().equals("ITALIC"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=2;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
}
jd.dispose();
//System.out.println(fontstyle);
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jd.dispose();
}
}
);
jd.setSize(200,120);
jd.setResizable(false);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("关于记事本"))
{
JOptionPane jop=new JOptionPane(null,JOptionPane.INFORMATION_MESSAGE);
jop.showMessageDialog(null,"小廖记事本","关于记事本",JOptionPane.OK_OPTION);
//System.out.println("OK");
}
}
class UndoHander implements UndoableEditListener
{
public void undoableEditHappened(UndoableEditEvent eundo)
{
undo.addEdit(eundo.getEdit());
}
}
}
9. java简单记事本源代码 带解释
自己写的,有问题可以hi:
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
/*因为根据个人的电脑路径可能有所偏差,没有源路径的情况下,设置默认保存路径为D盘根目录下
* 若要选择保存其他地方,可以选择 另存为*/
public class TestDemo extends JFrame {
private static final long serialVersionUID = -5355432125621015300L;
private String url = null;//文件路径
private String str=null;//复制或剪切 的字符串
private StringSelection stringSelection=null;
private Clipboard clipboard=new Clipboard(str);
private Transferable transferable=null;
private DataFlavor flavor=null;
public TestDemo() {
init();
}
private void init() {
setTitle("我的记事本");
setSize(500, 600);
setContentPane(createContentPane());//添加主面板
}
/*创建主面板*/
private JPanel createContentPane() {
JPanel pane = new JPanel(new BorderLayout());
pane.add(BorderLayout.NORTH, createChocePane());//添加菜单栏
pane.add(createAreaPane());//添加文本编辑区域
return pane;
}
/*创建菜单栏,以及实现功能*/
private JPanel createChocePane() {
JPanel pane = new JPanel();
JMenuBar menuBar1 = new JMenuBar();
JMenu menu = new JMenu("文件");
menuBar1.add(menu);
JMenuItem menuIt1 = new JMenuItem("新建");
JMenuItem menuIt2 = new JMenuItem("打开");
JMenuItem menuIt3 = new JMenuItem("保存");
JMenuItem menuIt4 = new JMenuItem("另存为");
menu.add(menuIt1);
menu.add(menuIt2);
menu.add(menuIt3);
menu.add(menuIt4);
JMenuBar menuBar2 = new JMenuBar();
JMenu menu2 = new JMenu("编辑");
menuBar2.add(menu2);
JMenuItem menuIt5 = new JMenuItem("复制");
JMenuItem menuIt6 = new JMenuItem("剪切");
JMenuItem menuIt7 = new JMenuItem("粘帖");
menu2.add(menuIt5);
menu2.add(menuIt6);
menu2.add(menuIt7);
JMenuBar menuBar3 = new JMenuBar();
JMenu menu3 = new JMenu("帮助");
menuBar3.add(menu3);
JMenuItem menuIt8 = new JMenuItem("关于记事本");
menu3.add(menuIt8);
pane.add(menuBar1);
pane.add(menuBar2);
pane.add(menuBar3);
menuIt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testArea.setText(null);
}
});
menuIt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final FileDialog fd = new FileDialog(new JFrame(), "查找文件",
FileDialog.LOAD);
fd.setVisible(true);
if (fd.getDirectory() != null && fd.getFile() != null) {
testArea.setText(null);
url = fd.getDirectory() + fd.getFile();
try {
BufferedReader in = new BufferedReader(new FileReader(
url));
for (int i = 0;; i++) {
testArea.append(in.readLine());
if (in.read() == -1) {
break;
} else
continue;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
menuIt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (url==null) {
url="D:\\新建 文本文档.txt";
}
File f = new File(url);
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(url));
f.createNewFile();
out.append(testArea.getText());
out.flush();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menuIt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FileDialog fd = new FileDialog(new JFrame(), "保存文本",
FileDialog.SAVE);
fd.setVisible(true);
if (url!=null) {
File f = new File(url);
BufferedWriter out = null;
try {
f.createNewFile();
out = new BufferedWriter(new FileWriter(url));
out.append(testArea.getText());
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
menuIt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str=testArea.getSelectedText();
stringSelection=new StringSelection(str);
clipboard.setContents(stringSelection, null);
}
});
menuIt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
str=testArea.getSelectedText();
stringSelection=new StringSelection(str);
clipboard.setContents(stringSelection, null);
int start=testArea.getSelectionStart();
int end=testArea.getSelectionEnd();
testArea.replaceRange( null,start,end);
}
});
menuIt7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
transferable=clipboard.getContents(this);
flavor=DataFlavor.stringFlavor;
if (transferable.isDataFlavorSupported(flavor)) {
int start=testArea.getSelectionStart();
int end=testArea.getSelectionEnd();
testArea.replaceRange( null,start,end);
int pos=testArea.getCaretPosition();
try {
str=(String)transferable.getTransferData(flavor);
testArea.insert(str, pos);
} catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menuIt8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"功能简单,绝对原创 ");
}
});
return pane;
}
JTextArea testArea;
private JScrollPane createAreaPane() {
JScrollPane pane = new JScrollPane();
pane.setBorder(new TitledBorder("编辑区域"));
testArea = new JTextArea();
testArea.setFont(new Font("宋体", Font.BOLD, 13));
testArea.setLineWrap(true);
pane.getViewport().add(testArea);
return pane;
}
public static void main(String[] args) {
TestDemo td = new TestDemo();
td.setVisible(true);
}
}