导航:首页 > 编程语言 > 用java编写的记事本

用java编写的记事本

发布时间:2022-04-12 15:52:06

㈠ 用java编个简单的记事本程序

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;

class Editor extends Frame implements ActionListener,ItemListener
{
// Date date=new Date();
int n1;String str="";int k=0;String st1,st2;
JTextArea ta=new JTextArea(50,50);
Dialog dialog,dialog1;
Choice ce1,ce2,ce3;
Button btn1,btn2,btn3;Panel p1=new Panel();
Panel p2=new Panel();Panel p3=new Panel();Panel p4=new Panel();
public Editor (String s)
{
super ("记事本编辑板");
ImageIcon YouImg = new ImageIcon("iconimage.jpg");
setIconImage(YouImg.getImage());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}

});
setBounds(50,35,700,500);

MenuBar mb=new MenuBar();
setMenuBar(mb);
Menu file=new Menu("文件(F)");Menu edit=new Menu("编辑(E)");Menu help=new Menu("帮助(H)");
Menu pattern=new Menu("格式(P)");Menu time=new Menu("时间(T)");
MenuItem exit=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_Q));
MenuItem tdate=new MenuItem("日期/时间",new MenuShortcut(KeyEvent.VK_D));
MenuItem renew=new MenuItem("新建", new MenuShortcut(KeyEvent.VK_N));
MenuItem save=new MenuItem("保存", new MenuShortcut(KeyEvent.VK_S));
MenuItem open=new MenuItem("打开",new MenuShortcut(KeyEvent.VK_O));
MenuItem =new MenuItem("复制",new MenuShortcut(KeyEvent.VK_C));
MenuItem paste=new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_V));
MenuItem cut=new MenuItem("剪切",new MenuShortcut(KeyEvent.VK_X));
MenuItem helptopic=new MenuItem("主题",new MenuShortcut(KeyEvent.VK_T));
helptopic.addActionListener(this);
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 WindowAdapter(){
public void windowClosing(WindowEvent e){
dialog1.setVisible(false);}
});
//dialog.setBackground(Color.WHITE);
dialog.setLayout(null);dialog.setBounds(170,150,450,300);
Label L1=new Label("字体(F)");Label L2=new Label("字形(Y)");
Label L3=new Label("大小(S)");TextArea ta1=new TextArea(5,5);
ta1=new TextArea("至于记事本的主题嘛,因为本人的水平有限, 所有不能为大家作过多的指导,望大家见谅!"); //在这里我想加一个文本框用来放帮助信息。

ce1=new Choice();ce2=new Choice();
ce3=new Choice();
btn1=new Button("确定");btn2=new Button("取消");btn1.addActionListener(this);btn2.addActionListener(this);
btn3=new Button("颜色");
btn3.addActionListener(this);

ce1.add("华文行楷");ce1.add("华文中宋");ce1.add("华文新魏");ce1.add("华文细黑");ce1.add("宋体");ce1.add("方正姚体");
ce1.add("幼圆");ce1.add("隶书");ce1.add("楷体-GB2312");ce1.add("华文行楷");ce1.add("华文彩云");ce1.add("仿宋-GB2312");
ce2.add("粗体");ce2.add("斜体");ce2.add("常规");
for(int n1=0;n1<=100;n1++)
{ce3.add(""+n1);}
dialog.add(p1);p1.setBounds(5,30,440,35);p1.setLayout( null);//p1.setBackground(Color.red);
dialog.add(p2);p2.setBounds(5,65,440,27);dialog.add(p3);p3.setBounds(5,90,440,40);//p4.setBounds(5,120,440,138);
p1.setLayout( null);
p1.add(L1);L1.setBounds(5,15,50,25);p1.add(L2);L2.setBounds(150,15,50,25);
p1.add(L3);L3.setBounds(250,15,50,25);p2.setLayout( null);//p2.setBackground(Color.yellow);
p2.add(ce1);ce1.setBounds(5,0,130,25);p2.add(ce2);ce2.setBounds(160,0,90,25);p2.add(ce3);ce3.setBounds(260,0,75,25);
p2.add(btn1);btn1.setBounds(360,0,75,25);
p3.setLayout( null);p3.add(btn2);btn2.setBounds(360,10,75,25);p3.add(btn3);btn3.setBounds(160,10,75,25);
dialog.add(p4);p4.setBackground(Color.yellow);p4.setBounds(5,120,440,138);
dialog1.add(ta1);
//设置字体的下拉情况
MenuItem Font=new MenuItem("字体");//给字体加监听器
Font.addActionListener(this);
MenuItem Replace=new MenuItem("替换"); MenuItem Seek=new MenuItem("查找");
//首要添加组件的情况
mb.add(file);mb.add(edit);mb.add(pattern);mb.add(time);mb.add(help);
file.add(renew);file.addSeparator();file.add(open);time.add(tdate);
file.addSeparator(); file.add(exit);file.addSeparator();file.add(save);
edit.add(); edit.addSeparator();edit.add(paste);edit.addSeparator();edit.add(cut);
pattern.add(Font); pattern.add(Replace);
pattern.add(Seek);help.add(helptopic);
//给必要的组件加上监听器
renew.addActionListener( this);tdate.addActionListener(this);
open.addActionListener(this); save.addActionListener(this);exit.addActionListener(this);
cut.addActionListener(this);.addActionListener(this);paste.addActionListener(this);
ce1.addItemListener(this);ce2.addItemListener(this);ce3.addItemListener(this);
//添加必要的组件
add(ta,"Center");ta.setBackground(new Color(249,255,234));
setSize(700,500);
setVisible(true);
this.validate() ;

}

public void itemStateChanged(ItemEvent e) {
if (ce2.getSelectedItem().equals("粗体"))
{ k=1;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}
if (ce2.getSelectedItem().equals("斜体"))
{ k=2;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}
if (ce2.getSelectedItem().equals("常规"))
{ k=3;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}
}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("新建"))
{System.out.println("new ");
ta.setText("");}
if(e.getActionCommand().equals("退出"))
{ System.exit(0);}
try
{
if(e.getActionCommand().equals("打开"))
openText();
if(e.getActionCommand().equals("保存"))
saveText();
}catch(IOException e1){}
if(e.getActionCommand().equals("复制"))
{ str=ta.getSelectedText();}
if(e.getActionCommand().equals("粘贴"))
{ ta.insert(str,ta.getCaretPosition());}
if(e.getActionCommand().equals("剪切"))
{str=ta.getSelectedText();ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());

}
if(e.getActionCommand().equals("字体"))
{dialog.setVisible(true);}
if(e.getActionCommand().equals("主题"))
{dialog1.setVisible(true);}
if(e.getActionCommand().equals("颜色"))
{
Color clr=JColorChooser.showDialog(this,"颜色对话框",null);
ta.setForeground(clr);
}

if(e.getSource()==btn2)
dialog.setVisible(false);
if(e.getSource()==btn1)
{
if(k==1)
{ ta.setFont(new Font(st1,Font.BOLD,Integer.parseInt(st2)));}
if(k==2)
{ ta.setFont(new Font(st1,Font.ITALIC,Integer.parseInt(st2)));}
if(k==3)
{ ta.setFont(new Font(st1,Font.PLAIN,Integer.parseInt(st2)));}
dialog.setVisible(false);
}

//if (e.getActionCommand().equals("日期/时间"))
//ta.setText(ta.getText()+""+date);
if(e.getActionCommand().equals("日期/时间"))
{
Calendar c1 =Calendar.getInstance();
int y = c1.get(Calendar.YEAR);
int m = c1.get(Calendar.MONTH);
int d = c1.get(Calendar.DATE);
int h = c1.get(Calendar.HOUR);
int m1 = c1.get(Calendar.MINUTE);
int m2 = m+1;
ta.setText(ta.getText()+""+(y+"年"+m2+"月"+d+"日"+h+":"+m1));
}

}

public void openText() throws IOException
{
FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
fd.setVisible(true);FileInputStream fis=new FileInputStream(fd.getDirectory()+fd.getFile());
ta.setText("");
int n=0;
while((n=fis.read())!=-1)
ta.append(""+(char)n);
fis.close();
}
public void saveText() throws IOException
{
FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.SAVE);
fd.setVisible(true);
FileOutputStream out=new FileOutputStream(fd.getDirectory()+fd.getFile()+".txt");
String str=ta.getText();

String str1=ta.getText();
for(int n=0;n<str.length();n++)
out.write((byte)str.charAt(n));
out.close();
}

public static void main(String[] args)
{
Editor f=new Editor(" 记事本程序");
//添加时间代码
Date date=new Date();
System.out.print(date);
/*Calendar now=Calendar.getInstance();
int year=now.get(Calendar.YEAR);
int month=now.get(Calendar.MONTH)+1;
int day=now.get(Calendar.DATE);
System.out.print(year+"年"+month+"月"+day+"日");

int week=now.get(Calendar.DAY_OF_WEEK);
String str="日一二三四五六";//星期1-7
int i=2*(week-1);
System.out.println("星期"+str.substring(i,i+2));//对应中文的下标*/

}

class dialog extends Dialog
{

public dialog()
{
super(dialog, "字体设置");

class Mycanvas extends Canvas
{
Toolkit tk;
Image img;
Mycanvas()
{
setSize(440,138);
tk=getToolkit();
img=tk.getImage("photo.jpg");
}
public void paint(Graphics g)
{
g.drawImage(img,5,120,440,138,this);
}
}
}
}
}

㈡ java编写简单记事本

方法/步骤
下载JDK,并安装成功,CMD测试Java环境安装成功。出现如下提示则Java环境安装成功

打开mycelipse进入主界面,点击左上角file--new---java project,新建一个Java项目,项目名字任意取,这里我取名为note--然后点击完成,创建Java项目

然后右键项目下的src--new---package,新建一个包,包名我定为notetest,然后点击完成

然后右键我们建立好的包notetest--new--class,包名为Note,点击完成

首先我们想导入工程所需要的包,以便编写程序时进行调用
package notepad;
import java.awt.*;
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.BufferedWriter;

首先定义好编写程序时变量,我们设为成员变量方便以后调用
public class Notepad{
//屏幕分辨率
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
//窗体
private Frame frm ;

//显示内容多行文本
private TextArea notetext;
//记事本菜单
private MenuBar mb;
//菜单
private Menu m ;
//菜单项
private MenuItem open,save;

为程序创建组件--Frame--菜单栏目--文本框TextArea
public Notepad(){init();}

private void init(){
frm = new Frame("模拟记事本");

//设置菜单
mb = new MenuBar();
m = new Menu("文件");

//显示文件的地方
notetext = new TextArea();
notetext.setBounds(30, 50, 750, 500);
frm.add(notetext);
myEvent();

为控件和按钮添加监听事件
private void myEvent() {
//打开菜单的事件处理
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//判断路径和文件名是否为空 如果为空结束程序
if(dir == null || name == null)

//如果不为空讲路径名和文件名封装到File对象里

file = new File(dir,name);

//使用字符缓冲读取文件

BufferedReader bfr = null;

try {
bfr = new BufferedReader(new FileReader(file));
});
save.addActionListener(new ActionListener(){

//获取路径和文件名

String dir =fd.getDirectory();
String name = fd.getFile();
//如果不为空讲路径名和文件名封装到File对象里
file = new File(dir,name);
}

BufferedWriter bfr= null;
}

});
}
}

然后创建主方法,开始测试程序,新建一个NoteDemo类
package notepad;

public class NoteDemo {
public static void main(String[] args) {
new Notepad();
}

}

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

import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFileChooser; import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.io.FileWriter;
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);
setSize(400,400);
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());
while(true){
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);
try{
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)
System.exit(0);
}
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; i<encry.length;i++){
encry1[i]=(char) (encry[i]^'F');
str.append(encry1[i]);
}
return str.toString();
}
}
class CloseHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}

㈣ 如何用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);
}
}

㈤ 电脑怎么用记事本写Java程序并运行

1. 新建一个记事本,后缀名是  .java  ;然后在里面写一段java的代码,如图:

2.把写好的java文件丢进D盘,就是第一步给出的那个class文件;
3. 打开dos界面 开始-〉运行-〉cmd-〉 这个是命令行模式,选择D盘,如图:

4.选择D盘之后,在命令提示符中输入“javac 文件名.java”,我的文件名为java,所以输入javac java.java然后回车,等待编译。这时候你就会发现它提示说javac不是内部文件。接下来就是java环境变量设置的问题了;
5.下载一个jdk执行默认安装。例如我下的jdk版本是: jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe;
6.配置环境:右击我的电脑点属性,进去点高级就能看到环境变量。点进去就能设置你的系统变量了,如图:

6-1。变量名:classpath    变量值:(.;C:\Program Files\Java\jdk1.6.0_10\lib;)括号里面都是 (主要我们在编译运行程序的时候通过classpath可以帮助我们找到一些需要的系统类,“.”号是表示当前路径;“;”号是用来隔开多个变量值,如果你的系统里面已经有classpath环 境变量,就不用新建了,直接加“;”号,在后面加新值);如图所示

㈥ 使用记事本编写JAVA程序,并运行输出结果,具体的实现步骤是什么

1、首先在电脑中新建一个记事本,将记事本的后缀改为“.java”,如下图所示。

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

importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.InputEvent;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjavax.swing.BorderFactory;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JOptionPane;importjavax.swing.JPopupMenu;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.KeyStroke;importjavax.swing.ScrollPaneConstants;importjavax.swing.SwingConstants;{privateJMenuItemmenuOpen;privateJMenuItemmenuSave;privateJMenuItemmenuSaveAs;privateJMenuItemmenuClose;privateJMenueditMenu;privateJMenuItemmenuCut;privateJMenuItemmenuCopy;privateJMenuItemmenuPaste;privateJMenuItemmenuAbout;privateJTextAreatextArea;privateJLabelstateBar;;privateJPopupMenupopUpMenu;publicJNotePadUI(){super("新建文本文件");setUpUIComponent();setUpEventListener();setVisible(true);}privatevoidsetUpUIComponent(){setSize(640,480);//菜单栏JMenuBarmenuBar=newJMenuBar();//设置“文件”菜单JMenufileMenu=newJMenu("文件");menuOpen=newJMenuItem("打开");//快捷键设置menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));menuSave=newJMenuItem("保存");menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));menuSaveAs=newJMenuItem("另存为");menuClose=newJMenuItem("关闭");menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));fileMenu.add(menuOpen);fileMenu.addSeparator();//分隔线fileMenu.add(menuSave);fileMenu.add(menuSaveAs);fileMenu.addSeparator();//分隔线fileMenu.add(menuClose);//设置“编辑”菜单JMenueditMenu=newJMenu("编辑");menuCut=newJMenuItem("剪切");menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));menuCopy=newJMenuItem("复制");menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));menuPaste=newJMenuItem("粘贴");menuPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));editMenu.add(menuCut);editMenu.add(menuCopy);editMenu.add(menuPaste);//设置“关于”菜单JMenuaboutMenu=newJMenu("关于");menuAbout=newJMenuItem("关于JNotePad");aboutMenu.add(menuAbout);menuBar.add(fileMenu);menuBar.add(editMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar);//文字编辑区域textArea=newJTextArea();textArea.setFont(newFont("宋体",Font.PLAIN,16));textArea.setLineWrap(true);JScrollPanepanel=newJScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);ContainercontentPane=getContentPane();contentPane.add(panel,BorderLayout.CENTER);//状态栏stateBar=newJLabel("未修改");stateBar.setHorizontalAlignment(SwingConstants.LEFT);stateBar.setBorder(BorderFactory.createEtchedBorder());contentPane.add(stateBar,BorderLayout.SOUTH);popUpMenu=editMenu.getPopupMenu();fileChooser=newJFileChooser();}privatevoidsetUpEventListener(){//按下窗口关闭钮事件处理addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){closeFile();}});//菜单-打开menuOpen.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){openFile();}});//菜单-保存menuSave.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFile();}});//菜单-另存为menuSaveAs.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFileAs();}});//菜单-关闭文件menuClose.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){closeFile();}});//菜单-剪切menuCut.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){cut();}});//菜单-复制menuCopy.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){();}});//菜单-粘贴menuPaste.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){paste();}});//菜单-关于menuAbout.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//显示对话框JOptionPane.showOptionDialog(null,"程序名称:\nJNotePad\n"+"程序设计:\n\n"+"简介:\n一个简单的文字编辑器\n"+"可作为验收Java的实现对象\n"+"欢迎网友下载研究交流\n\n"+"/","关于JNotePad",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,null,null);}});//编辑区键盘事件textArea.addKeyListener(newKeyAdapter(){publicvoidkeyTyped(KeyEvente){processTextArea();}});//编辑区鼠标事件textArea.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEvente){if(e.getButton()==MouseEvent.BUTTON3)popUpMenu.show(editMenu,e.getX(),e.getY());}publicvoidmouseClicked(MouseEvente){if(e.getButton()==MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});}privatevoidopenFile(){if(isCurrentFileSaved()){//文件是否为保存状态open();//打开}else{//显示对话框intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){//确认文件保存caseJOptionPane.YES_OPTION:saveFile();//保存文件break;//放弃文件保存caseJOptionPane.NO_OPTION:open();break;}}}(){if(stateBar.getText().equals("未修改")){returnfalse;}else{returntrue;}}privatevoidopen(){//fileChooser是JFileChooser的实例//显示文件选取的对话框intoption=fileChooser.showDialog(null,null);//使用者按下确认键if(option==JFileChooser.APPROVE_OPTION){try{//开启选取的文件BufferedReaderbuf=newBufferedReader(newFileReader(fileChooser.getSelectedFile()));//设定文件标题setTitle(fileChooser.getSelectedFile().toString());//清除前一次文件textArea.setText("");//设定状态栏stateBar.setText("未修改");//取得系统相依的换行字符StringlineSeparator=System.getProperty("line.separator");//读取文件并附加至文字编辑区Stringtext;while((text=buf.readLine())!=null){textArea.append(text);textArea.append(lineSeparator);}buf.close();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"开启文件失败",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFile(){//从标题栏取得文件名称Filefile=newFile(getTitle());//若指定的文件不存在if(!file.exists()){//执行另存为saveFileAs();}else{try{//开启指定的文件BufferedWriterbuf=newBufferedWriter(newFileWriter(file));//将文字编辑区的文字写入文件buf.write(textArea.getText());buf.close();//设定状态栏为未修改stateBar.setText("未修改");}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"写入文件失败",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFileAs(){//显示文件对话框intoption=fileChooser.showSaveDialog(null);//如果确认选取文件if(option==JFileChooser.APPROVE_OPTION){//取得选择的文件Filefile=fileChooser.getSelectedFile();//在标题栏上设定文件名称setTitle(file.toString());try{//建立文件file.createNewFile();//进行文件保存saveFile();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"无法建立新文件",JOptionPane.ERROR_MESSAGE);}}}privatevoidcloseFile(){//是否已保存文件if(isCurrentFileSaved()){//释放窗口资源,而后关闭程序dispose();}else{intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){caseJOptionPane.YES_OPTION:saveFile();break;caseJOptionPane.NO_OPTION:dispose();}}}privatevoidcut(){textArea.cut();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoid(){textArea.();popUpMenu.setVisible(false);}privatevoidpaste(){textArea.paste();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidprocessTextArea(){stateBar.setText("已修改");}publicstaticvoidmain(String[]args){newJNotePadUI();}}

㈧ 如何用JAVA编写简单的记事本程序

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.datatransfer.*;public class NewEdit
{
public static void main(String args[])
{
MyFrame EditFrame=new MyFrame();
EditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EditFrame.setVisible(true);
}
}class MyFrame extends JFrame
{
public MyFrame()
{ Dimension screenSize=toolKit.getScreenSize();
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;

setSize(screenWidth/2,screenHeight/2);
setLocation(screenWidth/4,screenHeight/4);

Image img=toolKit.getImage("icon.gif");
setIconImage(img);
setTitle("MyEdit");

JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);

JMenu fileMenu=new JMenu("文件");
JMenu editMenu=new JMenu("编辑");
JMenu helpMenu=new JMenu("帮助");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);

JMenuItem newItem=new JMenuItem("新建");
newItem.addActionListener(new NewListener());

JMenuItem openItem=new JMenuItem("打开");
openItem.addActionListener(new OpenListener());

JMenuItem saveItem=new JMenuItem("保存");
saveItem.addActionListener(new SaveListener());

JMenuItem escItem=new JMenuItem("退出");
escItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
System.exit(0);
}
});

fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(escItem);

JMenuItem allItem=new JMenuItem("全选");
//JMenuItem Item=new JMenuItem("复制");
//CopyAction Action=new CopyAction();
//Item.addActionListener(Action);

JMenuItem cutItem=new JMenuItem("剪切");
//JMenuItem pasteItem=new JMenuItem("粘贴");
//Action pasteAction=new PasteAction();
//pasteItem.addActionListener(pasteAction);

editMenu.add(allItem);
allItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
textArea.selectAll();
}
});

CopyAction Action=new CopyAction("剪切");
editMenu.add(Action);

CutAction cutAction=new CutAction("复制");
editMenu.add(cutAction);

Action pasteAction=new PasteAction("粘贴");
editMenu.add(pasteAction);

popup=new JPopupMenu();
popup.add(Action);
popup.add(pasteAction);
popup.add(cutAction);

textArea = new JTextArea();
textArea.add(popup);

textArea.addMouseListener(new
MouseListener(){
public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3)
{
popup.show(textArea, e.getX(), e.getY());
}
} public void mouseReleased(MouseEvent e) {} });//可尝试用MouseAdapter,代码可能更简单
JScrollPane scroller = new JScrollPane(textArea);
add(scroller);
//OR--this.getContentPane().add(scroller)
//scroller.setComponentPopupMenu(popup);
//textArea.setInheritsPopupMenu(true);
//scroller.addMouseListener(new MouseAdapter(){});

}

public void writeFile(String fileName)
{
try
{
File file = new File(fileName);
FileWriter write = new FileWriter(file);
write.write(textArea.getText());
write.close();
}
catch(Exception e){
System.out.println("Error closing file!");
}
}

public void openFile(String fileName)
{
try
{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content,charsRead,size-charsRead);
readIn.close();
textArea.setText(new String(content,0,charsRead));
}
catch(IOException e)
{
System.out.println("Error opening file!");
}
}

private class NewListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
textArea.setText("");
}
}

private class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
{
openFile(fileName);
}
}
}

private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
saveFileDialog.setVisible(true);
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
if(fileName !=null)
{
writeFile(fileName);
}
}
}

private class CutAction extends AbstractAction
{
public CutAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);

clipboard.setContents(selection,null);
textArea.replaceRange("",textArea.getSelectionStart(),
textArea.getSelectionEnd());
}
}

private class CopyAction extends AbstractAction
{
public CopyAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection= new StringSelection(text);

clipboard.setContents(selection,null);
}
}

private class PasteAction extends AbstractAction
{
public PasteAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
Transferable contents = clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try
{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception ex){}
textArea.replaceRange(text,
textArea.getSelectionStart(),textArea.getSelectionEnd());
}
}

private JTextArea textArea;
private JPopupMenu popup;

private String fileName="";

private FileDialog openFileDialog=new FileDialog(this,"OpenFile",FileDialog.LOAD); private FileDialog saveFileDialog=new FileDialog(this,"SaveFile",FileDialog.SAVE);

Toolkit toolKit = Toolkit.getDefaultToolkit();
private Clipboard clipboard = toolKit.getSystemClipboard();}

㈨ 如何用java的GUI编写一个记事本

很久之前写过的,凑合着看吧,

package个人练习;

importjava.awt.FileDialog;
importjava.awt.Menu;
importjava.awt.MenuBar;
importjava.awt.MenuItem;
importjava.awt.TextArea;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.event.WindowListener;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
importjava.io.PrintWriter;
importjava.io.Writer;
importjava.sql.Savepoint;

importjavax.swing.JFrame;
/**
*创建一个类似记事本的程序
*该程序拥有自己的书写窗口
*可以自定义保存地址
*@authorAdministrator
*
*/
/*
*创建记事本类
*实现ActionListener接口
*重写它的actionPerformed方法
*
*/
{
//拥有自己的窗口
JFrameframe=newJFrame("记事本程序");
//菜单栏
MenuBarmenubar=newMenuBar();
//创建下拉菜单
Menuwenjian=newMenu("文件");
//创建保存按钮
MenuItemdakai=newMenuItem("打开文件");
MenuItembaochun=newMenuItem("保存");
MenuItemxinjian=newMenuItem("新建");
//创建可以显示文本的多行区域
TextAreawenben=newTextArea();
//显示一个对话窗口,用户选择一个文件,如果模式是SAVE,则寻找一个地方去写入一个文件
FileDialogfd;
Filefile=null;
/*
*创建jishiben的构造函数
*/

publicJiShiBen(){
frame.setMenuBar(menubar);//将菜单栏关联到窗口
menubar.add(wenjian);//将下拉菜单添加到菜单栏
wenjian.add(xinjian);
wenjian.add(dakai);//将保存按钮添加到下拉菜单
wenjian.add(baochun);

dakai.addActionListener(this);//由baochuan按钮接受处理操作事件
baochun.addActionListener(this);
xinjian.addActionListener(this);
frame.add(wenben);//将文本区域添加到窗口
//设置窗口的属性
frame.setSize(600,480);
frame.setLocation(200,200);
frame.setVisible(true);
/*
*
*用于接收窗口事件的侦听器接口。
*旨在处理窗口事件的类要么实现此接口(及其包含的所有方法),
*要么扩展抽象类WindowAdapter(仅重写所需的方法)。
*然后使用窗口的addWindowListener方法将从该类所创建
*的侦听器对象向该Window注册。
*当通过打开、关闭、激活或停用、
*图标化或取消图标化而改变了窗口状态时,
*将调用该侦听器对象中的相关方法,
*并将WindowEvent传递给该方法。
*windowClosing(WindowEvente)
用户试图从窗口的系统菜单中关闭窗口时调用。
*
*/

frame.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);//终止当前运行的java虚拟机
}
});
}

/*
*实现ActionListener接口中的方法
*(non-Javadoc)
*@seejava.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==baochun){
//publicObjectgetSource()最初发生Event的对象。返回,最初发生Event的对象。
fd=newFileDialog(frame,"保存文本文件",FileDialog.SAVE);
/*
*FileDialog(Dialogparent,Stringtitle,intmode)
创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。

staticintLOAD
此常量值指示文件对话框窗口的作用是查找要读取的文件。
staticintSAVE
此常量值指示文件对话框窗口的作用是查找要写入的文件。

*/
fd.setVisible(true);
//文件的存放位置是通过fd获取的位置,文件名是通过fd获取的文件名
file=newFile(fd.getDirectory(),fd.getFile()+".txt");
save(file);
}
if(e.getSource()==dakai){
fd=newFileDialog(frame,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
/*
*StringgetFile()
获取此文件对话框的选定文件。
StringgetDirectory()
获取此文件对话框的目录。
*/
file=newFile(fd.getDirectory()+fd.getFile());
System.out.println(fd.getFile());
System.out.println(fd.getDirectory());
open(file);
}
if(e.getSource()==xinjian){
wenben.setText("");
}
}

/*
*创建save方法
*/
publicvoidsave(Filefile){
try{
PrintWriterpw=newPrintWriter(file);
pw.write(wenben.getText());//wenben.getText()返回文本区域的文本
pw.close();
}catch(Exceptione){
}
}
//创建open方法
publicvoidopen(Filefile){
try{
FileInputStreamfis=newFileInputStream(file);
InputStreamReaderisr=newInputStreamReader(fis);
BufferedReaderbr=newBufferedReader(isr);
Stringline=null;
while((line=br.readLine())!=null){
wenben.append(line+" ");
}
br.close();

}catch(Exceptione){

}
}
publicstaticvoidmain(String[]args){
newJiShiBen();
}


}

㈩ 怎样用java编写一个记事本 ,具体步骤!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;

public class TextEditorFrame extends JFrame {
File file = null;
Color color = Color.red;

TextEditorFrame() {
initTextPane();
initAboutDialog();
initToolBar();
initMenu();
}

void initTextPane() {
getContentPane().add(new JScrollPane(text));
}

JTextPane text = new JTextPane(); // 这是用来做文本框的
JFileChooser filechooser = new JFileChooser(); // 文件选择框
JColorChooser colorchooser = new JColorChooser();//
JDialog about = new JDialog(this); // 关于对话框
JMenuBar menubar = new JMenuBar();// 菜单

JMenu[] menus = new JMenu[] { new JMenu("文件"), new JMenu("编辑"),
new JMenu("帮助") };

JMenuItem menuitems[][] = new JMenuItem[][] {
{ new JMenuItem("新建"), new JMenuItem("打开"), new JMenuItem("保存"), new JMenuItem("退出") },
{new JMenuItem("复制"), new JMenuItem("剪切"), new JMenuItem("粘贴"), new JMenuItem("颜色") }, { new JMenuItem("关于") } };

void initMenu() {

for (int i = 0; i < menus.length; i++) {
menubar.add(menus[i]);
for (int j = 0; j < menuitems[i].length; j++) {
menus[i].add(menuitems[i][j]);
menuitems[i][j].addActionListener(action);
}
}
this.setJMenuBar(menubar);
}

ActionListener action = new ActionListener() { // when here have not wrong:
public void actionPerformed(ActionEvent e) {
JMenuItem mi = (JMenuItem) e.getSource();
String id = mi.getText();
if (id.equals("新建")) {
text.setText("");
file = null;
} else if (id.equals("打开")) {
if (file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser
.showOpenDialog(TextEditorFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {

file = filechooser.getSelectedFile();
openFile();
}

} else if (id.equals("保存")) {
if (file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser
.showSaveDialog(TextEditorFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = filechooser.getSelectedFile();
saveFile();
}

} else if (id.equals("退出")) {
TextEditorFrame f = new TextEditorFrame();
int s = JOptionPane.showConfirmDialog(f, "你真的要结束吗", "结束程序",
JOptionPane.YES_NO_CANCEL_OPTION);
if (s == JOptionPane.YES_OPTION)
System.exit(0);
} else if (id.equals("剪切")) {
text.cut();
} else if (id.equals("复制")) {
text.();
} else if (id.equals("粘贴")) {
text.paste();
} else if (id.equals("color")) {
color = JColorChooser.showDialog(TextEditorFrame.this, "",
color);
text.setForeground(color);

} else if (id.equals("关于")) {
about.setSize(200, 150);
about.show();
}

}

};

void saveFile() {
try {
FileWriter fw = new FileWriter(file);
fw.write(text.getText());
fw.close();
} catch (Exception e) {
e.printStackTrace();
}

}

void openFile() {
try {
FileReader fr = new FileReader(file);
int len = (int) file.length();
char[] buffer = new char[len];
fr.read(buffer, 0, len);
fr.close();
text.setText(new String(buffer));
} catch (Exception e) {
e.printStackTrace();
}

}

void initAboutDialog() {
about.getContentPane().add(new JLabel("作者-覃树新:记事本Q1.0版本"));
about.setModal(true);
about.setSize(200, 100);
}

JToolBar toolbar = new JToolBar();// 我来加上工具条
JButton[] buttons = new JButton[] {
new JButton("", new ImageIcon("qin.jpg")),
new JButton("", new ImageIcon("shu.jpg")),
new JButton("", new ImageIcon("xin.jpg")) };

void initToolBar() {
for (int i = 0; i < buttons.length; i++)
toolbar.add(buttons[i]);
buttons[0].setToolTipText("复制");
buttons[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.();
}
});
buttons[1].setToolTipText("剪切");
buttons[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.cut();
}
});

buttons[2].setToolTipText("粘贴");
buttons[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.paste();
}
});

this.getContentPane().add(toolbar, BorderLayout.NORTH);
}

public static void main(String args[]) {

TextEditorFrame f = new TextEditorFrame();

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)

{
TextEditorFrame f = new TextEditorFrame();
int s = JOptionPane.showConfirmDialog(f, "你真的要结束吗", "结束程序",
JOptionPane.YES_NO_OPTION);
if (s == JOptionPane.YES_OPTION)
System.exit(0);
}
});

f.setTitle("简单的记事本");
f.setSize(800, 600);
f.setVisible(true);
}
}

以前看到的一个程序

阅读全文

与用java编写的记事本相关的资料

热点内容
cad2011怎么转换成pdf格式 浏览:962
传祺gs5安卓车机如何还原车机 浏览:898
单片机和编程器互相传输数据 浏览:88
app订单怎么取消 浏览:465
程序员用双显示器有什么作用 浏览:609
网约车算法杀熟 浏览:4
卡萨帝用的什么压缩机 浏览:153
350乘20算法 浏览:90
自助编程软件app 浏览:436
服务器如何看日活数 浏览:684
数控车床原理图及编程 浏览:287
java文件流下载 浏览:338
编程工作工资多少 浏览:439
专业安全文件夹 浏览:779
表格里的根号算法怎么打 浏览:195
javacorepdf 浏览:575
pdf转换word编辑 浏览:446
35岁程序员实习期恐慌 浏览:703
如何做一个系统u盘文件夹名字 浏览:970
如何确认哪个ip重启了服务器 浏览:132