import java.awt.*
import java.awt.event.*
import javax.swing.*
import javax.swing.undo.CannotRedoException
import javax.swing.undo.UndoManager
import java.util.Date
import java.text.SimpleDateFormat
public class Notepad extends JFrame {
// 菜单
JMenuBar menub = new JMenuBar()
// 显示纯文本的多行区域
JTextArea text = new JTextArea()
JMenu files = new JMenu("文件(F)")
JMenu edit = new JMenu("编辑(E)")
JMenu formats = new JMenu("格式(O)")
JMenu help = new JMenu("帮助(H)")
JMenuItem newFile = new JMenuItem("新建(N)")
JMenuItem open = new JMenuItem("打开(O)"纳轮)
JMenuItem save = new JMenuItem("保存(S)")
JMenuItem saveAs = new JMenuItem("另存为(A)")
JMenuItem exit = new JMenuItem("退出(X)")
JMenuItem undo = new JMenuItem("撤销(U)")
JMenuItem cut = new JMenuItem("剪切(T)")
JMenuItem copy = new JMenuItem("复祥含制(C)")
JMenuItem paste = new JMenuItem("粘贴(P)")
JMenuItem selectAll = new JMenuItem("全选(A)")
JMenuItem timeDate = new JMenuItem("时间/日期(D)")
JCheckBoxMenuItem lineWrap = new JCheckBoxMenuItem("自动换行(M)")
JMenuItem fonts = new JMenuItem("字体")
JMenuItem about = new JMenuItem("关于记事本(A)")
JFrame th = this
String name
String openedPath = null
boolean opened = false
boolean reworked = false
UndoManager undoManager = new UndoManager()
// 初始化窗体
public Notepad(String name) {
super(name)
this.name = name
int x, y
// 得到用户屏幕大小
Dimension size = Toolkit.getDefaultToolkit().getScreenSize()
x = (size.width - 600) / 2
y = (size.height - 400) / 2
setSize(600, 400)
// 让程序谨茄笑界面显示在屏幕中央
setLocation(x, y)
// 将此窗口的最小大小设置为一个常量值。
setMinimumSize(new Dimension(250, 150))
setDefaultCloseOperation(EXIT_ON_CLOSE)
}
// 初始化布局
void init() {
files.setMnemonic('F')
edit.setMnemonic('E')
formats.setMnemonic('O')
help.setMnemonic('H')
newFile.setMnemonic('N')
open.setMnemonic('O')
save.setMnemonic('S')
saveAs.setMnemonic('A')
exit.setMnemonic('X')
undo.setMnemonic('U')
cut.setMnemonic('T')
copy.setMnemonic('C')
paste.setMnemonic('P')
selectAll.setMnemonic('A')
timeDate.setMnemonic('D')
lineWrap.setMnemonic('M')
about.setMnemonic('A')
// 为控件添加助记符
newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_MASK))
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK))
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK))
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
InputEvent.CTRL_MASK))
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK))
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK))
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK))
selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
InputEvent.CTRL_MASK))
// 为控件添加快捷键
timeDate.setAccelerator(KeyStroke.getKeyStroke("F5"))
files.add(newFile)
files.add(open)
files.add(save)
files.add(saveAs)
files.addSeparator()
files.add(exit)
edit.add(undo)
edit.addSeparator()
edit.add(cut)
edit.add(copy)
edit.add(paste)
edit.addSeparator()
edit.add(selectAll)
edit.add(timeDate)
formats.add(lineWrap)
formats.add(fonts)
help.add(about)
menub.add(files)
menub.add(edit)
menub.add(formats)
menub.add(help)
setJMenuBar(menub)
getContentPane().add(new JScrollPane(text))
Listen listen = new Listen()
Listen1 listen1 = new Listen1()
// 为控件添加事件侦听器
newFile.addActionListener(listen)
undo.addActionListener(listen)
open.addActionListener(listen)
save.addActionListener(listen)
saveAs.addActionListener(listen)
exit.addActionListener(listen)
cut.addActionListener(listen)
copy.addActionListener(listen)
paste.addActionListener(listen)
selectAll.addActionListener(listen)
timeDate.addActionListener(listen)
lineWrap.addActionListener(listen)
about.addActionListener(listen)
open.addActionListener(listen1)
save.addActionListener(listen1)
saveAs.addActionListener(listen1)
// 暂时没有实现的功能 :设置字体
//undo功能没有实现
fonts.setEnabled(false)
}
class Listen implements ActionListener {
// 实现用于一般 *** 作的事件侦听器
public void actionPerformed(ActionEvent e) {
Object source = e.getSource()
if (source == newFile) {
text.setText("")
// 设置标题
th.setTitle(name)
openedPath = null
opened = false
} else if (source == exit)
setVisible(false)
else if (source == undo)
try {
//此功能没有实现 撤销要用栈?
// undo.setEnabled(undoManager.canUndo())
undoManager.undo()
} catch (CannotRedoException cre) {
cre.printStackTrace()
}
else if (source == selectAll)
text.selectAll()
else if (source == cut)
text.cut()
else if (source == copy)
text.copy()
else if (source == paste)
text.paste()
else if (source == lineWrap)
// 设置文本区的换行策略(获取文本区的换行策略)
text.setLineWrap(!text.getLineWrap())
else if (source == about) {
String message = "--------\n版本:1.0\n作者:时超" +
"\n撤销功能要用堆栈存贮 *** 作" +
"\n还有字体格式"+
"\n暂时没有实现" +
"\n\n感谢您的使用"
JOptionPane.showMessageDialog(th, message, "关于",
JOptionPane.PLAIN_MESSAGE)
} else if (source == timeDate) {
Date nowTime = new Date()
SimpleDateFormat times = new SimpleDateFormat(
"HH:mm yyyy-MM-dd")
text.insert(times.format(nowTime), text.getCaretPosition())
}
}
}
class Listen1 implements ActionListener {
// 实现用于对文件进行 *** 作的事件侦听器
public void actionPerformed(ActionEvent e) {
Object source = e.getSource()
// 打开文件事件
if (source == open) {
// 显示对话窗口 以便选择文件
FileDialog openFile = new FileDialog(th, "打开文件",
FileDialog.LOAD)
openFile.setVisible(true)
// 获取文件路径
String filePath = openFile.getDirectory() + openFile.getFile()
try {
FileInputStream fis = new FileInputStream(filePath)
byte[] content = new byte[fis.available()]
fis.read(content)
text.setText(new String(content))
// 设置 TextComponent 的文本插入符的位置
text.setCaretPosition(0)
if (openFile.getFile() != null) {
th.setTitle(openFile.getFile() + name)
openedPath = filePath
opened = true
}
fis.close()
} catch (Exception ex) {
ex.printStackTrace()
}
opened = true
}
// 保存及另存为事件
else if (source == save || source == saveAs) {
String savePath = openedPath
if (savePath == null || source == saveAs) {
// 如果 mode 的值为 LOAD,那么文件对话框将查找要读取的文件,所显示的文件是当前目录中的文件
// 如果 mode 的值为 SAVE,则文件对话框将查找要写入文件的位置。
FileDialog saveFile = new FileDialog(th, "保存文件",
FileDialog.SAVE)
saveFile.setVisible(true)
savePath = saveFile.getDirectory() + saveFile.getFile()
}
try {
FileOutputStream fos = new FileOutputStream(savePath)
fos.write(text.getText().getBytes())
fos.close()
} catch (Exception ex) {
ex.printStackTrace()
}
if (source == save)
openedPath = savePath
}
}
}
public static void main(String[] args) {
try {
// 使用当前线程的上下文类加载器加载给定类名称所指定的 LookAndFeel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
} catch (Exception e) {
System.err.println("Couldn't use the system look and feel:" + e)
}
String name = ("--记事本 版本1.0--")
Notepad note = new Notepad(name)
note.init()
note.setVisible(true)
}
}
这是我写的代码,相信对你有用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
'设置d出式菜单(即在编辑框中单击鼠标右键时d出的动态菜单)
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)