资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java开发记事本代码 Java开发记事本

如何用JAVA程序编写一个记事本

import java.io.*;

成都创新互联公司科技有限公司专业互联网基础服务商,为您提供成都服务器托管高防服务器租用,成都IDC机房托管,成都主机托管等互联网服务。

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 copy = 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');

copy.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));

copy.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(copy);

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);

copy.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 == copy)

text.copy();

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.首先,先看看电脑是否设置为显示已知文件扩展名。

如果没有,要先设置一下。点击计算机,工具(T),文件夹选项(O),

查看,在高级设置一栏里找到隐藏已知文件扩展名,把前面的钩钩去掉。

2.点击鼠标右键,新建,文本文档,命名为:MyFirstJava.java

然后编写第一个程序代码,注意类名和文件名要一致,

还有划线部分首字母要大写。

3.写好程序之后,点击右上角的文件,另存为,出现下面的页面。

然后,要看你的jdk安装在哪(我的安装在D盘)。就可以把文件保存在D盘,还有文件名设置为MyFirstJava.java,保存类型设置为:所有文件。

]

4.保存好之后,关闭文件。点开时,在搜索程序和文件中输入cmd,

然后,Enter,Enter,就会出现下面的页面。

5.在控制台先输入d:(因为我的是在D盘,若是在C盘就输入c:,F盘就输入f:);

回车,再输入javac MyFirstJava.java(注意javac后空一格);

回车,在输入java MyFirstJava(java后面也有一个空格)。

这时,就会出来结果了!:)

6.运行结束都在D盘中(安装jdk的地方)

会出现一个MyFirstJave.Class 文件。

用java编写记事本的代码是什么

楼主应该是想编译记事本的写的java代码吧?

1.从sun.com上免费下载jdk5.0

2.安装jdk

3.配置环境变量,在"控制面版"的"系统"的"高级"标签,

在"path="键上添加你的jdk/bin路径;

4.添加"java_home"键,值等于jdk路径;

配置就ok了.

5.测试在cmd命令下输入"java -version"回车;

就会显示出你的jdk版本,否则就照以上步骤从来;

6.编译%--在你的"FileName.java"的文件夹里,(当然是在cmd命令下即dos模式)输入"javac FileName.java"回车,就完完成了编译.

例如:你的文件是Hello.java

就输入 javac Hello.java

7.运行文件当你成功编译后,输入"java FileName"就可以得到结果.

例如:java Hello

不推荐这样做。编程和编译其实用工具实现起来很容易。比如

netbeans,eclips==都是免费的强大编译软件!推荐使用!

如何使用记事本编写java程序

很简单的啊,在记事本里写上相应的java代码,写好保存后将相应的文件名后缀改为****.java,然后用CMD去编译一下就可以了。

具体流程是这样的:

然后就可以去CMD那里编译和运行了。

在华信智原的JAVA课堂里面第一节课就会讲到这个基础的知识。

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

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

class test implements ActionListener

{

JFrame frame;

JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31;

JTextArea ta;

JPanel p1,p2,p3,p4;

JMenuBar mb;

JMenu m1,m2,m3;

JMenuItem mt1,mt2,mt3,mt4,mt5,mt6,mt7;

JRadioButton rb1,rb2;

ButtonGroup bg;

Double d1=0.0,d2=0.0,d3=0.0,d4=1.0,d5=1.0;

String s1="",s2="",s3="",s4="";

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());

b1=new JButton("7");b2=new JButton("8");b3=new JButton("9");b4=new JButton("/");b5=new JButton("1/x");b6=new JButton("sin");b7=new JButton("log");

b8=new JButton("4");b9=new JButton("5");b10=new JButton("6");b11=new JButton("*");b12=new JButton("x^y");b13=new JButton("cos");b14=new JButton("ln");

b15=new JButton("1");b16=new JButton("2");b17=new JButton("3");b18=new JButton("-");b19=new JButton(new ImageIcon("lanying.gif"));b20=new JButton("tan");b21=new JButton("x^3");

b22=new JButton("0");b23=new JButton("+/-");b24=new JButton(".");b25=new JButton("+");b26=new JButton("√x");b27=new JButton("cot");b28=new JButton("x^2");

b29=new JButton("Backspace");b30=new JButton("C");b31=new JButton("=");

mb=new JMenuBar();

m1=new JMenu("文件(F)");m2=new JMenu("编辑(E)");m3=new JMenu("帮助(H)");

mt1=new JMenuItem("清零");mt2=new JMenuItem("退出");mt3=new JMenuItem("复制");mt4=new JMenuItem("粘贴");mt5=new JMenuItem("版本");mt6=new JMenuItem("标准型");mt7=new JMenuItem("科学型");

ta=new JTextArea(1,30);

p1=new JPanel();p2=new JPanel();p3=new JPanel();p4=new JPanel();

rb1=new JRadioButton("科学型");rb2=new JRadioButton("标准型");

bg=new ButtonGroup();

b1.setForeground(Color.blue);b1.setBackground(Color.white);b2.setForeground(Color.blue);b2.setBackground(Color.white);

b3.setForeground(Color.blue);b3.setBackground(Color.white);b8.setForeground(Color.blue);b8.setBackground(Color.white);

b9.setForeground(Color.blue);b9.setBackground(Color.white);b10.setForeground(Color.blue);b10.setBackground(Color.white);

b15.setForeground(Color.blue);b15.setBackground(Color.white);b16.setForeground(Color.blue);b16.setBackground(Color.white);

b17.setForeground(Color.blue);b17.setBackground(Color.white);b22.setForeground(Color.blue);b22.setBackground(Color.white);

b23.setForeground(Color.blue);b23.setBackground(Color.white);b24.setForeground(Color.blue);b24.setBackground(Color.white);

b4.setForeground(Color.red);b4.setBackground(Color.white);b11.setForeground(Color.red);b11.setBackground(Color.white);

b18.setForeground(Color.red);b18.setBackground(Color.white);b25.setForeground(Color.red);b25.setBackground(Color.white);

b5.setForeground(Color.blue);b5.setBackground(Color.white);b6.setForeground(Color.blue);b6.setBackground(Color.white);

b7.setForeground(Color.blue);b7.setBackground(Color.white);b12.setForeground(Color.blue);b12.setBackground(Color.white);

b13.setForeground(Color.blue);b13.setBackground(Color.white);b14.setForeground(Color.blue);b14.setBackground(Color.white);

b19.setForeground(Color.blue);b19.setBackground(Color.white);b20.setForeground(Color.blue);b20.setBackground(Color.white);

b21.setForeground(Color.blue);b21.setBackground(Color.white);b26.setForeground(Color.blue);b26.setBackground(Color.white);

b27.setForeground(Color.blue);b27.setBackground(Color.white);b28.setForeground(Color.blue);b28.setBackground(Color.white);

b29.setForeground(Color.red);b29.setBackground(Color.white);b30.setForeground(Color.red);b30.setBackground(Color.white);

b31.setForeground(Color.red);b31.setBackground(Color.white);

bg.add(rb1);bg.add(rb2);

p1.setBackground(Color.yellow);

cp.setBackground(Color.CYAN);

m1.setMnemonic(KeyEvent.VK_F);m2.setMnemonic(KeyEvent.VK_E);m3.setMnemonic(KeyEvent.VK_H);

m1.add(mt6);m1.add(mt7);m1.addSeparator();m1.add(mt1);m1.addSeparator();m1.add(mt2);m2.add(mt3);m2.addSeparator();m2.add(mt4);m3.add(mt5);

mb.add(m1);mb.add(m2);mb.add(m3);

frame.setJMenuBar(mb);

p2.setLayout(new GridLayout(4,7));

p3.setLayout(new GridLayout(1,3));

ta.setEditable(false);

p1.add(ta);

p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(b7);

p2.add(b8);p2.add(b9);p2.add(b10);p2.add(b11);p2.add(b12);p2.add(b13);p2.add(b14);

p2.add(b15);p2.add(b16);p2.add(b17);p2.add(b18);p2.add(b19);p2.add(b20);p2.add(b21);

p2.add(b22);p2.add(b23);p2.add(b24);p2.add(b25);p2.add(b26);p2.add(b27);p2.add(b28);

p3.add(b29);p3.add(b30);p3.add(b31);

Border etched=BorderFactory.createEtchedBorder();

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

p4.add(rb1);p4.add(rb2);

p4.setBorder(border);

b2.setActionCommand("8");

b2.addActionListener(this);

cp.add(p1);cp.add(p4);cp.add(p2);cp.add(p3);

frame.setSize(400,330);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

b1.setActionCommand("7");

b1.addActionListener(this);

b2.setActionCommand("8");

b2.addActionListener(this);

b3.setActionCommand("9");

b3.addActionListener(this);

b4.setActionCommand("/");

b4.addActionListener(this);

b5.setActionCommand("1/x");

b5.addActionListener(this);

b6.setActionCommand("sin");

b6.addActionListener(this);

b7.setActionCommand("log");

b7.addActionListener(this);

b8.setActionCommand("4");

b8.addActionListener(this);

b9.setActionCommand("5");

b9.addActionListener(this);

b10.setActionCommand("6");

b10.addActionListener(this);

b11.setActionCommand("*");

b11.addActionListener(this);

b12.setActionCommand("x^y");

b12.addActionListener(this);

b13.setActionCommand("cos");

b13.addActionListener(this);

b14.setActionCommand("ln");

b14.addActionListener(this);

b15.setActionCommand("1");

b15.addActionListener(this);

b16.setActionCommand("2");

b16.addActionListener(this);

b17.setActionCommand("3");

b17.addActionListener(this);

b18.setActionCommand("-");

b18.addActionListener(this);

b19.setActionCommand("x!");

b19.addActionListener(this);

b20.setActionCommand("tan");

b20.addActionListener(this);

b21.setActionCommand("x^3");

b21.addActionListener(this);

b22.setActionCommand("0");

b22.addActionListener(this);

b23.setActionCommand("+/-");

b23.addActionListener(this);

b24.setActionCommand(".");

b24.addActionListener(this);

b25.setActionCommand("+");

b25.addActionListener(this);

b26.setActionCommand("√x");

b26.addActionListener(this);

b27.setActionCommand("cot");

b27.addActionListener(this);

b28.setActionCommand("x^2");

b28.addActionListener(this);

b29.setActionCommand("Backspace");

b29.addActionListener(this);

b30.setActionCommand("C");

b30.addActionListener(this);

b31.setActionCommand("=");

b31.addActionListener(this);

rb1.setActionCommand("kxx");

rb1.addActionListener(this);

rb2.setActionCommand("bzx");

rb2.addActionListener(this);

}

public void actionPerformed(ActionEvent e) //throws Exception

{

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

{

b5.setEnabled(false);b6.setEnabled(false);b7.setEnabled(false);

b12.setEnabled(false);b13.setEnabled(false);b14.setEnabled(false);

b19.setEnabled(false);b20.setEnabled(false);b21.setEnabled(false);

b26.setEnabled(false);b27.setEnabled(false);b28.setEnabled(false);

}

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

{

b5.setEnabled(true);b6.setEnabled(true);b7.setEnabled(true);

b12.setEnabled(true);b13.setEnabled(true);b14.setEnabled(true);

b19.setEnabled(true);b20.setEnabled(true);b21.setEnabled(true);

b26.setEnabled(true);b27.setEnabled(true);b28.setEnabled(true);

}

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

{

ta.append("1");

}

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

{

ta.append("2");

}

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

{

ta.append("3");

}

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

{

ta.append("4");

}

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

{

ta.append("5");

}

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

{

ta.append("6");

}

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

{

ta.append("7");

}

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

{

ta.append("8");

}

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

{

ta.append("9");

}

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

{

ta.append("0");

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=1;

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=2;

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=3;

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=4;

}

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

{

s2=ta.getText();

d2=Double.parseDouble(s2);

if(i==1)

{

d3=d1+d2;

ta.setText( d3.toString());

}

if(i==2)

{

d3=d1-d2;

ta.setText( d3.toString());

}

if(i==3)

{

d3=d1*d2;

ta.setText( d3.toString());

}

if(i==4)

{

if(d2==0.0)

ta.setText("ERROR");

else

{

d3=d1/d2;

ta.setText( d3.toString());

}

}

if (i==5)

{

s2=ta.getText();

d2 = Double.parseDouble(s2);

for (int l=1;l=d2 ; l++)

{

d5=d5*d1;

}

ta.setText( d5.toString());

}

}

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

{

ta.setText("");

d4=1.0;

d5=1.0;

}

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

{

s3=ta.getText();

a=s3.length();

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

s4=ta.getText(1,3);

ta.setText(s4);

}

*/

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=1/d1;

ta.setText( d2.toString());

}

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

{

ta.append(".");

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=0-d1;

ta.setText( d2.toString());

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=d1*d1;

ta.setText( d2.toString());

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=d1*d1*d1;

ta.setText( d2.toString());

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

ta.setText("");

i=5;

// d2=d1*d1*d1;

// ta.setText( d2.toString());

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=Math.sqrt(d1);

ta.setText( d2.toString());

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

if (d10)

{

ta.setText( "error");

}

else if (d1==0)

{

ta.setText( "0.0");

}

else {

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

d4=d4*k;

ta.setText( d4.toString());

}

}

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

{

s1=ta.getText();

d1 = Double.parseDouble(s1);

d2=Math.sin(3.1415926*d1/180);

ta.setText( d2.toString());

}

}

}

用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;publicclassJNotePadUIextendsJFrame{privateJMenuItemmenuOpen;privateJMenuItemmenuSave;privateJMenuItemmenuSaveAs;privateJMenuItemmenuClose;privateJMenueditMenu;privateJMenuItemmenuCut;privateJMenuItemmenuCopy;privateJMenuItemmenuPaste;privateJMenuItemmenuAbout;privateJTextAreatextArea;privateJLabelstateBar;privateJFileChooserfileChooser;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){copy();}});//菜单-粘贴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;}}}privatebooleanisCurrentFileSaved(){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);}privatevoidcopy(){textArea.copy();popUpMenu.setVisible(false);}privatevoidpaste(){textArea.paste();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidprocessTextArea(){stateBar.setText("已修改");}publicstaticvoidmain(String[]args){newJNotePadUI();}}


当前题目:java开发记事本代码 Java开发记事本
本文链接:http://cdkjz.cn/article/hjhsdd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220