用JAVA 编写一个记事本,要有保存和设置字体的功能

用JAVA 编写一个记事本,要有保存和设置字体的功能~我要源代码~~

import java.awt.*;
import java.io.*;

public class MyClipboard extends Frame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 5541943532647624059L;

private TextArea editor = new TextArea();

private MyFile mf = new MyFile(this);

private MyClipboard1 cb =new MyClipboard1();

private MyFindDialog findDlg = new MyFindDialog(this, editor);

public MyClipboard(String title) {
super(title);
MyMenuBar mb = new MyMenuBar(this);
mb.addMenus(new String[] { "文件", "编辑", "查找", "帮助" });
mb.addMenuItems(0, new String[] { "新建", "打开", "保存", null, "退出" });
mb.addMenuItems(1, new String[] { "剪贴", "复制", "粘贴", "清除", null, "全选" });
mb.addMenuItems(2, new String[] { "查找", null, "查找替换" });
mb.addMenuItems(3, new String[] { "我的记事本信息" });
add(editor);
mb.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MyClipboard.this.dispose();
}
});
}

public void actionPerformed(ActionEvent e) {
String selected = e.getActionCommand();
if (selected.equals("新建"))
editor.setText("");
else if (selected.equals("打开")) {
try {
editor.setText(mf.getData());
} catch (IOException ie) {
}
} else if (selected.equals("保存")) {
try {
mf.setData(editor.getText());
} catch (IOException ie) {
}
} else if (selected.equals("退出")) {
dispose();
} else if (selected.equals("剪贴")) {
cb.setData(editor.getSelectedText());
editor.replaceRange("", editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("复制")) {
cb.setData(editor.getSelectedText());
} else if (selected.equals("粘贴")) {
String str = cb.getData();
editor.replaceRange(str, editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("清除")) {
editor.replaceRange("", editor.getSelectionStart(), editor
.getSelectionEnd());
} else if (selected.equals("全选")) {
editor.setSelectionStart(0);
editor.setSelectionEnd(editor.getText().length());
} else if (selected.equals("查找")) {
findDlg.showFind();
} else if (selected.equals("查找替换")) {
findDlg.showReplace();
}
}

public static void main(String[] args) {
MyClipboard memo = new MyClipboard("我的记事本");
memo.setSize(500, 500);
memo.setLocation(239, 120);
memo.setVisible(true);
}
}

class MyClipboard1 {
private Clipboard cb;

public MyClipboard1() {
cb = Toolkit.getDefaultToolkit().getSystemClipboard();
}

public void setData(String data) {
cb.setContents(new StringSelection(data), null);
}

public String getData() {
Transferable content = cb.getContents(null);
try {
return (String) content.getTransferData(DataFlavor.stringFlavor);
} catch (Exception ue) {
}
return null;
}
}

class MyFile {
private FileDialog fDlg;

public MyFile(Frame parent) {
fDlg = new FileDialog(parent, "", FileDialog.LOAD);
}

private String getPath() {
return fDlg.getDirectory() + "\\" + fDlg.getFile();
}

public String getData() throws IOException {
fDlg.setTitle("打开");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br = new BufferedReader(new FileReader(getPath()));
StringBuffer sb = new StringBuffer();
String aline;
while ((aline = br.readLine()) != null)
sb.append(aline + '\n');
br.close();
return sb.toString();
}

public void setData(String data) throws IOException {
fDlg.setTitle("保存");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw = new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}

class MyFindDialog extends Dialog implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 4380007102323378083L;

private Label lFind = new Label("查找字符串:");

private Label lReplace = new Label("替换字符串:");

private TextField tFind = new TextField(10);

private TextField tReplace = new TextField(10);

private Button bFind = new Button("查找");

private Button bReplace = new Button("替换");

private TextArea ta;

public MyFindDialog(Frame owner, TextArea ta) {
super(owner, "查找", false);
this.ta = ta;
setLayout(null);
lFind.setBounds(10, 30, 80, 20);
lReplace.setBounds(10, 70, 80, 20);
tFind.setBounds(90, 30, 90, 20);
tReplace.setBounds(90, 70, 90, 20);
bFind.setBounds(190, 30, 80, 20);
bReplace.setBounds(190, 70, 80, 20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MyFindDialog.this.dispose();
}
});
}

public void showFind() {
setTitle("查找");
setSize(280, 60);
setVisible(true);
}

public void showReplace() {
setTitle("查找替换");
setSize(280, 110);
setVisible(true);
}

private void find() {
String text = ta.getText();
String str = tFind.getText();
int end = text.length();
int len = str.length();
int start = ta.getSelectionEnd();
if (start == end)
start = 0;
for (; start <= end - len; start++) {
if (text.substring(start, start + len).equals(str)) {
ta.setSelectionStart(start);
ta.setSelectionEnd(start + len);
return;
}
}
ta.setSelectionStart(end);
ta.setSelectionEnd(end);
}

private void replace() {
String str = tReplace.getText();
if (ta.getSelectedText().equals(tFind.getText()))
ta.replaceRange(str, ta.getSelectionStart(), ta.getSelectionEnd());
else
find();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == bFind)
find();
else if (e.getSource() == bReplace)
replace();
}
}

class MyMenuBar extends MenuBar implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2311645080753585104L;

public MyMenuBar(Frame parent) {
parent.setMenuBar(this);
}

public void addMenus(String[] menus) {
for (int i = 0; i < menus.length; i++)
add(new Menu(menus[i]));
}

public void addMenuItems(int menuNumber, String[] items) {
for (int i = 0; i < items.length; i++) {
if (items[i] != null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else
getMenu(menuNumber).addSeparator();
}
}

public void addActionListener(ActionListener al) {
for (int i = 0; i < getMenuCount(); i++)
for (int j = 0; j < getMenu(i).getItemCount(); j++)
getMenu(i).getItem(j).addActionListener(al);
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-09-13
没。。。JAVA本身 有这种的软件 国产的么?

用java设计一个电子记事本,要含保存,新建等基本功能,用EditPlus或Utredi...
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import...

用JAVA编写一个记事本~只要实现以下功能:插入~删除~查找~保存~另存为...
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.Window...

用JAVA编个简单的记事本程序
dialog=new Dialog(this, "字体设置");dialog.setSize(300,300); dialog1=new Dialog(this, "帮助主题");dialog1.setBounds(250,100,300,400); dialog.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ dialog.setVisible(false);} }); dialog1.addWindowListener(new Window...

java windows记事本源代码
import java.io.*;public class Notepad extends JFrame implements ActionListener { private JTextArea textArea;private JFileChooser fileChooser;private String filePath = ""; \/\/ 保存文件路径 private JButton saveButton, openButton;public Notepad { setTitle; \/\/ 设置窗口标题 setSize; \/\/ 设置...

如何用JAVA程序编写一个记事本
用图形用户界面实现。能实现编辑、保存、另存为、查找替换等功能。提示:使用文件输入输出流。还要有设计报告1、设计分折2、程序结构(画流程图)3、各模块的功能及程序说明4、源程序... 用图形用户界面实现。能实现编辑、保存、另存为、查找替换等功能。提示:使用文件输入输出流。还要有设计报告1、设计分折2、程序...

我用java编了记事本代码但是解决不了字体和替换的问题,我怎么办?
记事本的字体需要选中后设置字体功能 如:font.setFont("");就可以了.若需要其它字体,可以把特别字体放到C:window\/font 文件夹下,然后使用默认的window风格,再设置一下字体.希望可以帮助到你.

用java编写记事本(输入,打开,保存功能就行)或者计算器(实现加减乘除就...
\/\/设置JFrame super("Calculator");this.setBounds(480, 320, 240, 220);this.setResizable(false);this.setBackground(Color.lightGray);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.getContentPane().setLayout(new FlowLayout());\/\/内容窗格流布局 \/\/文本行 text = new JTextField("0.", ...

用JAVA编写记事本
(1)为“新建” “打开” “保存” “撤消” “复制” “剪切” “粘贴”设置工具栏按钮(2)编辑:查找、查找下一个、替换(3)格式:字体设置、颜色的设置、时间显示(4)添加状态栏,在状态栏显示当前文件是否已修改基本功能要求:实现Window系统中“记事本”的基本功能。 请尽可能的简单,因为小弟我只学了2个月的...

用JAVA中写记事本,如何实现保存和打开文件
\/\/保存和读取,I\/O流操作.我只写了方法 public void readFile()\/\/用于读取文件内容 { try { FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);String str;while((str = br.readLine()) != null){ txtEdit.setText(txtEdit.getText()+str+"\\n");} br....

如何用java一步步编出一个记事本程序
首先是功能划分!就是你要实现什么功能!就拿windowsxp的notepad说,功能就那几个!实现的功能划分出来就开始界面设计了!设计成什么样的,菜单有哪些,怎么布局,这就开始设计界面部分了!然后就开始代码,代码首先是写出界面!然后添加相应的事件!然后调试bug,这样一个记事本就完成了! 已赞过 已踩过< 你对这个回答的评价...

相似回答