java记事本
Ⅰ 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编写一个记事本
不添加监听的记事本不是好的记事本:
http://hi..com/shy2850/blog/item/3d15b409797d34dc63d98662.html
呵呵,虽然不符合要求,但是基本功能还是很值得借鉴的。
Ⅲ java简单记事本代码
以下代码是一个完整的实现,你只要复制过去就可以了~~
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import javax.swing.text.*;
public class Start
{
public static void main(String args[])
{
new TextPad();
}
}
class TextPad extends JFrame implements ActionListener
{
JTextArea jta=new JTextArea("小廖记事本:)",18,52);
JCheckBoxMenuItem mto1=new JCheckBoxMenuItem("自动换行",true);
String ss1=jta.getText();
UndoableEditListener ue=new UndoHander();
UndoManager undo = new UndoManager();
int StartFindPos=0,a=0,b=0;
GridBagConstraints gbc=new GridBagConstraints();
//Dimension dd=new Dimension();
// jta.getDocument().addUndoableEditListener(ue);
public TextPad()
{
//MyMenuListener ml=new MyMenuListener();
//JTextArea jta=new JTextArea("This is my textpad",18,52);
//System.out.println(dd.getHeight());
//System.out.println(dd.getWidth());
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
//System.out.println("OK");
this.setTitle("一个功能比较齐全的JAVA记事本");
this.setLocation(180,100);
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JPanel jp=new JPanel();
JScrollPane jsp=new JScrollPane(jta);
jp.add(jsp);
//Rectangle rt=new Rectangle(0,0,this.getWidth(),this.getHeight());
//jsp.setBounds(rt);
//System.out.println(this.getHeight());
//System.out.println(this.getWidth());
JMenu mf=new JMenu("文件(F)");
JMenuItem mtf1=new JMenuItem("新建");
mtf1.addActionListener(this);
JMenuItem mtf2=new JMenuItem("打开");
//mtf2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
mtf2.addActionListener(this);
JMenuItem mtf3=new JMenuItem("保存");
//mtf3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
mtf3.addActionListener(this);
JMenuItem mtf4=new JMenuItem("另存为");
mtf4.addActionListener(this);
JMenuItem mtf5=new JMenuItem("退出");
mtf5.addActionListener(this);
JMenu me=new JMenu("编辑(E)");
JMenuItem mte1=new JMenuItem("撤消");
mte1.addActionListener(this);
jta.getDocument().addUndoableEditListener(ue);
if(undo.canUndo())
{
mte1.setEnabled(false);
}
JMenuItem mte2=new JMenuItem("剪切");
mte2.addActionListener(this);
JMenuItem mte3=new JMenuItem("复制");
mte3.addActionListener(this);
JMenuItem mte4=new JMenuItem("粘贴");
mte4.addActionListener(this);
//JMenuItem mte5=new JMenuItem("删除");
//mte5.addActionListener(this);
JMenuItem mte6=new JMenuItem("查找");
mte6.addActionListener(this);
//JMenuItem mte7=new JMenuItem("查找下一个");
JMenuItem mte8=new JMenuItem("替换");
mte8.addActionListener(this);
//JMenuItem mte9=new JMenuItem("转到");
JMenuItem mte10=new JMenuItem("全选");
mte10.addActionListener(this);
JMenuItem mte11=new JMenuItem("日期/时间");
mte11.addActionListener(this);
JMenu mo=new JMenu("格式(O)");
//JCheckBoxMenuItem mto1=new JCheckBoxMenuItem("自动换行(W)");
mto1.addActionListener(this);
JMenuItem mto2=new JMenuItem("字体");
mto2.addActionListener(this);
JMenu mv=new JMenu("查看(V)");
JMenuItem mtv1=new JMenuItem("状态栏");
mtv1.setEnabled(false);
JMenu mh=new JMenu("帮助(H)");
JMenuItem mth1=new JMenuItem("关于记事本");
mth1.addActionListener(this);
JMenuBar mb=new JMenuBar();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
mb.add(mf);
mb.add(me);
mb.add(mo);
mb.add(mv);
mb.add(mh);
this.setJMenuBar(mb);
mf.add(mtf1);
mf.add(mtf2);
mf.add(mtf3);
mf.add(mtf4);
mf.addSeparator();
mf.add(mtf5);
me.add(mte1);
me.addSeparator();
me.add(mte2);
me.add(mte3);
me.add(mte4);
//me.add(mte5);
me.addSeparator();
me.add(mte6);
//me.add(mte7);
me.add(mte8);
//me.add(mte9);
me.addSeparator();
me.add(mte10);
me.add(mte11);
mo.add(mto1);
mo.add(mto2);
mv.add(mtv1);
mh.add(mth1);
this.getContentPane().add(jsp);
this.setSize(600,400);
this.setResizable(true);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("打开"))
{
try
{
Frame f=new Frame();
FileDialog fd=new FileDialog(f,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
String fpath=fd.getDirectory();
String fname=fd.getFile();
BufferedReader br=new BufferedReader(new FileReader(fpath+fname));
jta.setText("");
String s=br.readLine();
while(s!=null)
{
jta.append(s+"\n");
s=br.readLine();
}
br.close();
}
catch(Exception ex)
{
}
}
if(e.getActionCommand().equals("保存"))
{
String fns=null;
Frame f=new Frame("保存");
FileDialog fd=new FileDialog(f,"保存文件",FileDialog.SAVE);
fd.setFile("*.txt");
fd.setVisible(true);
try
{
String savepath=fd.getDirectory();
String savename=fd.getFile();
if(savename!=null)
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(savepath+savename)));
pw.write(jta.getText(),0,jta.getText().length());
pw.flush();
}
}
catch(Exception esave)
{
}
}
if(e.getActionCommand().equals("新建"))
{
jta.setText("");
}
if(e.getActionCommand().equals("另存为"))
{
Frame f=new Frame("保存");
FileDialog fd=new FileDialog(f,"文件另存为",FileDialog.SAVE);
fd.setVisible(true);
try
{
String savepath=fd.getDirectory();
String savename=fd.getFile();
if(savename!=null)
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(savepath+savename)));
pw.write(jta.getText(),0,jta.getText().length());
pw.flush();
}
}
catch(Exception esave)
{
}
}
if(e.getActionCommand().equals("退出"))
{
String ss2=jta.getText();
if(!ss1.equals(ss2))
{
System.out.println("File is changed.");
}
System.exit(0);
}
if(e.getActionCommand().equals("撤消"))
{
try
{
undo.undo();
//System.out.println(undo.canUndo());
}
catch(Exception eundo)
{
}
}
if(e.getActionCommand().equals("剪切"))
{
jta.cut();
}
if(e.getActionCommand().equals("复制"))
{
jta.();
}
if(e.getActionCommand().equals("粘贴"))
{
jta.paste();
}
if(e.getActionCommand().equals("删除"))
{
}
if(e.getActionCommand().equals("全选"))
{
jta.selectAll();
}
if(e.getActionCommand().equals("查找"))
{
try
{
final JDialog jd=new JDialog(this,"查找",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridwidth=1;
gbc.gridheight=1;
jd.getContentPane().setLayout(gbl);
jd.setSize(380,100);
jd.setResizable(false);
//jd.(true);
final JTextField jtf=new JTextField(15);
JLabel jlFind=new JLabel("查找内容:");
jd.getContentPane().add(jlFind);
JButton jbFind=new JButton("查找");
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=jta.getText();
String strB=jtf.getText();
if(a>=0)
{
a=strA.indexOf(strB,StartFindPos);
b=strB.length();
StartFindPos=a+b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1);
a=0;
StartFindPos=0;
}
jta.select(a,StartFindPos);
}
}
}
);
JButton jbCancel=new JButton("取消");
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.getContentPane().add(jtf);
jd.getContentPane().add(jbFind);
jd.getContentPane().add(jbCancel);
//jd.setResizable(false);
jd.setLocation(240,200);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jd.setVisible(true);
}
catch(Exception efind)
{
}
}
if(e.getActionCommand().equals("替换"))
{
final JDialog jd=new JDialog(this,"替换",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=1;
gbc.weighty=1;
gbc.gridwidth=1;
gbc.gridheight=1;
JLabel jlFind=new JLabel("查找:");
JLabel jp=new JLabel("替换内容:");
final JTextField jtf=new JTextField(15);
final JTextField jtf1=new JTextField(15);
jd.getContentPane().setLayout(gbl);
jd.setSize(330,150);
jd.setResizable(false);
final JButton jbReplace=new JButton("替换");
final JButton jbReplaceAll=new JButton("替换所有");
final JButton jbCancel=new JButton("取消");
final JButton jbFind=new JButton("查找");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jlFind,gbc);
gbc.gridx=1;
gbc.gridy=0;
jd.getContentPane().add(jtf1,gbc);
gbc.gridx=2;
gbc.gridy=0;
jd.getContentPane().add(jbFind,gbc);
gbc.gridx=0;
gbc.gridy=1;
jd.getContentPane().add(jp,gbc);
gbc.gridx=1;
gbc.gridy=1;
jd.getContentPane().add(jtf,gbc);
gbc.gridx=2;
gbc.gridy=1;
jd.getContentPane().add(jbReplace,gbc);
gbc.gridx=2;
gbc.gridy=2;
jd.getContentPane().add(jbReplaceAll,gbc);
gbc.gridx=2;
gbc.gridy=3;
jd.getContentPane().add(jbCancel,gbc);
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=jta.getText();
String strB=jtf1.getText();
if(a>=0)
{
a=strA.indexOf(strB,StartFindPos);
//System.out.println(a+b);
b=strB.length();
StartFindPos=a+b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果",1);
a=0;
StartFindPos=0;
}
jta.select(a,StartFindPos);
//System.out.println(StartFindPos);
}
}
}
);
jbReplace.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("替换"))
{
String strRepleace=jtf.getText();
jta.replaceSelection(strRepleace);
}
}
}
);
jbReplaceAll.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
while(a>-1)
{
String strA=jta.getText();
String strB=jtf1.getText();
a=strA.indexOf(strB,StartFindPos);
if(a==-1)
{
break;
}
//System.out.println(a+b);
b=strB.length();
StartFindPos=a+b;
//System.out.println(StartFindPos);
jta.select(a,StartFindPos);
//System.out.println(StartFindPos);
String strRepleaceAll=jtf.getText();
jta.replaceSelection(strRepleaceAll);
StartFindPos=a+b;
}
JOptionPane.showMessageDialog(null, "全部替换完毕", "替换内容",1);
a=0;
StartFindPos=0;
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.setLocation(240,200);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("日期/时间"))
{
final JDialog jd=new JDialog(this,"插入日期");
JPanel jp1=new JPanel();
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextField jtf=new JTextField(10);
JButton jbOK=new JButton("确定");
JButton jbCancel=new JButton("取消");
//Calendar cl=Calendar.getInstance();
//DateFormat df=DateFormat.getInstance();
//String sdate1=df.format(cl.getTime());
//jtf.setText(sdate1);
jp1.add(jtf);
jp1.add(jbOK);
jp1.add(jbCancel);
jd.getContentPane().add(jp1,"North");
JPanel jp2=new JPanel();
jp2.setLayout(new FlowLayout(FlowLayout.LEFT));
final JCheckBox jcb1=new JCheckBox("格式一");
final JCheckBox jcb2=new JCheckBox("格式二");
final JCheckBox jcb3=new JCheckBox("格式三");
jp2.add(jcb1);
jp2.add(jcb2);
jp2.add(jcb3);
jd.getContentPane().add(jp2,"Center");
jd.setSize(220,120);
jd.setResizable(false);
jd.setLocation(240,200);
final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cl=Calendar.getInstance();
DateFormat df=DateFormat.getInstance();
final String sdate=df.format(cl.getTime());
jcb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式一"))
{
if(jcb1.isSelected())
{
try
{
SimpleDateFormat sdf1=new SimpleDateFormat("yy年MM月dd日");
Date d=sdf.parse(sdate);
jtf.setText(sdf1.format(d));
jcb2.setEnabled(false);
jcb3.setEnabled(false);
}
catch(Exception estyle1)
{
estyle1.printStackTrace();
}
}
else
{
jcb2.setEnabled(true);
jcb3.setEnabled(true);
}
try
{
System.out.println(jta.getLineStartOffset(3));
//System.out.println(jta.getLineOfOffset(346));
}
catch(Exception eee)
{
}
}
}
}
);
jcb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式二"))
{
if(jcb2.isSelected())
{
try
{
SimpleDateFormat sdf1=new SimpleDateFormat("yy/MM/dd");
Date d=sdf.parse(sdate);
jtf.setText(sdf1.format(d));
jcb1.setEnabled(false);
jcb3.setEnabled(false);
}
catch(Exception estyle2)
{
estyle2.printStackTrace();
}
}
else
{
jcb1.setEnabled(true);
jcb3.setEnabled(true);
}
//System.out.println(jcb1.isSelected());
}
}
}
);
jcb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("格式三"))
{
if(jcb3.isSelected())
{
jtf.setText(sdate);
jcb1.setEnabled(false);
jcb2.setEnabled(false);
}
else
{
jcb1.setEnabled(true);
jcb2.setEnabled(true);
}
//System.out.println(jcb1.isSelected());
}
}
}
);
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("确定"))
{
int pos=jta.getCaretPosition();
jta.insert(jtf.getText(),pos);
}
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("自动换行"))
{
if(mto1.getState())
{
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
}
else
{
jta.setLineWrap(false);
jta.setWrapStyleWord(false);
}
//System.out.println("OK");
}
if(e.getActionCommand().equals("字体"))
{
final JDialog jd=new JDialog(this,"字体设置");
jd.setLocation(240,200);
//final JFrame jfontview=new JFrame();
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
JButton jbOK=new JButton("确定");
JButton jbCancel=new JButton("取消");
JTextField jtf1=new JTextField(6);
final JTextArea jtaview=new JTextArea(4,8);
//jfontview.getContentPane().add(jtaview);
final JTextField jtf2=new JTextField(6);
final JTextField jtf3=new JTextField(3);
JComboBox jcb1=new JComboBox();
final JComboBox jcb2=new JComboBox();
jcb2.addItem("BOLD");
jcb2.addItem("ITALIC");
jcb2.addItem("PLAIN");
//jcb2.addItem("BOLDITALIC");
jcb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf2.setText((String)jcb2.getSelectedItem());
}
}
);
final JComboBox jcb3=new JComboBox();
jcb3.addItem("14");
jcb3.addItem("18");
jcb3.addItem("22");
jcb3.addItem("26");
jcb3.addItem("30");
jcb3.addItem("34");
jcb3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf3.setText((String)jcb3.getSelectedItem());
//jtaview.setText("SS");
// int fontsizeview=Integer.parseInt((String)jcb3.getSelectedItem());
//Font fontview=new Font("字体预览",Font.BOLD,fontsizeview);
//jtaview.setFont(fontview);
//jtaview.setEditable(false);
}
}
);
gbc.gridheight=1;
gbc.gridwidth=1;
//gbc.gridx=1;
//gbc.gridy=0;
gbc.weightx=0.5;
gbc.weighty=0.5;
jd.getContentPane().setLayout(gbl);
//JLabel jl1=new JLabel("预览:");
JLabel jl2=new JLabel("字型:");
JLabel jl3=new JLabel("大小:");
gbc.gridx=0;
gbc.gridy=0;
jd.getContentPane().add(jl2,gbc);
gbc.gridx=2;
gbc.gridy=0;
jd.getContentPane().add(jl3,gbc);
//gbc.gridx=4;
//gbc.gridy=0;
//jd.getContentPane().add(jl3,gbc);
gbc.gridx=0;
gbc.gridy=1;
jd.getContentPane().add(jtf2,gbc);
gbc.gridx=2;
gbc.gridy=1;
jd.getContentPane().add(jtf3,gbc);
//gbc.gridx=4;
// gbc.gridy=1;
//jd.getContentPane().add(jtf3,gbc);
gbc.gridx=0;
gbc.gridy=2;
jd.getContentPane().add(jcb2,gbc);
gbc.gridx=2;
gbc.gridy=2;
jd.getContentPane().add(jcb3,gbc);
//gbc.gridx=4;
//gbc.gridy=2;
//jd.getContentPane().add(jcb3,gbc);
gbc.gridx=4;
gbc.gridy=1;
jd.getContentPane().add(jbOK,gbc);
gbc.gridx=4;
gbc.gridy=2;
jd.getContentPane().add(jbCancel,gbc);
jbOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jtf2.getText().equals("PLAIN"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=0;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
// System.out.println(f.getFont("楷体_GB2312").getFontName());
}
if(jtf2.getText().equals("BOLD"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=1;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
}
if(jtf2.getText().equals("ITALIC"))
{
int fontsize=Integer.parseInt(jtf3.getText());
int fontstyle=2;
Font f=new Font("字体设置",fontstyle,fontsize);
jta.setFont(f);
}
jd.dispose();
//System.out.println(fontstyle);
}
}
);
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jd.dispose();
}
}
);
jd.setSize(200,120);
jd.setResizable(false);
jd.setVisible(true);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
if(e.getActionCommand().equals("关于记事本"))
{
JOptionPane jop=new JOptionPane(null,JOptionPane.INFORMATION_MESSAGE);
jop.showMessageDialog(null,"小廖记事本","关于记事本",JOptionPane.OK_OPTION);
//System.out.println("OK");
}
}
class UndoHander implements UndoableEditListener
{
public void undoableEditHappened(UndoableEditEvent eundo)
{
undo.addEdit(eundo.getEdit());
}
}
}
Ⅳ 如何用记事本进行java编程
1.安装JDK
下载地址:http://cn.sun.com/
2:配置环境变量
方法:
安装后需要设置环境变量,在我的电脑--》属性--->高级--->环境变量
加入:JAVA_HOME = C:\Program Files\Java\jdk1.5.0_06
然后把JAVA_HOME 加入path中就可以了
PATH=F:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;C:\Program Files\Oracle\jre\1.1.8\bin;%JAVA_HOME%\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PROGRA~1\ULTRAE~1;D:\soft\StyleReportEE\bin
3:工具初学者可以用记事本,(也可以用eclipse,netbeans)
编写好后报存成 .java文件
4.编译
在DOS窗口下找到你保存的JAVA文件目录,执行:javac *.java
5:运行
在dos窗口下找到你编译的文件目录执行: java *
这样你就可以看到你写的程序的执行结果。
刚开始学Java,用“JDK+记事本”就行了,JDK提供Java开发工具包和运行环境。先下载一个JDK(http://blog.chinaunix.net/u2/80678/showart_1315214.html)装上(还有设置环境变量的问题,这里先不说),用记事本编辑Java代码,在命令行提示窗口用javac 命令编译,用java 命令执行就可以了。
当你对java程序有个整体感官的认识后,接下来就可以用一个IDE了,即集成开发环境,网上有很多IDE,JBuilder、JCreater以及着名的eclipse,推荐用eclipse,在www.eclipse.org下载,然后自己往里面安装——你需要但还没安装的“插件”(因为,本身已有很多插件,一般情况可以满足你的需要),最好不要用通过第三方公司进行过集成的 eclipse,比如Lomboz(它的内核也是eclipse,如果你需要Lomboz,则可以把它作为一个插件安装到eclipse中),这样,可以直接感觉eclipse的架构和魅力。
最后,要说的就是,在第一个阶段不要停留太久,要在适当的时候过渡到eclipse;所有上面是一个过程。
Ⅳ java记事本怎么写呀
下面是一个简单的记事本程序:
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
import java.awt.datatransfer.*;//数据传送
import java.util.*;
public class Notepad
{public static void main(String args[])
{MyMenuFrame mf=new MyMenuFrame();//定义主类
mf.setSize(new Dimension(300,200));//用setSize()方法指定窗口的初始大小
mf.setVisible(true);
}
}
class MyMenuFrame extends Frame implements ActionListener//定义窗口
{
Clipboard clipboard;//定义剪切板对象
FileDialog filedialog_save,filedialog_load;//定义文件对话框
MenuBar m_MenuBar;//定义菜单条
Menu menuFile,menuEdit,menuStyle,menuSearch,menuHelp;//定义菜单项
MenuItem mi_File_New,mi_File_Open,mi_File_Save,mi_file_Savaas,mi_File_Close,mi_File_Exit,
mi_Edit_Ce,mi_Edit_Copy,mi_Edit_Cut,mi_Edit_Paste,
mi_Edit_Style,mi_Style_Font,mi_Style_Auto,
mi_Search_sphere,
mi_Help_Helptopic,mi_Help_About;
TextArea text;
MyMenuFrame( )//构造方法
{
super("记事本");//指定窗口标题
text=new TextArea(20,20);
add(text);
clipboard = null;
clipboard = getToolkit().getSystemClipboard();//获取系统剪切板
filedialog_save=new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
filedialog_save.setVisible(false);
filedialog_load=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
filedialog_load.setVisible(false);
m_MenuBar=new MenuBar( );
menuFile=new Menu("文件");//创建菜单项,创建菜单子项并初始化
MenuItem mi_File_New=new MenuItem("新建");
MenuItem mi_File_Open=new MenuItem("打开");
MenuItem mi_File_Save=new MenuItem("保存");
MenuItem mi_File_Saveas=new MenuItem("另存为");
MenuItem mi_File_Close=new MenuItem("关闭");
MenuItem mi_File_Exit=new MenuItem(" 退出");
mi_File_Exit.setShortcut(new MenuShortcut(′x′));//设置快捷键
mi_File_Open.setActionCommand("打开");//简化
mi_File_New.setActionCommand("新建");
mi_File_Exit.setActionCommand("退出");
mi_File_New.addActionListener(this);//使菜单子项响应动作事件
mi_File_Open.addActionListener(this);
mi_File_Save.addActionListener(this);
mi_File_Saveas.addActionListener(this);
mi_File_Close.addActionListener(this);
mi_File_Exit.addActionListener(this);
menuFile.add(mi_File_New);//把菜单子项加入菜单项
menuFile.add(mi_File_Open);
menuFile.add(mi_File_Save);
menuFile.add(mi_File_Saveas);
menuFile.add(mi_File_Close);
menuFile.addSeparator();//加一条横向分割线
menuFile.add(mi_File_Exit);
m_MenuBar.add(menuFile);//把菜单项加入菜单条
menuEdit=new Menu("编辑");
mi_Edit_Ce=new MenuItem("撤消");
mi_Edit_Copy=new MenuItem("复制");
mi_Edit_Cut=new MenuItem("剪切");
mi_Edit_Paste=new MenuItem("粘贴");
mi_Edit_Copy.setActionCommand("复制");
mi_Edit_Cut.setActionCommand("剪切");
mi_Edit_Paste.setActionCommand("粘贴");
mi_Edit_Ce.addActionListener(this);
mi_Edit_Copy.addActionListener(this);
mi_Edit_Cut.addActionListener(this);
mi_Edit_Paste.addActionListener(this);
menuEdit.add(mi_Edit_Ce);
menuEdit.add(mi_Edit_Copy);
menuEdit.add(mi_Edit_Cut);
menuEdit.add(mi_Edit_Paste);
m_MenuBar.add(menuEdit);
menuStyle=new Menu("格式");
mi_Style_Auto=new MenuItem("自动换行");
mi_Style_Font=new MenuItem("字体");
mi_Style_Auto.addActionListener(this);
mi_Style_Font.addActionListener(this);
menuStyle.add(mi_Style_Auto);
menuStyle.add(mi_Style_Font);
m_MenuBar.add(menuStyle);
menuSearch=new Menu("查看");
mi_Search_sphere=new MenuItem("状态栏");
mi_Search_sphere.addActionListener(this);
menuSearch.add(mi_Search_sphere);
m_MenuBar.add(menuSearch);
menuHelp=new Menu("帮助");
mi_Help_Helptopic=new MenuItem("帮助主题");
mi_Help_About=new MenuItem("关于记事本");
mi_Help_Helptopic.addActionListener(this);
mi_Help_About.addActionListener(this);
menuHelp.add(mi_Help_Helptopic);
menuHelp.add(mi_Help_About);
m_MenuBar.add(menuHelp);
setMenuBar(m_MenuBar);//把菜单条加入到菜单
addWindowListener(new WindowAdapter()//监听窗口(关闭窗口)
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setVisible(true);
validate(); //如果加载了其他东西,就可用它来显示组件
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="新建")
{
text.setText(null);//清空
}
if(e.getActionCommand()=="打开")
{
filedialog_load.setVisible(true);//使文件对话框可见
}
if(e.getActionCommand()=="保存")
{
filedialog_save.setVisible(true);//使文件对话框可见
}
if(e.getActionCommand() == "复制")
{
String s = text.getSelectedText();//复制到剪切板
StringSelection text1= new StringSelection(s);//拖动鼠标选取文本
clipboard.setContents(text1, null);
}
if(e.getActionCommand() == "剪切")
{
String s1 = text.getSelectedText();//剪切到剪切板
StringSelection text1 = new StringSelection(s1);//拖动鼠标选取文本
clipboard.setContents(text1, null);
int j = text.getSelectionStart();
int k = text.getSelectionEnd();
text.replaceRange("", j, k);//从Text中删除被选取文本
}
if(e.getActionCommand() =="粘贴")//从剪切板粘贴数据
{
Transferable transferable = clipboard.getContents(this);
DataFlavor dataflavor = DataFlavor.stringFlavor;
if(transferable.isDataFlavorSupported(dataflavor))
{
try//处理异常事件的发生(类型不符合)
{
String s3 = (String)transferable.getTransferData(dataflavor);
text.insert(s3, text.getCaretPosition());
}
catch(Exception e1) { }
}
}
if(e.getActionCommand()=="退出")
{
dispose();
System.exit(0);
}
}
}