我想用java编写windows记事本的新建功能怎么写?要有代码演示。

 我来答
鼻翼泪滴
2011-03-17 · 超过20用户采纳过TA的回答
知道答主
回答量:71
采纳率:0%
帮助的人:37.7万
展开全部
这是一个我以前写的简单的记事本,里面有新建,保存,另存,打开等功能,但是只是逻辑最简单的那种,你看看吧,希望对你有帮助;

import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
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.JScrollPane;
import javax.swing.JTextArea;

public class NotePad {
public static void main(String[] args) {
NotePadFrame notPadFrame = new NotePadFrame();
notPadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
notPadFrame.setVisible(true);
}
}

class NotePadFrame extends JFrame {
private JMenu jmb, jmb1;
private JMenuBar Jmenu = new JMenuBar();
private JMenuItem fm, fm1, fm2, fm3, fm4, fe1, fe2, fe3, fe4;
String fileName, copy, paste, cut;
NotePadPanel notePadPanel = new NotePadPanel(this);
private NotePadFrame f;

public NotePadFrame() {
jmb = new JMenu("文件");
this.setJMenuBar(Jmenu);
fm = new JMenuItem("新建");
jmb.add(fm);
jmb.addSeparator();
// 新建
fm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == fm) {
if (!(notePadPanel.getTa().getText()).equals("")) {
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(null,
"你是否保存", "提示", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (response == 0) {
FileDialog d = new FileDialog(f, "保存文件",
FileDialog.SAVE);
d.setVisible(true);
fileName = d.getDirectory() + d.getFile();
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");

byte[] bb = notePadPanel.getTa().getText()
.getBytes();

fout.write(bb);
// 关闭
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
notePadPanel.getTa().setText("");
}
if (response == 1) {
JOptionPane.showMessageDialog(null, "你选择了取消");
notePadPanel.getTa().setText("");
}
}
}
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
});

fm1 = new JMenuItem("打开");
jmb.add(fm1);
jmb.addSeparator();
// 打开文件
fm1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == fm1) {
FileDialog d = new FileDialog(f, "打开文件",
FileDialog.LOAD);
d.setVisible(true);
File file = new File(d.getDirectory() + d.getFile());
for (int i = 0; i <= file.length(); i++) {
char[] ch = new char[1024];
FileReader fr = new FileReader(file);
fr.read(ch);
String str = new String(ch);
notePadPanel.getTa().setText(str);
}
}
} catch (IOException e3) {
System.out.println(e3.getMessage());
}

}

});

fm2 = new JMenuItem("保存");
jmb.add(fm2);
jmb.addSeparator();
// 保存文件
fm2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == fm2) {
if (fileName == null) {
fileName = JOptionPane.showInputDialog("请输入文件名",
"java");
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");

byte[] bb = notePadPanel.getTa().getText()
.getBytes();

fout.write(bb);
// 关闭
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
} else {
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");

byte[] bb = notePadPanel.getTa().getText()
.getBytes();

fout.write(bb);
// 关闭
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
}
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}

}

});

fm3 = new JMenuItem("另存为");
jmb.add(fm3);
jmb.addSeparator();

// 另存为
fm3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fm3) {
try {
FileDialog d = new FileDialog(f, "另存为", FileDialog.SAVE);
d.setVisible(true);
fileName = d.getDirectory() + d.getFile();
FileOutputStream fout = new FileOutputStream(fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText().getBytes();

fout.write(bb);
// 关闭
fout.close();
} catch (Exception e4) {
System.out.println(e4.getMessage());
}
}
}
});
fm4 = new JMenuItem("关闭");
jmb.add(fm4);
fm4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fm4) {
System.exit(0);
}
}
});
jmb1 = new JMenu("编辑");
fe1 = new JMenuItem("复制");
jmb1.add(fe1);
jmb1.addSeparator();
fe1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fe1) {
copy = notePadPanel.getTa().getSelectedText();
}
}
});
fe2 = new JMenuItem("粘贴");
jmb1.add(fe2);
jmb1.addSeparator();
fe2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fe2) {
System.out.println("copy="+copy);
notePadPanel.getTa().setText(copy);
}
}

});
fe3 = new JMenuItem("剪切");
jmb1.add(fe3);
jmb1.addSeparator();
fe3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (e.getSource() == fe3) {
copy = notePadPanel.getTa().getSelectedText();
notePadPanel.getTa().setText("");
}
}

});
fe4 = new JMenuItem("版本");
jmb1.add(fe4);

fe4.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
if(e.getSource()==fe4){
JOptionPane.showMessageDialog(f, "NotePad 1.0");
}

}

});
Jmenu.add(jmb);
Jmenu.add(jmb1);

}
}

class NotePadPanel extends JPanel {
private JButton jb1, jb;
private JTextArea ta;
String fileName;
private JScrollPane jsp;

public JTextArea getTa() {
return ta;
}

public void setTa(JTextArea ta) {
this.ta = ta;
}

public NotePadPanel(NotePadFrame notePadFrame) {
ta = new JTextArea();
ta.setWrapStyleWord(true);

jsp = new JScrollPane(ta);

jb = new JButton("保存");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == jb) {
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 e1) {
System.out.println(e1.getMessage());
}

}

});
jb1 = new JButton("关闭");
jb1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
System.exit(0);
}
}
});
this.add(jb);
this.add(jb1);
notePadFrame.add(this, "South");
notePadFrame.setSize(600, 400);
notePadFrame.add(jsp);
notePadFrame.setTitle("记事本");

int W = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int H = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
notePadFrame.setLocation((W - notePadFrame.getWidth()) / 2,
(H - notePadFrame.getHeight()) / 2);

}
}
幽冥神者
2011-03-17 · TA获得超过3213个赞
知道大有可为答主
回答量:1953
采纳率:50%
帮助的人:1778万
展开全部
新建功能啊?点新建,判断一下当前状态,没保存就提示保存,已保存过的就直接覆盖原文件,如果没修改过就直接新建了。新建的过程就是把原来的文字什么全干掉,各种标志变量回归初始化状态,等等。代码就算了,都不够忙活的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式