----------------------
android培训、
java培训、期待与您交流!----------------------
import java.awt.Font;
import java.awt.HeadlessException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.undo.UndoManager;
public class NotePadFounction {
// 构造函数
public NotePadFounction() {
}
// 读取文件 传入参数为父窗口和文本编辑控件
public String loadFile(JFrame frame, JTextArea editor, String fileUrl) {
JFileChooser chooser = new JFileChooser();
int r = chooser.showOpenDialog(frame);
if (r == JFileChooser.APPROVE_OPTION) {
try {
editor.read(new FileReader(chooser.getSelectedFile()), null);
return chooser.getSelectedFile().getAbsolutePath();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return fileUrl;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return fileUrl;
}
}
return fileUrl;
}
// 保存文件 传入参数为父窗口和文本编辑控件
public String saveFile(JFrame frame, JTextArea editor, String fileUrl) {
if (new File(fileUrl).exists()) {
try {
editor.write(new FileWriter(new File(fileUrl)));
return fileUrl;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JFileChooser chooser = new JFileChooser();
int r = chooser.showOpenDialog(frame);
if (r == JFileChooser.APPROVE_OPTION) {
try {
editor.write(new FileWriter(chooser.getSelectedFile()));
fileUrl = chooser.getSelectedFile().getAbsolutePath();
return fileUrl;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return fileUrl;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return fileUrl;
}
}
return fileUrl;
}
// 设置字体 传入参数为控件以及字体需要的参数
public void setFont(JTextArea editor, String family, int size,
boolean bold, boolean italic) {
int k = 0;
if (bold)
k += Font.BOLD;
if (italic)
k += Font.ITALIC;
editor.setFont(new Font(family, k, size));
}
// 粘贴
public void Paste(JTextArea editor) {
editor.paste();
}
// 复制
public void Copy(JTextArea editor) {
editor.copy();
}
// 剪贴
public void Cut(JTextArea editor) {
editor.cut();
}
// 删除
public void Delete(JTextArea editor) {
editor.replaceSelection("");
}
// 全选
public void All(JTextArea editor) {
editor.selectAll();
}
// 插入时间
public void Time(JTextArea editor) {
Date now = new Date();
DateFormat d3 = DateFormat.getDateTimeInstance();
String time = d3.format(now);
editor.setText(editor.getText() + time);
}
// 撤销
public void Updo(UndoManager undomanager) {
if (undomanager.canUndo())
undomanager.undo();
}
// 恢复
public void Redo(UndoManager undomanager) {
if (undomanager.canRedo())
undomanager.redo();
}
// 新建
public String newFile(JFrame frame, String fileUrl, JTextArea editor)
throws HeadlessException, FileNotFoundException {
if (fileUrl.equals("") && !editor.getText().equals("")) {
editor.setText("");
fileUrl = "";
return fileUrl;
}
JTextArea tempeditor = new JTextArea();
try {
if(new File(fileUrl).exists())
tempeditor.read(new FileReader(new File(fileUrl)), null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (tempeditor.getText().equals(editor.getText())) {
editor.setText("");
fileUrl = "";
return fileUrl;
} else {
int temp = JOptionPane.showConfirmDialog(frame, "尚未保存,是否保存?");
if (temp == JOptionPane.OK_OPTION) {
fileUrl = this.saveFile(frame, editor, fileUrl);
} else if (temp == JOptionPane.CANCEL_OPTION)
return fileUrl;
editor.setText("");
fileUrl = "";
return fileUrl;
}
}
// 改变是否自动换行
public void changeWrap(JTextArea editor, boolean wrap) {
editor.setLineWrap(wrap);
}
// 改变是否显示字体设置栏
public void changeDisplayFont(JToolBar toolbar, boolean display) {
toolbar.setVisible(display);
}
}
--------------------------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class NotePadReadConfig {
public static void ReadConfig(int[] config) {
if (new File("Config.ini").exists()) {
File temp = new File("Config.ini");
try {
String tempconfig[] = new String[8];
Scanner scanner = new Scanner(new FileInputStream(temp));
int i = 0;
while (scanner.hasNextLine()) {
tempconfig[i] = scanner.nextLine();
i++;
}
if (i < 8)
for (int k = 0; k < 8; ++k)
config[k] = -1;
else
for (int k = 0; k < 8; ++k)
config[k] = Integer.parseInt(tempconfig[k]);
scanner.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
for (int k = 0; k < 8; ++k)
config[k] = -1;
}
public static void WriteConfig(int[] config)
{
try {
PrintWriter writer=new PrintWriter(new FileOutputStream(new File("Config.ini")));
for(int i=0;i<8;++i)
writer.println(config[i]);
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileNotFoundException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.undo.UndoManager;
public class NotePadGui extends JFrame {
private static final long serialVersionUID = 1L;
// 用于当做文版编辑器
private JTextArea editor = new JTextArea();
// 弹出菜单
private JPopupMenu pop;
// 字体设置工具栏属性
private JToggleButton radioBold;
private JToggleButton radioItalic;
private Choice chocieFamily;
private Choice chocieSize;
//菜单的事件监听器
private MenuActionListener l;
//撤销和恢复的监听器
private UndoManager undomanager;
//用于储存文件路径
private String fileUrl="";
//用于储存当前窗口的指针
private JFrame frame=this;
//用于记录是否自动换行
private boolean wrap=true;
//用于记录是否显示字体设置栏
private boolean display=false;
//用于储存工具条的指针
private JToolBar font;
private int x,y,width,height,family,size;
// 操作函数
private NotePadFounction func = new NotePadFounction();
// 构造函数 传入参数分别为起始坐标和长以及宽
public NotePadGui(int []config) {
super("NotePad+");
// 设置窗口的UI
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClas
sName());
} catch (Exception e) {
System.err.println("不能被设置外观的原因:" + e);
}
if(config[0]==-1)
{
x=100;
y=100;
width=400;
height=600;
family=0;
size=17;
}
else
{
x=config[0];
y=config[1];
width=config[2];
height=config[3];
if(config[4]==1)
wrap=true;
else
wrap=false;
if(config[5]==1)
display=true;
else
display=false;
family=config[6];
size=config[7];
}
this.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);// 设置默认关闭按钮的响应
this.setBounds(x, y, width, height);
l=new MenuActionListener();
undomanager=new UndoManager();
editor.getDocument().addUndoableEditListener(undomanager);
// 设置菜单
JMenuBar menubar = new JMenuBar();
JMenu menuFile = new JMenu("文件(F)");
JMenuItem itemNew = new JMenuItem("新建(N)");
itemNew.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
fileUrl=func.newFile(frame, fileUrl, editor);
} catch (HeadlessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
JMenuItem itemOpen = new JMenuItem("打开(O)");
itemOpen.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fileUrl=func.loadFile(frame, editor, fileUrl);
}
});
JMenuItem itemSave = new JMenuItem("保存(S)");
itemSave.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fileUrl=func.saveFile(frame, editor, fileUrl);
}
});
JMenuItem itemSaveAs = new JMenuItem("另存为(A)");
itemSaveAs.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
func.saveFile(frame, editor, "");
}
});
JMenuItem itemExit = new JMenuItem("退出(X)");
itemExit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
exit();
}
});
menuFile.add(itemNew);
menuFile.add(itemOpen);
menuFile.add(itemSave);
menuFile.add(itemSaveAs);
menuFile.addSeparator();
menuFile.add(itemExit);
JMenu menuEdit = new JMenu("编辑(E)");
JMenuItem itemUpdo = new JMenuItem("撤销(U)");
itemUpdo.addActionListener(l);
JMenuItem itemRpdo = new JMenuItem("恢复(R)");
itemRpdo.addActionListener(l);
JMenuItem itemCut = new JMenuItem("剪贴(T)");
itemCut.addActionListener(l);
JMenuItem itemCopy = new JMenuItem("复制(C)");
itemCopy.addActionListener(l);
JMenuItem itemPaste = new JMenuItem("粘贴(P)");
itemPaste.addActionListener(l);
JMenuItem itemDelete = new JMenuItem("删除(L)");
itemDelete.addActionListener(l);
JMenuItem itemAll = new JMenuItem("全选(A)");
itemAll.addActionListener(l);
JMenuItem itemTime = new JMenuItem("插入当前时间");
itemTime.addActionListener(l);
menuEdit.add(itemUpdo);
menuEdit.add(itemRpdo);
menuEdit.addSeparator();
menuEdit.add(itemCut);
menuEdit.add(itemCopy);
menuEdit.add(itemPaste);
menuEdit.add(itemDelete);
menuEdit.addSeparator();
menuEdit.add(itemAll);
menuEdit.add(itemTime);
JMenu menuFormat = new JMenu("格式(O)");
JMenuItem itemWrap = new JMenuItem("自动换行(W)");
if(wrap)
wrap=false;
else
wrap=true;
func.changeWrap(editor, wrap);
itemWrap.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(wrap)
wrap=false;
else
wrap=true;
func.changeWrap(editor, wrap);
}
});
JMenuItem itemFont = new JMenuItem("字体");
itemFont.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(display)
display=false;
else
display=true;
func.changeDisplayFont(font, display);
}
});
menuFormat.add(itemWrap);
menuFormat.add(itemFont);
JMenu menuHelp = new JMenu("帮助(H)");
JMenuItem itemHelp = new JMenuItem("查看帮助");
itemHelp.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new NotePadHelp(frame,1).setVisible(true);
}
});
JMenuItem itemIntro = new JMenuItem("关于NotePad+");
itemIntro.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new NotePadHelp(frame,2).setVisible(true);
}
});
menuHelp.add(itemHelp);
menuHelp.add(itemIntro);
menubar.add(menuFile);
menubar.add(menuEdit);
menubar.add(menuFormat);
menubar.add(menuHelp);
this.setJMenuBar(menubar);
// 设置可拖动工具条
JToolBar tool = new JToolBar();
JButton jbuNew = new JButton(new ImageIcon("ico//new.png"));
jbuNew.setToolTipText("新建");
jbuNew.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
fileUrl=func.newFile(frame, fileUrl, editor);
} catch (HeadlessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
JButton jbuSave = new JButton(new ImageIcon("ico//save.png"));
jbuSave.setToolTipText("保存");
jbuSave.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fileUrl=func.saveFile(frame, editor, fileUrl);
}
});
tool.add(jbuNew);
tool.add(jbuSave);
this.add(tool, BorderLayout.NORTH);
font = new JToolBar();
chocieFamily = new Choice();
GraphicsEnvironment family = GraphicsEnvironment
.getLocalGraphicsEnvironm
ent();
String temp[] = family.getAvailableFontFamilyNa
mes();
for (int i = 0; i < temp.length; ++i)
chocieFamily.add(temp[i]);
chocieFamily.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
func.setFont(editor, chocieFamily.getSelectedItem(),
Integer.parseInt(chocieSize.getSelectedItem()),
radioBold.isSelected(), radioItalic.isSelected());
}
});
chocieFamily.select(this.family);
chocieSize = new Choice();
for (int i = 1; i <= 72; ++i)
chocieSize.add(i + "");
chocieSize.select(size);
chocieSize.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
func.setFont(editor, chocieFamily.getSelectedItem(),
Integer.parseInt(chocieSize.getSelectedItem()),
radioBold.isSelected(), radioItalic.isSelected());
}
});
radioBold = new JToggleButton("B ");
radioBold.setFont(new Font("黑体", Font.BOLD, 18));
radioBold.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
func.setFont(editor, chocieFamily.getSelectedItem(),
Integer.parseInt(chocieSize.getSelectedItem()),
radioBold.isSelected(), radioItalic.isSelected());
}
});
radioItalic = new JToggleButton("I ");
radioItalic.setFont(new Font("黑体", Font.ITALIC, 18));
radioItalic.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
func.setFont(editor, chocieFamily.getSelectedItem(),
Integer.parseInt(chocieSize.getSelectedItem()),
radioBold.isSelected(), radioItalic.isSelected());
}
});
font.add(chocieFamily);
font.add(chocieSize);
font.add(radioBold);
font.add(radioItalic);
tool.add(font, BorderLayout.NORTH);
// 设置弹出菜单
pop = new JPopupMenu();
JMenuItem itemUpdoP = new JMenuItem("撤销(U)");
itemUpdoP.addActionListener(l);
JMenuItem itemRpdoP = new JMenuItem("恢复(R)");
itemRpdoP.addActionListener(l);
JMenuItem itemCutP = new JMenuItem("剪贴(T)");
itemCutP.addActionListener(l);
JMenuItem itemCopyP = new JMenuItem("复制(C)");
itemCopyP.addActionListener(l);
JMenuItem itemPasteP = new JMenuItem("粘贴(P)");
itemPasteP.addActionListener(l);
JMenuItem itemDeleteP = new JMenuItem("删除(L)");
itemDeleteP.addActionListener(l);
JMenuItem itemAllP = new JMenuItem("全选(A)");
itemAllP.addActionListener(l);
JMenuItem itemTimeP = new JMenuItem("插入当前时间");
itemTimeP.addActionListener(l);
pop.add(itemUpdoP);
pop.add(itemRpdoP);
pop.add(itemCutP);
pop.add(itemCopyP);
pop.add(itemPasteP);
pop.add(itemDeleteP);
pop.add(itemAllP);
pop.add(itemTimeP);
JScrollPane scrollpanel = new JScrollPane(editor);
this.add(scrollpanel, BorderLayout.CENTER);
func.setFont(editor, chocieFamily.getSelectedItem(),
Integer.parseInt(chocieSize.getSelectedItem()),
radioBold.isSelected(), radioItalic.isSelected());
editor.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
if (e.isPopupTrigger()) {
pop.show(editor, e.getX(), e.getY());
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
if(display)
display=false;
else
display=true;
func.changeDisplayFont(font, display);
}
private class MenuActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
switch(e.getActionCommand())
{
case "剪贴(T)":
func.Cut(editor);
break;
case "复制(C)":
func.Copy(editor);
break;
case "粘贴(P)":
func.Paste(editor);
break;
case "删除(L)":
func.Delete(editor);
break;
case "全选(A)":
func.All(editor);
break;
case "插入当前时间":
func.Time(editor);
break;
case "撤销(U)":
func.Updo(undomanager);
break;
case "恢复(R)":
func.Redo(undomanager);
break;
}
}
}
private void exit()
{
func.saveFile(frame, editor, fileUrl);
int config[]=new int[8];
config[0]=this.getBounds().x;
config[1]=this.getBounds().y;
config[2]=this.getBounds().width;
config[3]=this.getBounds().height;
if(wrap)
config[4]=0;
else
config[4]=1;
if(display)
config[5]=0;
else
config[5]=1;
config[6]=this.family;
config[7]=this.size;
NotePadReadConfig.WriteConfig(config);
System.exit(0);
}
}
--------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class NotePadHelp extends JDialog{
private static final long serialVersionUID = 1L;
private JDialog dialog=this;
//构造函数 传入父窗口的指针
public NotePadHelp(JFrame frame,int k)
{
super(frame,"NotePad+",true);
this.setSize(200,200);
String temp="";
if(k==1)
{
String help="关于NotPad+\n" +
"\t支持基本的记事本功能\n" +
"\t请参考windows记事本的功能使用\n" +
"\t注:快捷键功能尚未实现";
temp=help;
}
else
{
String intro="作者:玄雨\n" +
"版本:不稳定测试版";
temp=intro;
}
this.setLayout(new BorderLayout());
JPanel display=new JPanel();
this.add(display,BorderLayout.CENTER);
JTextArea textarea=new JTextArea(temp);
textarea.setEnabled(false);
display.add(textarea);
JPanel buttonPanel=new JPanel();
this.add(buttonPanel,BorderLayout.SOUTH);
JButton button=new JButton("确定");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dialog.dispose();
}
});
buttonPanel.add(button);
this.pack();
}
}
------------------------------------------------------
public class NotePadStart {
public static void main(String[] args) {
// TODO Auto-generated method stub
int config[]=new int[8];
NotePadReadConfig.ReadConfig(config);
new NotePadGui(config).setVisible(true);
}
}
----------------------
android培训、
java培训、期待与您交流!----------------------
详情请查看: http://edu.csdn.net/heima
import java.awt.Font;
import java.awt.HeadlessException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.undo.UndoManager;
public class NotePadFounction {
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class NotePadReadConfig {
}
------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileNotFoundException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.undo.UndoManager;
public class NotePadGui extends JFrame {
}
--------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class NotePadHelp extends JDialog{
}
------------------------------------------------------
public class NotePadStart {
}
详情请查看: http://edu.csdn.net/heima