本页内容为:我写的JAVA记事本,该文章本站转载自网络,如有侵权请告之,我们将会及时将其删除,其正文内容如下:
package untitled1;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.JTextArea;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JCheckBoxMenuItem;
import java.awt.FileDialog;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.UndoableEditListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.UndoManager;
import javax.swing.undo.*;
import javax.swing.text.*;
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.*;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Container;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Notebook extends JFrame implements ActionListener,UndoableEditListener{
JTextArea jTextArea = new JTextArea();//文本编辑域
JMenuBar jMenuBar = new JMenuBar();//菜单栏
JMenu jMenuF = new JMenu("文件(F)");//文件菜单
JMenu jMenuE = new JMenu("编辑(E)");//编辑菜单
JMenu jMenuO = new JMenu("格式(O)");//格式菜单
JMenu jMenuV = new JMenu("查看(V)");//查看菜单
JMenu jMenuH = new JMenu("帮助(H)");//帮助菜单
//文件菜单子菜单项
JMenuItem jFnew = new JMenuItem("新建(N)");
int iFrameWidth = 600;//记事本宽
int iFrameHeight = 400;//记事本高
String srcCurFile = null;//当前编辑文件
String strFileContext = this.jTextArea.getText();//当前文件内容
int fontstyle = 0;//字形
int fontsize = 0;//字体大小
UndoManager undoManager = new UndoManager();//撤销管理器
int iStart = 0;//查找起始位置
int iEnd = 0;//查找结束位置
String strFind = null;//查找的字符串
JScrollPane jScrollPane = new JScrollPane(this.jTextArea);//滚动条
public Notebook()
{
this.jMenuBar.add(this.jMenuF);
this.jMenuBar.add(this.jMenuE);
this.jMenuBar.add(this.jMenuO);
this.jMenuBar.add(this.jMenuV);
this.jMenuBar.add(this.jMenuH);
this.jMenuF.add(this.jFnew);
this.jMenuF.add(this.jFopen);
this.jMenuF.add(this.jFsave);
this.jMenuF.add(this.jFsaveAs);
this.jMenuF.addSeparator();
this.jMenuF.add(this.jFset);
this.jMenuF.add(this.jFprint);
this.jFset.setEnabled(false);
this.jFprint.setEnabled(false);
this.jMenuF.addSeparator();
this.jMenuF.add(this.jFexit);
this.jFnew.addActionListener(this);
this.jFopen.addActionListener(this);
this.jFsave.addActionListener(this);
this.jFsaveAs.addActionListener(this);
this.jFset.addActionListener(this);
this.jFprint.addActionListener(this);
this.jFexit.addActionListener(this);
this.jMenuE.add(this.jEundo);
this.jMenuE.addSeparator();
this.jMenuE.add(this.jEcut);
this.jMenuE.add(this.jEcopy);
this.jMenuE.add(this.jEpaste);
this.jMenuE.add(this.jEdel);
this.jMenuE.addSeparator();
this.jMenuE.add(this.jEfind);
this.jMenuE.add(this.jEfindNext);
this.jMenuE.add(this.jEreplace);
this.jMenuE.add(this.jEgoto);
this.jEgoto.setEnabled(false);
this.jMenuE.addSeparator();
this.jMenuE.add(this.jEselectAll);
this.jMenuE.add(this.jEdata);
this.jEundo.addActionListener(this);
this.jEcut.addActionListener(this);
this.jEcopy.addActionListener(this);
this.jEpaste.addActionListener(this);
this.jEdel.addActionListener(this);
this.jEfind.addActionListener(this);
this.jEfindNext.addActionListener(this);
this.jEreplace.addActionListener(this);
this.jEgoto.addActionListener(this);
this.jEselectAll.addActionListener(this);
this.jEdata.addActionListener(this);
this.jMenuO.add(this.jCheckBoxMenuItem);
this.jMenuO.add(this.jOfont);
this.jCheckBoxMenuItem.addActionListener(this);
this.jOfont.addActionListener(this);
this.jMenuV.add(this.jVbar);
this.jVbar.addActionListener(this);
this.jVbar.setEnabled(false);
this.jMenuH.add(this.jHhelp);
this.jMenuH.addSeparator();
this.jMenuH.add(this.jHabout);
this.jHhelp.setEnabled(false);
this.jHhelp.addActionListener(this);
this.jHabout.addActionListener(this);
//可以自动换行
this.jTextArea.setLineWrap(true);
this.jTextArea.setWrapStyleWord(true);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
this.setLocation((screenSize.width-this.iFrameWidth)>>1,
(screenSize.height-this.iFrameHeight)>>1);//设置记事本在屏幕中央显示
this.setJMenuBar(this.jMenuBar);
this.setTitle("无标题 - 记事本");
this.getContentPane().add(this.jScrollPane);
this.setSize(this.iFrameWidth,this.iFrameHeight);
this.setVisible(true);
this.jTextArea.getDocument().addUndoableEditListener(this);//撤销侦听
}
else if(e.getActionCommand().equals("转到(G)"))
{
final JFrame frame = new JFrame("转到下列行");
final JTextField field = new JTextField(10);
field.setSize(100,50);
JLabel label = new JLabel("转到的行数");
JButton ok = new JButton("确定");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
jComboBoxFontSize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtf3.setText((String) jComboBoxFontSize.getSelectedItem());
if(jComboBoxFontSize.getSelectedItem().equals("14"))
{
fontsize = 14;
}
else if(jComboBoxFontSize.getSelectedItem().equals("18"))
{
fontsize = 18;
}
else if(jComboBoxFontSize.getSelectedItem().equals("22"))
{
fontsize = 22;
}
}
});
final JComboBox jComboBoxFont = new JComboBox();
jComboBoxFont.addItem("宋体");
jComboBoxFont.addItem("黑体");
jComboBoxFont.addItem("美体");
jComboBoxFont.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtf1.setText((String) jComboBoxFont.getSelectedItem());
}
});
JLabel jl1 = new JLabel("字体:");
JLabel jl2 = new JLabel("字形:");
JLabel jl3 = new JLabel("大小:");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jDialog.getContentPane().setLayout(gbl);
gbc.gridx = 5;
gbc.gridy = 2;
jDialog.getContentPane().add(jButtonOk, gbc);
jButtonOk.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("确定"))
{
Font f = new Font("字体设置", fontstyle, fontsize);
jTextArea.setFont(f);
jDialog.dispose();
}
}
});
jButtonExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("取消"))
{
jDialog.dispose();
}
}
else if(e.getActionCommand().equals("关于记事本(A)"))
{
JOptionPane.showMessageDialog(null, "sld开发得java记事本程序,可供学习参考。",
"警告",JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* 可撤消的编辑操作记录器
* @param e UndoableEditEvent
*/
public void undoableEditHappened(UndoableEditEvent e)
{
this.undoManager.addEdit(e.getEdit());
}
/**
* 保存文件方法
*/
private void save()
{
FileDialog fileDialog = null;
String strPath = "";
String strFileName = "";
if(this.srcCurFile != null)
{//文件已保存过
File file = new File(this.srcCurFile);
&n