自己写的JAVA记事本
2008-05-04 16:37
399 查看
刚学Java不久就做了这个小东东,功能嘛肯定不是很强啦,倒是可以用来学习学习!
源代码:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
public class FileIO extends WindowAdapter implements ActionListener{
JFrame jf;
JButton jb,jb1;
JTextArea ta;
String filename,copy,paste,cut;
JPanel jp;
JMenu jmb,jmb2;
JMenuItem _fm,_fm1,_fm2,_fm3,_fm4,_fe1,_fe2,_fe3,_fe4;
JMenuBar JMENU;
JScrollPane jsp;
JLabel jl;
public static void main (String[] argv) {
new FileIO();
}
public FileIO()
{
jp = new JPanel();
jl = new JLabel("李鸿记事本V1.0");
JMENU = new JMenuBar();
ta = new JTextArea();
jf = new JFrame();
jsp = new JScrollPane(ta);
jf.addWindowListener(this);
jmb = new JMenu("文件");
jmb2 = new JMenu("编辑");
_fm1 = new JMenuItem("打开");
_fm1.addActionListener(this);
_fm2 = new JMenuItem("储存");
_fm2.addActionListener(this);
_fm4 = new JMenuItem("另存为");
_fm4.addActionListener(this);
_fm3 = new JMenuItem("关闭");
_fm3.addActionListener(this);
_fm = new JMenuItem("新建");
_fm.addActionListener(this);
_fe1 = new JMenuItem("复制");
_fe1.addActionListener(this);
_fe2 = new JMenuItem("粘贴");
_fe2.addActionListener(this);
_fe3 = new JMenuItem("剪切");
_fe3.addActionListener(this);
_fe4 = new JMenuItem("作者");
_fe4.addActionListener(this);
jf.setJMenuBar(JMENU);
jf.setTitle("记事本");
jmb.add(_fm);
jmb.addSeparator();
jmb.add(_fm1);
jmb.addSeparator();
jmb.add(_fm2);
jmb.addSeparator();
jmb.add(_fm4);
jmb.addSeparator();
jmb.add(_fm3);
jmb2.add(_fe1);
jmb2.addSeparator();
jmb2.add(_fe2);
jmb2.addSeparator();
jmb2.add(_fe3);
jmb2.addSeparator();
jmb2.add(_fe4);
JMENU.add(jmb);
JMENU.add(jmb2);
jb = new JButton("保存");
jb.addActionListener(this);
jb1 = new JButton("关闭");
jb1.addActionListener(this);
jp.add(jb);
jp.add(jb1);
jp.add(jl);
jf.add(jp,"South");
ta.setWrapStyleWord(true);
jf.add(jsp);
jf.setSize(600,400);
jf.setVisible(true);
int W = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int H = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
jf.setLocation((W-jf.getWidth())/2,(H-jf.getHeight())/2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb||e.getSource()==_fm2){
try{
if(filename==null)
{
filename = JOptionPane.showInputDialog("请输入文件名","java");
FileOutputStream fout = new FileOutputStream(filename+".txt");
byte bb[] = ta.getText().getBytes();
fout.write(bb);
fout.close();
JOptionPane.showMessageDialog(null,"已保存");
}else
{
FileOutputStream fout = new FileOutputStream(filename+".txt");
byte bb[] = ta.getText().getBytes();
fout.write(bb);
fout.close();
JOptionPane.showMessageDialog(null,"已保存");
}
}catch(IOException ioe)
{
System.err.println(e);
}
}
if(e.getSource()==_fm)
{
if(!(ta.getText().equals(""))){
Object[] options = {"确定","取消"};
int response = JOptionPane.showOptionDialog(null,"你是否保存","提 示",JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
if(response==0)
{try{
FileDialog d = new FileDialog(jf,"保存文件",FileDialog.SAVE);
d.setVisible(true);
filename = d.getDirectory()+d.getFile();
FileOutputStream fout = new FileOutputStream(filename+".txt");
byte bb[] = ta.getText().getBytes();
fout.write(bb);
fout.close();
JOptionPane.showMessageDialog(null,"已保存");
ta.setText("");
}catch(IOException ioe)
{
System.err.println(e);
}
}
if(response==1)
{JOptionPane.showMessageDialog(null,"你选择了取消");
ta.setText("");
}
}}
if(e.getSource()==_fm1){
FileDialog d = new FileDialog(jf,"打开文件",FileDialog.LOAD);
d.setVisible(true);
File f = null;
f = new File(d.getDirectory()+d.getFile());
for(int i=0;i<=f.length();i++)
{char [] ch = new char[i];
try{
FileReader fr = new FileReader(f);
fr.read(ch);
String str = new String(ch);
ta.setText(str);
}catch(FileNotFoundException fe){
}catch(IOException ie){
}
}
}
if(e.getSource()==_fm4)
{
FileDialog d = new FileDialog(jf,"另存为",FileDialog.SAVE);
d.setVisible(true);
try{
filename = d.getDirectory()+d.getFile();
FileOutputStream fout = new FileOutputStream(filename+".txt");
byte bb[] = ta.getText().getBytes();
fout.write(bb);
fout.close();
}catch(IOException ioe)
{
System.err.println(e);
}
}
if(e.getSource()==_fm3||e.getSource()==jb1)
{System.exit(0);}
if(e.getSource()==_fe1)
{
copy = ta.getSelectedText();
}
if(e.getSource()==_fe2)
{
ta.setText(copy);
}
if(e.getSource()==_fe3)
{
copy = ta.getSelectedText();
ta.setText("");
}
if(e.getSource()==_fe4)
{
JOptionPane.showMessageDialog(jf,"作者:李鸿 /n ! 提醒 好好学习,天天向上 /n学习JAVA很快乐!");
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
源代码:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
public class FileIO extends WindowAdapter implements ActionListener{
JFrame jf;
JButton jb,jb1;
JTextArea ta;
String filename,copy,paste,cut;
JPanel jp;
JMenu jmb,jmb2;
JMenuItem _fm,_fm1,_fm2,_fm3,_fm4,_fe1,_fe2,_fe3,_fe4;
JMenuBar JMENU;
JScrollPane jsp;
JLabel jl;
public static void main (String[] argv) {
new FileIO();
}
public FileIO()
{
jp = new JPanel();
jl = new JLabel("李鸿记事本V1.0");
JMENU = new JMenuBar();
ta = new JTextArea();
jf = new JFrame();
jsp = new JScrollPane(ta);
jf.addWindowListener(this);
jmb = new JMenu("文件");
jmb2 = new JMenu("编辑");
_fm1 = new JMenuItem("打开");
_fm1.addActionListener(this);
_fm2 = new JMenuItem("储存");
_fm2.addActionListener(this);
_fm4 = new JMenuItem("另存为");
_fm4.addActionListener(this);
_fm3 = new JMenuItem("关闭");
_fm3.addActionListener(this);
_fm = new JMenuItem("新建");
_fm.addActionListener(this);
_fe1 = new JMenuItem("复制");
_fe1.addActionListener(this);
_fe2 = new JMenuItem("粘贴");
_fe2.addActionListener(this);
_fe3 = new JMenuItem("剪切");
_fe3.addActionListener(this);
_fe4 = new JMenuItem("作者");
_fe4.addActionListener(this);
jf.setJMenuBar(JMENU);
jf.setTitle("记事本");
jmb.add(_fm);
jmb.addSeparator();
jmb.add(_fm1);
jmb.addSeparator();
jmb.add(_fm2);
jmb.addSeparator();
jmb.add(_fm4);
jmb.addSeparator();
jmb.add(_fm3);
jmb2.add(_fe1);
jmb2.addSeparator();
jmb2.add(_fe2);
jmb2.addSeparator();
jmb2.add(_fe3);
jmb2.addSeparator();
jmb2.add(_fe4);
JMENU.add(jmb);
JMENU.add(jmb2);
jb = new JButton("保存");
jb.addActionListener(this);
jb1 = new JButton("关闭");
jb1.addActionListener(this);
jp.add(jb);
jp.add(jb1);
jp.add(jl);
jf.add(jp,"South");
ta.setWrapStyleWord(true);
jf.add(jsp);
jf.setSize(600,400);
jf.setVisible(true);
int W = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int H = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
jf.setLocation((W-jf.getWidth())/2,(H-jf.getHeight())/2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb||e.getSource()==_fm2){
try{
if(filename==null)
{
filename = JOptionPane.showInputDialog("请输入文件名","java");
FileOutputStream fout = new FileOutputStream(filename+".txt");
byte bb[] = ta.getText().getBytes();
fout.write(bb);
fout.close();
JOptionPane.showMessageDialog(null,"已保存");
}else
{
FileOutputStream fout = new FileOutputStream(filename+".txt");
byte bb[] = ta.getText().getBytes();
fout.write(bb);
fout.close();
JOptionPane.showMessageDialog(null,"已保存");
}
}catch(IOException ioe)
{
System.err.println(e);
}
}
if(e.getSource()==_fm)
{
if(!(ta.getText().equals(""))){
Object[] options = {"确定","取消"};
int response = JOptionPane.showOptionDialog(null,"你是否保存","提 示",JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
if(response==0)
{try{
FileDialog d = new FileDialog(jf,"保存文件",FileDialog.SAVE);
d.setVisible(true);
filename = d.getDirectory()+d.getFile();
FileOutputStream fout = new FileOutputStream(filename+".txt");
byte bb[] = ta.getText().getBytes();
fout.write(bb);
fout.close();
JOptionPane.showMessageDialog(null,"已保存");
ta.setText("");
}catch(IOException ioe)
{
System.err.println(e);
}
}
if(response==1)
{JOptionPane.showMessageDialog(null,"你选择了取消");
ta.setText("");
}
}}
if(e.getSource()==_fm1){
FileDialog d = new FileDialog(jf,"打开文件",FileDialog.LOAD);
d.setVisible(true);
File f = null;
f = new File(d.getDirectory()+d.getFile());
for(int i=0;i<=f.length();i++)
{char [] ch = new char[i];
try{
FileReader fr = new FileReader(f);
fr.read(ch);
String str = new String(ch);
ta.setText(str);
}catch(FileNotFoundException fe){
}catch(IOException ie){
}
}
}
if(e.getSource()==_fm4)
{
FileDialog d = new FileDialog(jf,"另存为",FileDialog.SAVE);
d.setVisible(true);
try{
filename = d.getDirectory()+d.getFile();
FileOutputStream fout = new FileOutputStream(filename+".txt");
byte bb[] = ta.getText().getBytes();
fout.write(bb);
fout.close();
}catch(IOException ioe)
{
System.err.println(e);
}
}
if(e.getSource()==_fm3||e.getSource()==jb1)
{System.exit(0);}
if(e.getSource()==_fe1)
{
copy = ta.getSelectedText();
}
if(e.getSource()==_fe2)
{
ta.setText(copy);
}
if(e.getSource()==_fe3)
{
copy = ta.getSelectedText();
ta.setText("");
}
if(e.getSource()==_fe4)
{
JOptionPane.showMessageDialog(jf,"作者:李鸿 /n ! 提醒 好好学习,天天向上 /n学习JAVA很快乐!");
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
相关文章推荐
- Java 错误记事本---记录自己菜鸟编程中出现的错误。
- 自己写的javaGUI记事本
- 自己做的java 记事本(查找,替换部分功能缺失) 多多交流
- 自己写的JAVA记事本(很简单,对初学者有帮助)
- JAVA强制类型转换(转载+自己的感想)
- 排序算法,基本的高级语言都有一些提供。C语言有qsort()函数,C++有sort()函数,java语言有Arrays类(不是Array)。用这些排序时,都可以写自己的排序规则。
- 本例把“记事本”程序放进自己的窗体里
- 数据结构—顺序表(自己实现Java的ArrayList)
- java-我的记事本
- 自己动手编写启动JAVA程序的EXE文件
- 自己写的一个 java InputDate类
- Java 中构建自己的异常类(二)
- 提高自己JAVA水平的十大技术讨论
- 11 csdn java 王少飞-编写自己的JDBC框架
- Java_JavaDoc_构建自己的JavaDoc
- Java开发_生成/解析自己的QRCode二维码
- 汗。。。自己的java语法居然都没学全for (File file : files)
- 在java中创建自己的swing界面
- 自己动手写搜索引擎(常搜吧历程五#解析文档之XML#)(Java、Lucene、hadoop)
- java记事本小程序