package texteditor4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import javax.swing.event.*;
import com.borland.dbswing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class TextEditor4
extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JMenuBar jMenuBar1 = new JMenuBar();
JToolBar jToolBar1 = new JToolBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
JMenuItem jMenuItem5 = new JMenuItem();
JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
String filename; //文件名
boolean isSaved = false;
Document document;
JFileChooser jFileChooser1 = new JFileChooser();
JMenu jMenu2 = new JMenu();
JMenuItem jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem();
FontChooser fontChooser1 = new FontChooser();
JMenuItem jMenuItem8 = new JMenuItem();
JButton jButton1 = new JButton();
ImageIcon imageIcon1;
ImageIcon imageIcon2;
ImageIcon imageIcon3;
ImageIcon imageIcon4;
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JMenu jMenu3 = new JMenu();
JMenuItem jMenuItem9 = new JMenuItem();
//Construct the frame
public TextEditor4() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
imageIcon1 = new ImageIcon(TextEditor4.class.getResource("new.gif"));
imageIcon2 = new ImageIcon(TextEditor4.class.getResource("open.gif"));
imageIcon3 = new ImageIcon(TextEditor4.class.getResource("save.gif"));
imageIcon4 = new ImageIcon(TextEditor4.class.getResource("saveas.gif"));
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout1);
document = jTextArea1.getDocument();
this.setSize(new Dimension(400, 300));
this.setTitle("记事本");
jMenu1.setText("文件");
jMenuItem1.setText("新建");
jMenuItem1.addActionListener(new TextEditor4_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("打开");
jMenuItem2.addActionListener(new TextEditor4_jMenuItem2_actionAdapter(this));
jMenuItem3.setText("保存");
jMenuItem3.addActionListener(new TextEditor4_jMenuItem3_actionAdapter(this));
jMenuItem4.setText("另存为");
jMenuItem4.addActionListener(new TextEditor4_jMenuItem4_actionAdapter(this));
jMenuItem5.setText("退出");
jMenuItem5.addActionListener(new TextEditor4_jMenuItem5_actionAdapter(this));
jTextArea1.setText("");
jLabel1.setToolTipText("");
jLabel1.setText(" ");
document.addDocumentListener(new TextEditor4_document_documentAdapter(this));
jMenu2.setText("设置");
jMenuItem6.setText("设置前景色");
jMenuItem6.addActionListener(new TextEditor4_jMenuItem6_actionAdapter(this));
jMenuItem7.setText("设置背景色");
jMenuItem7.addActionListener(new TextEditor4_jMenuItem7_actionAdapter(this));
jMenuItem8.setText("设置字体");
jMenuItem8.addActionListener(new TextEditor4_jMenuItem8_actionAdapter(this));
jButton1.setToolTipText("new file");
jButton1.setIcon(null);
jButton1.setSelectedIcon(null);
jButton1.setText("");
jButton1.addActionListener(new TextEditor4_jButton1_actionAdapter(this));
jButton2.setToolTipText("open file");
jButton2.setVerifyInputWhenFocusTarget(true);
jButton2.setText("");
jButton2.addActionListener(new TextEditor4_jButton2_actionAdapter(this));
jButton3.setToolTipText("save");
jButton3.setText("");
jButton3.addActionListener(new TextEditor4_jButton3_actionAdapter(this));
jButton4.setToolTipText("save as");
jButton4.setText("");
jButton4.addActionListener(new TextEditor4_jButton4_actionAdapter(this));
jButton1.setIcon(imageIcon1);
jButton2.setIcon(imageIcon2);
jButton3.setIcon(imageIcon3);
jButton4.setIcon(imageIcon4);
jMenu3.setText("帮助");
jMenuItem9.setText("关于");
jMenuItem9.addActionListener(new TextEditor4_jMenuItem9_actionAdapter(this));
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem3);
jMenu1.add(jMenuItem4);
jMenu1.addSeparator();
jMenu1.add(jMenuItem5);
contentPane.add(jLabel1, BorderLayout.SOUTH);
contentPane.add(jScrollPane1, BorderLayout.CENTER);
contentPane.add(jToolBar1, BorderLayout.NORTH);
jToolBar1.add(jButton1, null);
jToolBar1.add(jButton2, null);
jToolBar1.add(jButton3, null);
jToolBar1.add(jButton4, null);
jScrollPane1.getViewport().add(jTextArea1, null);
jMenu2.add(jMenuItem6);
jMenu2.add(jMenuItem7);
jMenu2.addSeparator();
jMenu2.add(jMenuItem8);
jMenu3.add(jMenuItem9);
this.setJMenuBar(jMenuBar1);
fontChooser1.setFrame(this);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void newFile() { //创建新的文本文件
if (toExit()) {
jTextArea1.setText("");
filename = null;
isSaved = false;
updateFileName();
jLabel1.setText("新建文件:" + filename);
}
}
boolean save() { //保存文件
if (filename == null) { //如果文件名为空,则调用saveAs()方法
return saveAs();
}
try {
File file = new File(filename); //新建文件对象
FileWriter out = new FileWriter(file);
String text = jTextArea1.getText();
out.write(text); //写文件
out.close();
this.isSaved = false; //将文件状态设为已保存
jLabel1.setText("保存文件:" + filename);
updateFileName(); //更新文件标题
return true;
}
catch (Exception e) {
jLabel1.setText("错误" + e);
}
return false;
}
boolean saveAs() { //文件另存为
if (JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)) {
filename = jFileChooser1.getSelectedFile().getPath(); //获得文件路径
return save(); //调用save()方法保存文件
}
else {
return false;
}
}
void updateFileName() { //更新标题文件名
String str;
if (filename == null) { //如果文件名为空,则标题名为Untitled
str = "Untitled";
}
else {
str = filename;
}
if (isSaved) {
str = "记事本-" + str;
}
this.setTitle(str); //设置标题名
this.repaint();
}
boolean toExit() { //是否能关闭当前文件
if (!isSaved) { //如果文件没有改变则返回true
return true;
}
int i = JOptionPane.showConfirmDialog(this, "是否保存文档?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION);
switch (i) {
case JOptionPane.YES_OPTION: //如选择yes,则保存文件
return save();
case JOptionPane.NO_OPTION: //选择no,则不保存文件,退出
return true;
case JOptionPane.CANCEL_OPTION: //选择cancel,则退回原理状态
return false;
default:
return false;
}
}
void openFile() { //打开文件
if (!toExit()) { //检查是否能关闭现在的文件
return;
}
if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
try {
String filename1 = jFileChooser1.getSelectedFile().getPath(); //获取文件路径
File file = new File(filen
没有合适的资源?快使用搜索试试~ 我知道了~
Java制作记事本提高(复杂的记事本)及一些复杂功能实现代码
共31个文件
class:18个
gif:8个
java:3个
需积分: 16 14 下载量 34 浏览量
2009-10-20
17:09:51
上传
评论
收藏 31KB RAR 举报
温馨提示
ava制作记事本提高是一个类似与windows 操作系统中写字板一样 具有一些相似的功能 对初学者来说值得看下 及对那些提高者来说可以尝试该进下
资源推荐
资源详情
资源评论
收起资源包目录
2.5.4--记事本提高(复杂的记事本).rar (31个子文件)
2.5.4--记事本提高(复杂的记事本)
src
texteditor4
AboutDialog.java 2KB
new.gif 139B
Application1.java 1KB
save.gif 184B
Thumbs.db 7KB
TextEditor4.java 16KB
SaveAs.gif 348B
open.gif 146B
TextEditor4.jpx 3KB
classes
texteditor4
TextEditor4_jButton1_actionAdapter.class 707B
AboutDialog_jButton1_actionAdapter.class 707B
TextEditor4.class 13KB
TextEditor4_jMenuItem7_actionAdapter.class 713B
TextEditor4_document_documentAdapter.class 978B
TextEditor4_jButton4_actionAdapter.class 707B
TextEditor4_jMenuItem5_actionAdapter.class 713B
TextEditor4_jMenuItem1_actionAdapter.class 713B
TextEditor4_jMenuItem3_actionAdapter.class 713B
new.gif 139B
TextEditor4_jMenuItem8_actionAdapter.class 713B
TextEditor4_jButton3_actionAdapter.class 707B
save.gif 184B
TextEditor4_jMenuItem9_actionAdapter.class 713B
TextEditor4_jButton2_actionAdapter.class 707B
AboutDialog.class 2KB
SaveAs.gif 348B
Application1.class 1KB
TextEditor4_jMenuItem6_actionAdapter.class 713B
TextEditor4_jMenuItem2_actionAdapter.class 713B
TextEditor4_jMenuItem4_actionAdapter.class 713B
open.gif 146B
共 31 条
- 1
资源评论
wangxinfeng1990
- 粉丝: 23
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功