import java.io.*;
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 = 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');
.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));
.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();
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);
.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 == )
text.();
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);
}
}
B. 用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);
}
}
C. 请问一下用记事本怎么编辑java程序win7系统 jdk也装好了
步骤如下:
1、新建一个“文本文档”然后打开。
2、写好代码保存,然后把后缀名由txt改为java。
3、按windows图标键+r打开系统的“运行”,输入“cmd”,打开系统“命令提示符”。
4、使用
cd
命令定位到之前写好的代码的文件路径。
5、使用
“javac
文件名.java”命令编译。
6、如果没有错误,使用“java
文件名”命令运行即可。
D. 使用记事本编写JAVA程序,并运行输出结果,具体的实现步骤是什么
1、首先在电脑中新建一个记事本,将记事本的后缀改为“.java”,如下图所示。
E. 如何使用记事本编写java程序
很简单的啊,在记事本里写上相应的java代码,写好保存后将相应的文件名后缀改为****.java,然后用CMD去编译一下就可以了。
具体流程是这样的:
然后就可以去CMD那里编译和运行了。
在华信智原的JAVA课堂里面第一节课就会讲到这个基础的知识。