Java记事本源代码(完整)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/**
* 作品:记事本
* 作者:***
* 功能:简单的文字编辑
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
class NotePad extends JFrame{
private JMenuBar menuBar;
private JMenu fielMenu,editMenu,formMenu,aboutMenu;
private JMenuItem
newMenuItem,openMenuItem,saveMenuItem,exitMenuItem;
private JMenuItem
cutMenuItem,copyMenuItem,pasteMenuItem,foundItem,replaceItem,s electAll;
private JMenuItem font,about;
private JTextArea textArea;
private JFrame foundFrame,replaceFrame;
private JCheckBoxMenuItem wrapline;
private JTextField textField1=new JTextField(15);
private JTextField textField2=new JTextField(15);
private JButton startButton,replaceButton,reallButton;
int start=0;
String value;
File file=null;
JFileChooser fileChooser=new JFileChooser();
boolean wrap=false;
public NotePad(){
//创建文本域
textArea=new JTextArea();
add(new JScrollPane(textArea),BorderLayout.CENTER);
//创建文件菜单及文件菜单项
fielMenu=new JMenu("文件");
fielMenu.setFont(new Font("微软雅黑",0,15));
newMenuItem=new JMenuItem("新建",new
ImageIcon("icons\\new24.gif"));
newMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_N,InputEvent.CTRL_MASK));
newMenuItem.addActionListener(listener);
openMenuItem=new JMenuItem("打开",new
ImageIcon("icons\\open24.gif"));
openMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_O,InputEvent.CTRL_MASK));
openMenuItem.addActionListener(listener);
saveMenuItem=new JMenuItem("保存",new
ImageIcon("icons\\save.gif"));
saveMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_S,InputEvent.CTRL_MASK));
saveMenuItem.addActionListener(listener);
exitMenuItem=new JMenuItem("退出",new
ImageIcon("icons\\exit24.gif"));
exitMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_E,InputEvent.CTRL_MASK));
exitMenuItem.addActionListener(listener);
//创建编辑菜单及菜单项
editMenu=new JMenu("编辑");
editMenu.setFont(new Font("微软雅黑",0,15));
cutMenuItem=new JMenuItem("剪切",new
ImageIcon("icons\\cut24.gif"));
cutMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_X,InputEvent.CTRL_MASK));
cutMenuItem.addActionListener(listener);
copyMenuItem=new JMenuItem("复制",new
ImageIcon("icons\\copy24.gif"));
copyMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent .VK_C,InputEvent.CTRL_MASK));
copyMenuItem.addActionListener(listener);
pasteMenuItem=new JMenuItem("粘贴",new
ImageIcon("icons\\paste24.gif"));
pasteMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13));
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEven t.VK_V,InputEvent.CTRL_MASK));
pasteMenuItem.addActionListener(listener);
foundItem=new JMenuItem("查找");
foundItem.setFont(new Font("微软雅黑",Font.BOLD,13));
foundItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK _F,InputEvent.CTRL_MASK));
foundItem.addActionListener(listener);
replaceItem=new JMenuItem("替换");
replaceItem.setFont(new Font("微软雅黑",Font.BOLD,13));
replaceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent. VK_R,InputEvent.CTRL_MASK));
replaceItem.addActionListener(listener);
selectAll=new JMenuItem("全选");
selectAll.setFont(new Font("微软雅黑",Font.BOLD,13));
selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK _A,InputEvent.CTRL_MASK));
selectAll.addActionListener(listener);
//创建格式菜单及菜单项
formMenu=new JMenu("格式");
formMenu.setFont(new Font("微软雅黑",0,15));
wrapline=new JCheckBoxMenuItem("自动换行");
wrapline.setFont(new Font("微软雅黑",Font.BOLD,13));
wrapline.addActionListener(listener);
wrapline.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(wrapline.isSelected()){
textArea.setLineWrap(true);
}
else
textArea.setLineWrap(false);
}
});
font=new JMenuItem("字体");
font.setFont(new Font("微软雅黑",Font.BOLD,13)); font.addActionListener(listener);
//创建关于菜单
aboutMenu=new JMenu("关于");
aboutMenu.setFont(new Font("微软雅黑",0,15)); about=new JMenuItem("记事本……");
about.setFont(new Font("微软雅黑",Font.BOLD,13)); about.addActionListener(listener);
//添加文件菜单项
fielMenu.add(newMenuItem);
fielMenu.add(openMenuItem);
fielMenu.add(saveMenuItem);
fielMenu.addSeparator();
fielMenu.add(exitMenuItem);
//添加编辑菜单项
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
editMenu.add(foundItem);
editMenu.add(replaceItem);
editMenu.addSeparator();
editMenu.add(selectAll);
//添加格式菜单项
formMenu.add(wrapline);
formMenu.add(font);
//添加关于菜单项
aboutMenu.add(about);
//添加菜单
menuBar=new JMenuBar();
menuBar.add(fielMenu);
menuBar.add(editMenu);
menuBar.add(formMenu);
menuBar.add(aboutMenu);
setJMenuBar(menuBar);
//创建两个框架,用作查找和替换
foundFrame=new JFrame();
replaceFrame=new JFrame();
//创建两个文本框
textField1=new JTextField(15);
textField2=new JTextField(15);
startButton=new JButton("开始");
startButton.addActionListener(listener);
replaceButton=new JButton("替换为");
replaceButton.addActionListener(listener);
reallButton=new JButton("全部替换");
reallButton.addActionListener(listener);
}
//创建菜单项事件监听器
ActionListener listener=new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name=e.getActionCommand();
if(e.getSource() instanceof JMenuItem){
if("新建".equals(name)){
textArea.setText("");
file=null;
}
if("打开".equals(name)){
if(file!=null){
fileChooser.setSelectedFile(file);
}
int
returnVal=fileChooser.showOpenDialog(NotePad.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fileChooser.getSelectedFile();
}
try{
FileReader reader=new FileReader(file);
int len=(int)file.length();
char[] array=new char[len];
reader.read(array,0,len);
reader.close();
textArea.setText(new String(array));
}
catch(Exception e_open){
e_open.printStackTrace();
}
}
}
if("保存".equals(name)){
if(file!=null){
fileChooser.setSelectedFile(file);
}
int
returnVal=fileChooser.showSaveDialog(NotePad.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fileChooser.getSelectedFile();
}
try{
FileWriter writer=new FileWriter(file);
writer.write(textArea.getText());
writer.close();
}
catch (Exception e_save) {
e_save.getStackTrace();
}
}
if("退出".equals(name)){
System.exit(0);
}
if("剪切".equals(name)){
textArea.cut();
}
if("复制".equals(name)){
textArea.copy();
}
if("粘贴".equals(name)){
textArea.paste();
}
if("查找".equals(name)){
value=textArea.getText();
foundFrame.add(textField1,BorderLayout.CENTER);
foundFrame.add(startButton,BorderLayout.SOUTH);
foundFrame.setLocation(300,300);
foundFrame.setTitle("查找");
foundFrame.pack();
foundFrame.setVisible(true);
foundFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
}
if("替换".equals(name)){
value=textArea.getText();
JLabel label1=new JLabel("查找内容:");
JLabel label2=new JLabel("替换为:");
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(2,2));
JPanel panel2=new JPanel();
panel2.setLayout(new GridLayout(1,3));
replaceFrame.add(panel1,BorderLayout.NORTH);
replaceFrame.add(panel2,BorderLayout.CENTER);
panel1.add(label1);
panel1.add(textField1);
panel1.add(label2);
panel1.add(textField2);
panel2.add(startButton);
panel2.add(replaceButton);
panel2.add(reallButton);
replaceFrame.setTitle("替换");
replaceFrame.setLocation(300,300);
replaceFrame.pack();
replaceFrame.setVisible(true);
replaceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLO SE);
}
if("开始".equals(name)||"下一个".equals(name)){
String temp=textField1.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
textArea.setSelectionStart(s);
textArea.setSelectionEnd(s+temp.length());
textArea.setSelectedTextColor(Color.GREEN);
start=s+1;
startButton.setText("下一个");
}else {
JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));
foundFrame.dispose();
}
}
if("替换为".equals(name)){
String temp=textField1.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
textArea.setSelectionStart(s);
textArea.setSelectionEnd(s+temp.length());
textArea.setSelectedTextColor(Color.GREEN);
start=s+1;
textArea.replaceSelection(textField2.getText());
}else {
JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif"));
foundFrame.dispose();
}
}
if("全部替换".equals(name)){
String temp=textArea.getText();
temp=temp.replaceAll(textField1.getText(),textField2.getTex t());
textArea.setText(temp);
}
if("全选".equals(name)){
textArea.selectAll();
}
if("字体".equals(name)){
FontDialog fontDialog=new
FontDialog(NotePad.this);
fontDialog.setVisible(true);
if(textArea.getFont()!=fontDialog.getFont()){
textArea.setFont(fontDialog.getFont());
}
}
if("记事本……".equals(name)){
AboutDialog aboutDialog=new
AboutDialog(NotePad.this);
aboutDialog.setVisible(true);
}
}
};
//创建字体设置对话面板,并添加相应事件监听器
class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListener{
public JCheckBox Bold=new JCheckBox("Bold",false);
public JCheckBox Italic=new JCheckBox("Italic",false);
public List Size,Name;
public int FontName;
public int FontStyle;
public int FontSize;
public JButton OK=new JButton("OK");
public JButton Cancel=new JButton("Cancel");
public JTextArea Text=new JTextArea("字体预览文本域\n0123456789\nAaBbCcXxYyZz");
public FontDialog(JFrame owner) {
super(owner,"字体设置",true);
GraphicsEnvironment
g=GraphicsEnvironment.getLocalGraphicsEnvironment();
String name[]=g.getAvailableFontFamilyNames();
Name=new List();
Size=new List();
FontName=0;
FontStyle=0;
FontSize=8;
int i=0;
Name.add("Default Value");
for(i=0;i<name.length;i++)
Name.add(name[i]);
for(i=8;i<257;i++)
Size.add(String.valueOf(i));
this.setLayout(null);
this.setBounds(250,200,480, 306);
this.setResizable(false);
OK.setFocusable(false);
Cancel.setFocusable(false);
Bold.setFocusable(false);
Italic.setFocusable(false);
Name.setFocusable(false);
Size.setFocusable(false);
Name.setBounds(10, 10, 212, 259);
this.add(Name);
Bold.setBounds(314, 10, 64, 22);
this.add(Bold);
Italic.setBounds(388, 10, 64, 22);
this.add(Italic);
Size.setBounds(232, 10, 64, 259);
this.add(Size);
Text.setBounds(306, 40, 157, 157);
this.add(Text);
OK.setBounds(306, 243, 74, 26);
this.add(OK);
Cancel.setBounds(390, 243, 74, 26);
this.add(Cancel);
Name.select(FontName);
Size.select(FontSize);
Text.setFont(getFont());
Name.addItemListener(this);
Size.addItemListener(this);
Bold.addItemListener(this);
Italic.addItemListener(this);
OK.addActionListener(this);
Cancel.addActionListener(this);
this.addWindowListener(this);
}
public void itemStateChanged(ItemEvent e) {
Text.setFont(getFont());
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==OK){
FontName=Name.getSelectedIndex();
FontStyle=getStyle();
FontSize=Size.getSelectedIndex();
this.setVisible(false);
}
else cancel();
}
public void windowClosing(WindowEvent e) {
cancel();
}
public Font getFont(){
if(Name.getSelectedIndex()==0) return new Font("新宋体",getStyle(),Size.getSelectedIndex()+8);
else return new
Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex() +8);
}
public void cancel(){
Name.select(FontName);
Size.select(FontSize);
setStyle();
Text.setFont(getFont());
this.setVisible(false);
}
public void setStyle(){
if(FontStyle==0 || FontStyle==2)
Bold.setSelected(false);
else Bold.setSelected(true);
if(FontStyle==0 || FontStyle==1)
Italic.setSelected(false);
else Italic.setSelected(true);
}
public int getStyle(){
int bold=0,italic=0;
if(Bold.isSelected()) bold=1;
if(Italic.isSelected()) italic=1;
return bold+italic*2;
}
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {} }
//创建关于对话框
class AboutDialog extends JDialog implements ActionListener{ public JButton OK,Icon;
public JLabel Name,Version,Author,Java;
public JPanel Panel;
AboutDialog(JFrame owner) {
super(owner,"关于",true);
OK=new JButton("OK");
Icon=new JButton(new ImageIcon("icons\\edit.gif"));
Name=new JLabel("Notepad");
Version=new JLabel("Version 1.0");
Java=new JLabel("JRE Version 6.0");
Author=new JLabel("Copyright (c) 11-5-2012 By Jianmin Chen");
Panel=new JPanel();
Color c=new Color(0,95,191);
Name.setForeground(c);
Version.setForeground(c);
Java.setForeground(c);
Author.setForeground(c);
Panel.setBackground(Color.white);
OK.setFocusable(false);
this.setBounds(250,200,280, 180);
this.setResizable(false);
this.setLayout(null);
Panel.setLayout(null);
OK.addActionListener(this);
Icon.setFocusable(false);
Icon.setBorderPainted(false);
Author.setFont(new Font(null,Font.PLAIN,11));
Panel.add(Icon);
Panel.add(Name);
Panel.add(Version);
Panel.add(Author);
Panel.add(Java);
this.add(Panel);
this.add(OK);
Panel.setBounds(0, 0, 280, 100);
OK.setBounds(180, 114, 72, 26);
Name.setBounds(80, 10, 160, 20);
Version.setBounds(80, 27, 160, 20);
Author.setBounds(15, 70, 250, 20);
Java.setBounds(80, 44, 160, 20);
Icon.setBounds(16, 14, 48, 48);
}
public void actionPerformed(ActionEvent e) { this.setVisible(false);
}
}
}
//创建记事本
public class ChenJianmin {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
NotePad notePad=new NotePad();
notePad.setTitle("记事本");
notePad.setVisible(true);
notePad.setBounds(100,100,800,600);
notePad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}。