我给你如下:/*
创新互联是一家专业从事网站设计制作、网站设计、网页设计的品牌网络公司。如今是成都地区具影响力的网站设计公司,作为专业的成都网站建设公司,创新互联依托强大的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、营销型网站建设及网站设计开发服务!
* 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
}
你安装JDK的目录下,有个src.zip文件,这个就是JDK源代码的java文件。
你可以解压来查看,但,最好是关联到IDE如 eclipse 中(不需解压),然后 CTRL + 点击就可以查看到源代码了。
如下图:
给你个做好了的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 JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
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;
write();
}
}
}else if(!(jta.getText().isEmpty())){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}else{
newNew();
}
}else if("打开".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=result;
jta.setText(result);
}
}else if("保存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
if(flag){
int returnVal = jfc.showDialog(null,"保存为");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
flag=false;
write();
}
}else{
write();
}
}else if("另存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java课堂笔记");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION) {//选择文件后再执行下面的语句,保证了程序的健壮性
f=jfc.getSelectedFile();
write();
}
}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)){
value=jta.getText();
jta.paste();
}else if("删除".equals(comm)){
value=jta.getText();
jta.replaceSelection(null);
}else if("全选".equals(comm)){
jta.selectAll();
}else if("查找".equals(comm)){
value=jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("替换".equals(comm)){
value=jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找内容:");
JLabel jl2=new JLabel("替换为:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("版本".equals(comm)){
JDialog jd=new JDialog(frame,"关于对话框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
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)){
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;
jta.replaceSelection(jt.getText());
}else {
JOptionPane.showMessageDialog(jf, "查找完毕!", "提示", 0, ic);
jf.dispose();
}
}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());
byte[] b=new byte[1024];
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(){
try {
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[] menuLab={"文件","编辑","帮助"};
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.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyNotepad();
}
}