网站首页 > 文章中心 > 其它

java简易记事本代码

作者:小编 更新时间:2023-09-19 08:02:22 浏览量:443人看过

记事本java代码

//继承的类换一下,应该用swing包里面

class?Jsben?extends?Frame?{?--?class?Jsben?extends?JFrame?{

frame的方法setMenuBar(bar);和setJMenuBar(bar);这个不太一样,功能类似

java记事本源代码

给你个做好了的Java的源程序的记事本,自己看看就行了的,不怎么难的---

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.*;

public class MyNotepad implements ActionListener{

private JFrame frame=new JFrame("新记事本");

private JTextArea jta=new JTextArea();

private String result="";

private boolean flag=true;

private File f;

private JButton jb=new JButton("开始");

private JButton jbt=new JButton("替换为");

private JButton jba=new JButton("全部替换");

private Icon ic=new ImageIcon("D:\\java课堂笔记\\GUI\\11.gif");

private String value;

private int start=0;

private JFrame jf=new JFrame("查找");

private JFrame jfc=new JFrame("替换");

@Override

public void actionPerformed(ActionEvent e) {

String comm=e.getActionCommand();

if("新建".equals(comm)){

if(!(frame.getTitle().equals("新记事本"))){

if(!flag){

write();

newNew();

}else{

JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");

int returnVal = jfc.showDialog(null,"保存为");

if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性

f=jfc.getSelectedFile();

flag=false;

}

}else if(!(jta.getText().isEmpty())){

}else if("打开".equals(comm)){

jfc.setDialogType(JFileChooser.OPEN_DIALOG);

int returnVal = jfc.showOpenDialog(null);

frame.setTitle(f.getName());

result=read();

value=result;

jta.setText(result);

}else if("保存".equals(comm)){

if(flag){

}else if("另存".equals(comm)){

int returnVal = jfc.showDialog(null,"另存");

}else if("退出".equals(comm)){

System.exit(0);

}else if("撤销".equals(comm)){

jta.setText(value);

}else if("剪切".equals(comm)){

value=jta.getText();

jta.cut();

}else if("复制".equals(comm)){

jta.copy();

}else if("粘贴".equals(comm)){

jta.paste();

}else if("删除".equals(comm)){

jta.replaceSelection(null);

}else if("全选".equals(comm)){

jta.selectAll();

}else if("查找".equals(comm)){

jf.add(jtf,BorderLayout.CENTER);

jf.add(jb,BorderLayout.SOUTH);

jf.pack();

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}else if("替换".equals(comm)){

JLabel jl1=new JLabel("查找内容:");

jfc.setLayout(gl);

jfc.add(jl1);

jfc.add(jtf);

jfc.add(jb);

jfc.add(jt);

jfc.add(jbt);

jfc.add(jba);

jfc.pack();

jfc.setVisible(true);

jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}else if("版本".equals(comm)){

JDialog jd=new JDialog(frame,"关于对话框");

JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");

jd.add(l,BorderLayout.CENTER);

jd.setVisible(true);

//jd.pack();

jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

}else if("开始".equals(comm)||"下一个".equals(comm)){

String temp=jtf.getText();

int s=value.indexOf(temp,start);

if(value.indexOf(temp,start)!=-1){

jta.setSelectionStart(s);

jta.setSelectionEnd(s◆temp.length());

jta.setSelectedTextColor(Color.GREEN);

start=s◆1;

jb.setText("下一个");

//value=value.substring(s◆temp.length());//不能截取字串

}else {

JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);

jf.dispose();

}else if("替换为".equals(comm)){

jta.replaceSelection(jt.getText());

}else if("全部替换".equals(comm)){

String temp=jta.getText();

temp=temp.replaceAll(jtf.getText(), jt.getText());

jta.setText(temp);

public String read(){

String temp="";

try {

FileInputStream fis = new FileInputStream(f.getAbsolutePath());

while(true){

int num=fis.read(b);

if(num==-1)break;

temp=temp◆new String(b,0,num);

fis.close();

} catch (Exception e1) {

e1.printStackTrace();

return temp;

public void write(){

FileOutputStream fos=new FileOutputStream(f);

fos.write(jta.getText().getBytes());

fos.close();

} catch (Exception e) {

e.printStackTrace();

public void newNew(){

frame.dispose();

new MyNotepad();

flag=true;

public MyNotepad(){

JMenuBar jmb=new JMenuBar();

String[][] menuItemLab={{"新建","打开","保存","另存","退出"},

{"撤销","剪切","复制","粘贴","删除","全选","查找","替换"},

{"版本"}};

for(int i=0;imenuLab.length;i◆◆){

JMenu menu=new JMenu(menuLab[i]);

jmb.add(menu);

for(int j=0;jmenuItemLab[i].length;j◆◆){

JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);

menu.add(jmi);

jmi.addActionListener(this);

frame.setJMenuBar(jmb);

jta.setLineWrap(true);//自动换行

JScrollPane jsp=new JScrollPane(jta);//滚动窗口面板

frame.add(jsp);

jb.addActionListener(this);

jbt.addActionListener(this);

jba.addActionListener(this);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public static void main(String[] args) {

用Java语言编写一个记事本程序

import java.io.FileReader;

import java.io.FileWriter;

import javax.swing.JFileChooser; import java.awt.*;

import javax.swing.JFileChooser;

public class TextEditor extends Frame implements ActionListener{

MenuBar mainmenubar=new MenuBar();

Menu file;

MenuItem nw;

MenuItem op;

MenuItem cl;

MenuItem sf;

MenuItem ex;

TextArea tx;

public TextEditor(String title){

super(title);

CloseHandler handler= new CloseHandler();

this.addWindowListener(handler);

setLocationRelativeTo(null);

menuinit();

tx=new TextArea();

this.add(tx);

setVisible(true);

void menuinit(){

mainmenubar =new MenuBar();

file=new Menu("File");

nw=new MenuItem("Great");

op=new MenuItem("Open");

cl=new MenuItem("Close");

sf=new MenuItem("save");

ex=new MenuItem("Exit");

file.add(nw);

file.add(op);

file.add(cl);

file.add(sf);

file.add(ex);

mainmenubar.add(file);

setMenuBar(mainmenubar);

nw.addActionListener(this);

op.addActionListener(this);

cl.addActionListener(this);

sf.addActionListener(this);

ex.addActionListener(this);

public void actionPerformed(ActionEvent e){

Object ob=e.getSource();

JFileChooser f =new JFileChooser();

if((ob==nw)||(ob==ex)){

tx.setText("write word in here");

}else if(ob==op){

f.showOpenDialog(this);

try{

StringBuffer s=new StringBuffer();

FileReader in=new FileReader(f.getSelectedFile());

int b=in.read();

if(b==-1)

break;

s.append((char)b);

String myString=s.toString();

char[] mychar=myString.toCharArray();

String unkeyFile =FileEncry(mychar);

tx.setText(unkeyFile.toString());

in.close();

}catch(Exception ee){

}else if(ob==sf){

f.showSaveDialog(this);

FileWriter out =new FileWriter(f.getSelectedFile());

String mystr=tx.getText();

char[] strchar=mystr.toCharArray();

String keyFile =FileEncry(strchar);

out.write(keyFile);

out.close();

}catch (Exception ee){

}else if(ob==ex)

public static void main(String[] args){

new TextEditor("Simple TextEdit");

public String FileEncry(char[] encry){

StringBuffer str=new StringBuffer();

char[] encry1 =new char[encry.length];

for(int i=0; iencry.length;i◆◆){

encry1[i]=(char) (encry[i]^'F');

str.append(encry1[i]);

return str.toString();

class CloseHandler extends WindowAdapter{

public void windowClosing(WindowEvent e){

java简单记事本源代码 带解释

import javax.swing.border.*;

class test implements ActionListener

{

JFrame frame;

JTextArea ta;

JMenuBar mb;

ButtonGroup bg;

int a=0;

char c1;

int i=0;

public static void main(String[] args)

test that=new test();

that.go();

public void go()

frame=new JFrame("计算器");

Container cp= frame.getContentPane();

cp.setLayout(new FlowLayout());

mb=new JMenuBar();

bg=new ButtonGroup();

p1.setBackground(Color.yellow);

cp.setBackground(Color.CYAN);

frame.setJMenuBar(mb);

ta.setEditable(false);

p1.add(ta);

Border etched=BorderFactory.createEtchedBorder();

Border border=BorderFactory.createTitledBorder(etched,"计算类型");

b1.addActionListener(this);

b10.addActionListener(this);

b11.setActionCommand("*");

b11.addActionListener(this);

rb1.setActionCommand("kxx");

rb1.addActionListener(this);

public void actionPerformed(ActionEvent e) //throws Exception

if (e.getActionCommand()=="bzx")

if (e.getActionCommand()=="kxx")

if (e.getActionCommand()=="1")

ta.append("1");

if (e.getActionCommand()=="0")

ta.append("0");

if (e.getActionCommand()=="◆")

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=1;

if (e.getActionCommand()=="-")

if (e.getActionCommand()=="*")

if (e.getActionCommand()=="/")

if (e.getActionCommand()=="=")

if(i==1)

ta.setText("ERROR");

else

if (e.getActionCommand()=="C")

/*if (e.getActionCommand()=="Backspace")

//ta.cut(ta.select(a-1,a));

*/

if (e.getActionCommand()=="1/x")

if (e.getActionCommand()==".")

ta.append(".");

if (e.getActionCommand()=="◆/-")

if (e.getActionCommand()=="x^y")

if (e.getActionCommand()=="√x")

if (e.getActionCommand()=="x!")

if (d10)

ta.setText( "error");

else if (d1==0)

ta.setText( "0.0");

else {

for (int k=1;k=d1 ;k◆◆ )

if (e.getActionCommand()=="sin")

用Java编写简易记事本源代码

Java记事本源代码

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.DataFlavor;

import java.awt.datatransfer.Transferable;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import java.awt.event.InputEvent;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

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.JPopupMenu;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.KeyStroke;

import javax.swing.filechooser.FileFilter;

public class Demo extends JFrame {

private static final long serialVersionUID = 1L; //Eclipse自动生成序列号

String name = "无标题";

JPanel menuPanel = new JPanel();

JMenuBar mnbMain = new JMenuBar();

JMenu mnServer = new JMenu("文件(F)");

JMenuItem[] mniServers = new JMenuItem[]{

new JMenuItem("新建(N)"),

new JMenuItem("保存(S)"),

new JMenuItem("打开(O)"),

new JMenuItem("退出(X)"),

};

menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

mnbMain.add(mnServer);

menuPanel.add(mnbMain);

for (int i = 0; i mniServers.length; i◆◆) {

mnServer.add(mniServers[i]);

mniServers[0].addActionListener(new ActionListener() { //定义"新建"组件操作

public void actionPerformed(ActionEvent arg0) {

});

mniServers[1].addActionListener(new ActionListener() { //定义"保存"组件操作

chooseToSave();

chooseToOpen();

text.addFocusListener(new FocusListener() {

public void focusLost(FocusEvent e) {

// TODO Auto-generated method stub

public void focusGained(FocusEvent e) {

public Demo(int x,int y) {

this.setTitle( name ◆" - 记事本");

this.setLayout(new BorderLayout());

this.add(menuPanel, BorderLayout.NORTH);

this.add(jsp);

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

this.setVisible(true);

public Demo() {

protected void chooseToSave() {

File file = chooseFile();

if(null==file)return;

if(file.exists()){

int cho = JOptionPane.showConfirmDialog(this, "文件已存在,是否覆盖?");

System.out.println(cho);

if(cho==JOptionPane.OK_OPTION)save(file);

else return;

else save(file);

private void save(File file) {

name = file.getName();

write(text.getText(),file.getPath());

protected void chooseToOpen() {

if(null==file||!file.exists())return;

Demo.this.setTitle( name ◆" - 记事本");

read(text,file);

/*********************************************MAIN**************************************************/

new Demo();

private File chooseFile(){

JFileChooser chooser = new JFileChooser(); //构建文件选择器

chooser.setFileFilter(new FileFilter() {

public String getDescription() {

return "文本文件";

public boolean accept(File f) {

String name = f.getName().toLowerCase();

return f.isDirectory() || name.endsWith(".txt")

||name.endsWith(".c") || name.endsWith(".java")

||name.endsWith(".cpp"); //可识别文件

int result = chooser.showDialog(null, "确定");

if (result==JFileChooser.APPROVE_OPTION) {

File file = chooser.getSelectedFile();

System.out.println(file.getAbsolutePath());

} else {

System.out.println("未选择文件");

return chooser.getSelectedFile();

public static void read(JTextArea text,File file){ //定义读取文件操作

FileReader fr;

fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

String string = null;

while((string = br.readLine()) != null){

text.append(string◆"\n");

br.close();

fr.close();

} catch (FileNotFoundException e) {

} catch (IOException e) {

public static void write(String txt,String fileName){

FileWriter fw;

fw = new FileWriter(fileName);

BufferedWriter bw = new BufferedWriter(fw);

bw.write(txt);

bw.flush();

bw.close();

fw.close();

class TextAreaMenu extends JTextArea implements MouseListener {

private JPopupMenu pop = null; // 弹出菜单

private JMenuItem selectAll = null,copy = null, paste = null, cut = null, cancel=null; // 功能菜单

public TextAreaMenu() {

super();

init();

private void init() {

this.addMouseListener(this);

this.setSelectedTextColor(Color.red);

pop = new JPopupMenu();

pop.add(selectAll = new JMenuItem("全选"));

pop.add(copy = new JMenuItem("复制"));

pop.add(paste = new JMenuItem("粘贴"));

pop.add(cut = new JMenuItem("剪切"));

pop.add(cancel = new JMenuItem("撤销"));

selectAll.setAccelerator(KeyStroke.getKeyStroke('A', InputEvent.CTRL_MASK));

copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));

paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));

cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));

cancel.setAccelerator(KeyStroke.getKeyStroke('Z', InputEvent.CTRL_MASK));

copy.addActionListener(new ActionListener() {

action(e);

paste.addActionListener(new ActionListener() {

cut.addActionListener(new ActionListener() {

this.add(pop);

/**

* 菜单动作

* @param e

public void action(ActionEvent e) {

String str = e.getActionCommand();

if (str.equals(selectAll.getText())) { // 全选

this.selectAll();

else if (str.equals(copy.getText())) { // 复制

this.copy();

} else if (str.equals(paste.getText())) { // 粘贴

this.paste();

} else if (str.equals(cut.getText())) { // 剪切

this.cut();

else if (str.equals(cancel.getText())) { //撤销

public JPopupMenu getPop() {

return pop;

public void setPop(JPopupMenu pop) {

this.pop = pop;

* 剪切板中是否有文本数据可供粘贴

*

* @return true为有文本数据

public boolean isClipboardString() {

boolean b = false;

Clipboard clipboard = this.getToolkit().getSystemClipboard();

Transferable content = clipboard.getContents(this);

if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {

b = true;

return b;

* 文本组件中是否具备复制的条件

* @return true为具备

public boolean isCanCopy() {

int start = this.getSelectionStart();

int end = this.getSelectionEnd();

if (start != end)

public void mouseClicked(MouseEvent e) {

public void mouseEntered(MouseEvent e) {

public void mouseExited(MouseEvent e) {

public void mousePressed(MouseEvent e) {

copy.setEnabled(isCanCopy());

paste.setEnabled(isClipboardString());

cut.setEnabled(isCanCopy());

pop.show(this, e.getX(), e.getY());

public void mouseReleased(MouseEvent e) {

以上就是土嘎嘎小编为大家整理的java简易记事本代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章