网站建设资讯

NEWS

网站建设资讯

java记事本完整代码 java记事本代码中的格式部分

JAVA记事本的源代码

我给你如下:/*

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名雅安服务器托管、营销软件、网站建设、吉林网站维护、网站推广。

* WriteBoard.java

*

* Created on 2006年12月19日, 下午7:26

*/

/**

*

* @author LecH.giF

*/

import java.awt.datatransfer.*;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.awt.FileDialog;

public class WriteBoard extends java.awt.Frame {

Clipboard clipboard =null;

FileDialog fc = new FileDialog(this);

/** Creates new form WriteBoard */

public WriteBoard() {

clipboard = getToolkit().getSystemClipboard();

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// editor-fold defaultstate="collapsed" desc=" Generated Code "

private void initComponents() {

textArea1 = new java.awt.TextArea();

menuBar1 = new java.awt.MenuBar();

menu1 = new java.awt.Menu();

menuItem1 = new java.awt.MenuItem();

menuItem2 = new java.awt.MenuItem();

menuItem3 = new java.awt.MenuItem();

menuItem4 = new java.awt.MenuItem();

menuItem5 = new java.awt.MenuItem();

menu2 = new java.awt.Menu();

menuItem6 = new java.awt.MenuItem();

menuItem7 = new java.awt.MenuItem();

menuItem8 = new java.awt.MenuItem();

setTitle("WriteBoard");

addWindowListener(new java.awt.event.WindowAdapter() {

public void windowClosing(java.awt.event.WindowEvent evt) {

exitForm(evt);

}

});

add(textArea1, java.awt.BorderLayout.CENTER);

menu1.setLabel("Menu");

menuItem1.setLabel("\u65b0\u5efa");

menuItem1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

newText(evt);

}

});

menu1.add(menuItem1);

menuItem2.setLabel("\u6253\u5f00");

menuItem2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

open(evt);

}

});

menu1.add(menuItem2);

menuItem3.setLabel("\u4fdd\u5b58");

menuItem3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

menuItem3ActionPerformed(evt);

}

});

menu1.add(menuItem3);

menuItem4.setLabel("\u53e6\u5b58\u4e3a");

menuItem4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

menuItem4ActionPerformed(evt);

}

});

menu1.add(menuItem4);

menuItem5.setLabel("\u9000\u51fa");

menuItem5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

exit(evt);

}

});

menu1.add(menuItem5);

menuBar1.add(menu1);

menu2.setLabel("\u7f16\u8f91");

menuItem6.setLabel("\u526a\u5207");

menuItem6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

menuItem6ActionPerformed(evt);

}

});

menu2.add(menuItem6);

menuItem7.setLabel("\u590d\u5236");

menuItem7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

menuItem7ActionPerformed(evt);

}

});

menu2.add(menuItem7);

menuItem8.setLabel("\u7c98\u8d34");

menuItem8.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

menuItem8ActionPerformed(evt);

}

});

menu2.add(menuItem8);

menuBar1.add(menu2);

setMenuBar(menuBar1);

pack();

}// /editor-fold

private void menuItem4ActionPerformed(java.awt.event.ActionEvent evt) {

fc.show();

if(fc.getFile()!=null){

File file = new File(fc.getFile());

try {

PrintWriter pw = new PrintWriter(file);

pw.print(textArea1.getText());

pw.flush();

pw.close();

} catch (FileNotFoundException ex) {

ex.printStackTrace();

}

}

else{

return;

}

}

private void menuItem3ActionPerformed(java.awt.event.ActionEvent evt) {

fc.show();

if(fc.getFile()!=null){

File file = new File(fc.getFile());

try {

PrintWriter pw = new PrintWriter(file);

pw.print(textArea1.getText());

pw.flush();

pw.close();

} catch (FileNotFoundException ex) {

ex.printStackTrace();

}

}

else{

return;

}

}

private void menuItem8ActionPerformed(java.awt.event.ActionEvent evt) {

Transferable contents = clipboard.getContents(this);

DataFlavor flavor = DataFlavor.stringFlavor;

if(contents.isDataFlavorSupported(flavor))

try{

String str;

str=(String)contents.getTransferData(flavor);

textArea1.append(str);

}catch(Exception e){}

}

private void menuItem7ActionPerformed(java.awt.event.ActionEvent evt) {

String temp = this.textArea1.getSelectedText();

StringSelection text = new StringSelection(temp);

clipboard.setContents(text,null);

}

private void menuItem6ActionPerformed(java.awt.event.ActionEvent evt) {

String temp = this.textArea1.getSelectedText();

StringSelection text = new StringSelection(temp);

clipboard.setContents(text,null);

int start = textArea1.getSelectionStart();

int end = textArea1.getSelectionEnd();

textArea1.replaceRange("",start,end);

}

private void open(java.awt.event.ActionEvent evt) {

fc.show();

if(fc.getFile()!=null){

File file = new File(fc.getFile());

try {

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

String s;

try {

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

textArea1.append(s+"\n");

}

fr.close();

br.close();

} catch (IOException ex) {

ex.printStackTrace();

}

} catch (FileNotFoundException ex) {

ex.printStackTrace();

}

}

else{

return;

}

}

private void newText(java.awt.event.ActionEvent evt) {

this.textArea1.setText("");

}

private void exit(java.awt.event.ActionEvent evt) {

System.exit(0);

}

/** Exit the Application */

private void exitForm(java.awt.event.WindowEvent evt) {

System.exit(0);

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new WriteBoard().setVisible(true);

}

});

}

// Variables declaration - do not modify

private java.awt.Menu menu1;

private java.awt.Menu menu2;

private java.awt.MenuBar menuBar1;

private java.awt.MenuItem menuItem1;

private java.awt.MenuItem menuItem2;

private java.awt.MenuItem menuItem3;

private java.awt.MenuItem menuItem4;

private java.awt.MenuItem menuItem5;

private java.awt.MenuItem menuItem6;

private java.awt.MenuItem menuItem7;

private java.awt.MenuItem menuItem8;

private java.awt.TextArea textArea1;

// End of variables declaration

}

java记事本代码

import java.awt.event.ActionListener;

import java.util.EventListener;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.lang.*;

import java.awt.datatransfer.*;

import javax.swing.*;

public class MiniEdit extends JFrame implements ActionListener {

/**

* Method main

*

*

* @param args

*

*/

MenuBar menuBar = new MenuBar();

Menu file = new Menu("File"),

edit = new Menu("Edit"),

help = new Menu("Help");

MenuItem[] menuItem ={

new MenuItem("New"),

new MenuItem("Open"),

new MenuItem("Save"),

new MenuItem("Exit"),

new MenuItem("Select All"),

new MenuItem("Copy"),

new MenuItem("Cut"),

new MenuItem("Paste"),

new MenuItem("Help")

};

TextArea textArea = new TextArea();

String fileName = "NoName";

Toolkit toolKit = Toolkit.getDefaultToolkit();

Clipboard clipboard = toolKit.getSystemClipboard();

//opne and close message dialogs

private FileDialog openFileDialog =

new FileDialog(this,"Open File",FileDialog.LOAD);

private FileDialog saveFileDialog =

new FileDialog(this,"Save File",FileDialog.SAVE);

public static void main(String[] args) {

// TODO: Add your code here

MiniEdit MyEdit = new MiniEdit();

MyEdit.show();

}

/**

* Method MiniEdit

*

*

*/

public MiniEdit() {

// TODO: Add your code here

setTitle("MiniEdit");

setFont(new Font("Times New Roman",Font.PLAIN,15));

setBackground(Color.white);

setSize(500,500);

setMenuBar(menuBar);

menuBar.add(file);

menuBar.add(edit);

menuBar.add(help);

for(int i=0;i4;i++)

{

file.add(menuItem[i]);

edit.add(menuItem[i+4]);

}

help.add(menuItem[8]);

add(textArea);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

e.getWindow().dispose();

System.exit(0);

}

});

//add actionListener

for(int i=0;imenuItem.length;i++)

{

menuItem[i].addActionListener(this);

}

}

/**

* Method actionPerformed

*

*

* @param e

*

*/

public void actionPerformed(ActionEvent e) {

// TODO: Add your code here

Object eventSource = e.getSource();

if(eventSource == menuItem[0])//newItem

{

textArea.setText("");

}

else if(eventSource == menuItem[1])//OpenItem

{

openFileDialog.show();

fileName = openFileDialog.getDirectory()+openFileDialog.getFile();

if(fileName != null)

{

openFile(fileName);

}

}

else if(eventSource ==menuItem[2])//SaveItem

{

saveFileDialog.show();

fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();

if(fileName !=null)

{

writeFile(fileName);

}

}

else if(eventSource==menuItem[3])//exitItem

{

System.exit(0);

}

else if(eventSource == menuItem[4])//Select All

{

textArea.selectAll();

}

else if(eventSource == menuItem[5])//copy

{

String text = textArea.getSelectedText();

StringSelection selection= new StringSelection(text);

clipboard.setContents(selection,null);

}

else if(eventSource == menuItem[6])//cut

{

String text = textArea.getSelectedText();

StringSelection selection = new StringSelection(text);

clipboard.setContents(selection,null);

textArea.replaceText("",textArea.getSelectionStart(),

textArea.getSelectionEnd());

}

else if(eventSource == menuItem[7])//Paste

{

Transferable contents = clipboard.getContents(this);

if(contents==null)

return;

String text;

text="";

try{

text = (String)contents.getTransferData(DataFlavor.stringFlavor);

}catch(Exception ex){}

textArea.replaceText(text,

textArea.getSelectionStart(),textArea.getSelectionEnd());

}

else if(eventSource == menuItem[8])

{

// JOptionPane.showMessageDialog(null,"This is a MiniEdit.");

}

}

//Read 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(Exception e)

{

System.out.println("Error opening file!");

}

}

//write file

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

就这样交,不会错的

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

import java.io.FileWriter;

import java.io.IOException;

import javax.swing.JFileChooser;

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

JTextArea text = new TextAreaMenu(); //文本编辑区

JScrollPane jsp = new JScrollPane(text); //可滚动编辑区

JMenuBar mnbMain = new JMenuBar();

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

JMenu mnEdit = new JMenu("编辑(E)");

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

mnbMain.setBounds(5, 0, 50, 30);

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

mnServer.add(mniServers[i]);

}

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

@Override

public void actionPerformed(ActionEvent arg0) {

new Demo(getLocation().x+15,getLocation().y+5);

}

});

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

@Override

public void actionPerformed(ActionEvent arg0) {

chooseToSave();

}

});

mniServers[2].addActionListener(new ActionListener() { //定义"打开"组件操作

@Override

public void actionPerformed(ActionEvent arg0) {

chooseToOpen();

}

});

mniServers[3].addActionListener(new ActionListener() { //定义"退出"组件操作

@Override

public void actionPerformed(ActionEvent arg0) {

System.exit(0);

}

});

text.addFocusListener(new FocusListener() {

@Override

public void focusLost(FocusEvent e) {

// TODO Auto-generated method stub

}

@Override

public void focusGained(FocusEvent e) {

}

});

}

public Demo(int x,int y) {

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

this.setBounds(x, y, 600, 400);

this.setLayout(new BorderLayout());

this.add(menuPanel, BorderLayout.NORTH);

this.add(jsp);

jsp.setBounds(5, 30, getWidth()-10, getHeight()-50);

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

this.setVisible(true);

}

public Demo() {

this(200,200);

}

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

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

}

protected void chooseToOpen() {

File file = chooseFile();

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

name = file.getName();

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

read(text,file);

}

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

public static void main(String[] args) {

new Demo();

}

private File chooseFile(){

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

chooser.setFileFilter(new FileFilter() {

@Override

public String getDescription() {

return "文本文件";

}

@Override

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;

try {

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

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

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

FileWriter fw;

try {

fw = new FileWriter(fileName);

BufferedWriter bw = new BufferedWriter(fw);

bw.write(txt);

bw.flush();

bw.close();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

class TextAreaMenu extends JTextArea implements MouseListener {

private static final long serialVersionUID = -2308615404205560110L;

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

public void actionPerformed(ActionEvent e) {

action(e);

}

});

paste.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

action(e);

}

});

cut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

action(e);

}

});

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())) { //撤销

this.cut();

}

}

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

try {

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

b = true;

}

} catch (Exception e) {

}

return b;

}

/**

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

*

* @return true为具备

*/

public boolean isCanCopy() {

boolean b = false;

int start = this.getSelectionStart();

int end = this.getSelectionEnd();

if (start != end)

b = true;

return b;

}

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

copy.setEnabled(isCanCopy());

paste.setEnabled(isClipboardString());

cut.setEnabled(isCanCopy());

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

}

}

public void mouseReleased(MouseEvent e) {

}

}

记事本java代码

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

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

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

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

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

2、然后使用记事本的方式打开,输入java程序代码,如下图所示。

3、接着在键盘上按“win+R”快捷键键打开运行,输入“cmd”,如下图所示。

4、在命令行输入“D:”,按“Enter”键进去D盘,再输入“cd Desktop”进去Desktop文件夹,如下图所示。

5、最后再输入“javac Test.java”,按“Enter”键编译java程序,如下图所示就完成了。


分享标题:java记事本完整代码 java记事本代码中的格式部分
地址分享:http://cdweb.net/article/dochdgs.html