導航:首頁 > 編程語言 > java記事本的寫法

java記事本的寫法

發布時間:2024-06-20 09:02:03

A. 如何用java程序編寫一個記事本

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課堂裡面第一節課就會講到這個基礎的知識。

閱讀全文

與java記事本的寫法相關的資料

熱點內容
微信上面的圖片看不到 瀏覽:289
基層財務資料庫怎麼建立 瀏覽:262
加密狗檢測工具下載 瀏覽:889
智慧燈app怎麼使用 瀏覽:617
tcl手機升級程序 瀏覽:188
跳出了icloud密碼重置 瀏覽:110
哪裡能查信用卡數據 瀏覽:191
結婚請柬哪個app靠譜 瀏覽:932
qq無法閱讀pdf文件 瀏覽:284
ai導出pdf文件有輪廓線 瀏覽:819
刷機時數據線插台式電腦哪裡 瀏覽:432
手機qq怎樣隱身登陸 瀏覽:339
cdelete導致程序崩潰 瀏覽:783
小米4手機設置鈴聲在哪個文件夾里 瀏覽:184
linuxnet論壇源碼下載 瀏覽:241
手機修改文件後綴名 瀏覽:32
jsvideofullscreen 瀏覽:120
夢幻模擬戰1修改代碼 瀏覽:928
cad有nc編程功能嗎怎麼用 瀏覽:573
js調用外部js 瀏覽:273

友情鏈接