JAVA课程设计 记事本

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ta.cut();
checkMenuItemEnabled();
} else if (e.getSource() == ecopy ) {
ta.requestFocusInWindow();
ta.copy();
checkMenuItemEnabled();
saveFile(ta);
}else if (e.getSource() == fexit) {
ta.requestFocusInWindow();
System.exit(0);
}else if (e.getSource() == ecut ) {
ta.requestFocusInWindow();
JMenuBar bar;
JMenu file , edit, help;
JMenuItem fnew , fopen, fsave, fexit;
JMenuItem ecut, ecopy, epaste;
JMenuItem mh1,mh2;
Toolkit toolKit = Toolkit.getDefaultToolkit();
Clipboard clipBoard = toolKit.getSystemClipboard();
String oldValue;
Notepad(){
mh1 = new JMenuItem("关于记事本");
mh1.addActionListener(this);
help.add(mh1);
bar.add(file);
bar.add(edit);
bar.add(help);
int s = JOptionPane.showOptionDialog(null, "请注意保存文档!按“确定”新建文件",
"警告!", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
p1 = new JPanel();
p1.setLayout(new BorderLayout());
bar = new JMenuBar();
file = new JMenu("文件");
fnew = new JMenuItem("新建");
statusLabel.setText("没有选择任何文件");
return;
}
File saveFileName = fileChooser.getSelectedFile();
if (saveFileName == null || saveFileName.getName().equals(""))
fr.read(buffer, 0, len);
fr.close();
text.setText(new String(buffer));
} catch (Exception e) {
e.printStackTrace();
}
}
p1.add(statusLabel, BorderLayout.SOUTH);
this.addWindowListener(new Win());
add(p1);
setVisible(true);
setSize(400,500);
}
ecopy.addActionListener(this);
ecut.addActionListener(this);
epaste.addActionListener(this);
help = new JMenu("帮助");
+ Integer.toString(ta.getColumns()));
ta.setCaretPosition(0);
}
} else if (e.getSource() == fsave ) {
ta.requestFocusInWindow();
void saveFile(JTextArea text) {
ta.requestFocusInWindow();
if (isNewfile) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new JAVAFileFilter("txt"));
fopen = new JMenuItem("打开");
fsave = new JMenuItem("保存");
fexit = new JMenuItem("退出");
file.add(fnew);
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fnew ) {//新建
ta.requestFocusInWindow();
Object[] options = { "确定", "取消" };
import java.awt.event.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import javax.swing.event.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("另存为");
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION) {
} else if (e.getSource() == epaste) {
ta.requestFocusInWindow();
ta.paste();
checkMenuItemEnabled();
}else if (e.getSource() == mh1) {
helpFrame();
int returnVal = filechooser.showOpenDialog(Notepad.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file2 = filechooser.getSelectedFile();
fopen.addActionListener(this);
fsave.addActionListener(this);
fexit.addActionListener(this);
edit = new JMenu("编辑");
ecut = new JMenuItem("剪切");
ta.requestFocusInWindow();
if (file != null)
filechooser.setSelectedFile(file2);
filechooser.addChoosableFileFilter(new JAVAFileFilter("txt"));
if (s == JOptionPane.YES_OPTION)
ta.setText("");
isNewfile = true;
statusLabel.setText("空白文档");
file2 = null;
}else if (e.getSource() == fopen) {
file.add(fopen);
file.add(fsave);
file.addSeparator();
file.add(fexit);
fnew.addActionListener(this);
JTextArea ta;
JLabel statusLabel;
File file2 = nrue;
JPanel p1;
JFileChooser filechooser = new JFileChooser();
}
}
void openFile(JTextArea text) {
try {
FileReader fr = new FileReader(file2);
int len = (int) file2.length();
char[] buffer = new char[len];
setJMenuBar(bar);
ta = new JTextArea();
ta.addMouseListener(this);
p1.add(ta,BorderLayout.CENTER);
statusLabel = new JLabel("按Ctrl+F1获取帮助");
import javax.swing.undo.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
public class Notepad extends JFrame implements ActionListener,MouseListener{
openFile(ta);
isNewfile = false;
statusLabel.setText("当前打开文件:" + file2.getAbsoluteFile()
+ " 行:" + Integer.toString(ta.getRows()) + " 列:"
ecopy = new JMenuItem("复制");
epaste = new JMenuItem("粘贴");
edit.add(ecut);
edit.add(ecopy);
edit.add(epaste);
相关文档
最新文档