package com.notepad.text;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Datepad {
public static String getdate(){
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss EE");
return df.format(date);
}
}
-----------------------------
package com.notepad.text;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class fontDialog extends JDialog implements ActionListener,ListSelectionListener{
public static final int Cancle=0;
public static final int OK=1;
public static final String [] style={"正常","斜体","粗体","粗斜体"};
public static final String [] size={"8","9","10","11","12","14","16",
"18","20","22","24","26","28","36","48","72"};
private Font userFont=null;
private int userSelect=Cancle;
private JFrame parent=null;
private Container con;
private JScrollPane nameSPane,styleSPane,sizeSPane;
private JPanel panel[];
private JLabel nameLbl,styleLbl,sizeLbl;
private JTextField nameText,styleText,sizeText;
private JList nameList,styleList,sizeList;
private JButton OKBtn,cancleBtn;
public fontDialog() {
this(null);
}
public fontDialog(JFrame owner){
super(owner,true);
parent=owner;
setTitle("字体");
con=getContentPane();
BoxLayout box=new BoxLayout(con,BoxLayout.Y_AXIS);
con.setLayout(box);
panel=new JPanel[4];
for(int i=0;i<3;i++){
panel[i]=new JPanel();
panel[i].setLayout(new GridLayout(1,3));
}
panel[3]=new JPanel();
panel[3].setLayout(new FlowLayout());
nameLbl=new JLabel("字体");
styleLbl=new JLabel("字形");
sizeLbl=new JLabel("大小");
panel[0].add(nameLbl);
panel[0].add(styleLbl);
panel[0].add(sizeLbl);
nameText=new JTextField("宋体");
nameText.setColumns(5);
nameText.setEditable(false);
styleText=new JTextField("正常");
styleText.setColumns(5);
styleText.setEditable(false);
sizeText=new JTextField("12");
sizeText.setColumns(5);
sizeText.setEditable(false);
panel[1].add(nameText);
panel[1].add(styleText);
panel[1].add(sizeText);
GraphicsEnvironment eq = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] availableFonts= eq.getAvailableFontFamilyNames();
nameList=new JList(availableFonts);
nameList.addListSelectionListener(this);
nameSPane=new JScrollPane(nameList);
styleList=new JList(style);
styleList.addListSelectionListener(this);
styleSPane=new JScrollPane(styleList);
sizeList=new JList(size);
sizeList.addListSelectionListener(this);
sizeSPane=new JScrollPane(sizeList);
panel[2].add(nameSPane);
panel[2].add(styleSPane);
panel[2].add(sizeSPane);
OKBtn=new JButton("确定");
OKBtn.addActionListener(this);
cancleBtn=new JButton("取消");
cancleBtn.addActionListener(this);
panel[3].add(OKBtn);
panel[3].add(cancleBtn);
for(int i=0;i<4;i++)
con.add(panel[i]);
}
public int showFontDialog(){
setSize(300,300);
int x,y;
if (parent!=null){
x=parent.getX()+30;
y=parent.getY()+30;
}else{
x=150;
y=100;
}
setLocation(new Point(x,y));
setVisible(true);
return userSelect;
}
public Font getFont(){
return userFont;
}
public void actionPerformed(ActionEvent e){
int styleIndex=Font.PLAIN,fontSize;
if(e.getSource()==OKBtn){
if(styleText.getText().equals("正常"))
styleIndex=Font.PLAIN;
if(styleText.getText().equals("斜体"))
styleIndex=Font.ITALIC;
if(styleText.getText().equals("粗体"))
styleIndex=Font.BOLD;
if(styleText.getText().equals("粗斜体"))
styleIndex=Font.BOLD | Font.ITALIC;
fontSize=Integer.parseInt(sizeText.getText());
userFont=new Font(nameText.getText(),styleIndex,fontSize);
userSelect=OK;
setVisible(false);
}else{
userSelect=Cancle;
setVisible(false);
}
}
public void valueChanged(ListSelectionEvent e){
if (e.getSource()==nameList)
nameText.setText((String)nameList.getSelectedValue());
if (e.getSource()==styleList)
styleText.setText((String)styleList.getSelectedValue());
if (e.getSource()==sizeList)
sizeText.setText((String)sizeList.getSelectedValue());
}
}
----------------------------------------
package com.notepad.text;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class AboutBorder extends JFrame implements ActionListener{
private JFrame frame;
public AboutBorder(){
frame = new JFrame("About Notepad");
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.RIGHT));
JLabel label = new JLabel("About Notepad Verson 1.0 , auhtor----刘涛",JLabel.CENTER);
JButton button_OK = new JButton("OK");
button_OK.addActionListener(this);
pane.add(button_OK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label , BorderLayout.CENTER);
frame.add(pane , BorderLayout.SOUTH);
frame.setBounds(200, 200, 300, 100);
frame.setResizable(false);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
}
----------------------------------------
package com.notepad.text;
import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;
import java.awt.event.*;
public class NotePad extends JFrame{
private JFrame mainframe;
private JTextArea textarea ;
private JScrollPane jsp ;
private JMenuBar jmb ;
private JMenu File , Edit , Format ,View , Help ;
private JMenuItem F_new , F_open , F_save, F_saveas ,
F_page ,F_print , F_exit ; //File菜单
private JMenuItem E_undo , E_cut , E_copy , E_paste , E_delete, E_find ,
E_findnext , E_replace , E_go , E_seletall ,E_date ;//Edit菜单
private JMenuItem FM_word , FM_font ; //Format菜单
private JMenuItem V_status ; //View菜单
private JMenuItem H_view , H_about ; //Help菜单
private JFileChooser chooser ;
private String title = null;
private String text ;
/*
* 主构造函数 NotePad()
*/
public NotePad(){
buildLayout();
}
/*
* 窗体构建buildLayout()
*/
private void buildLayout(){
mainframe = new JFrame(title + " - Notepad");
Container con = mainframe.getContentPane();
textarea = new JTextArea();
textarea.setWrapStyleWord(true);
jsp = new JScrollPane(textarea);
jmb = new JMenuBar();
jmb.setBorderPainted(true);
//菜单主项
File = new JMenu("File");
Edit = new JMenu("Edit");
Format = new JMenu("Format");
View = new JMenu("View");
Help = new JMenu("Help");
//File子菜单项
F_new = new JMenuItem("New");
F_new.addActionListener(new CAL());
F_open = new JMenuItem("Open...");
F_open.addActionListener(new CAL());
F_save = new JMenuItem("Save");
F_save.addActionListener(new CAL());
F_saveas = new JMenuItem("Save As...");
F_saveas.addActionListener(new CAL());
F_page = new JMenuItem("Page Setup...");
F_print = new JMenuItem("Print...");
F_exit = new JMenuItem("Exit");
F_exit.addActionListener(new CAL());
//Edit子菜单项
E_undo = new JMenuItem("Undo");
E_cut = new JMenuItem("Cut");
E_cut.addActionListener(new CAL());
E_copy = new JMenuItem("Copy");
E_copy.addActionListener(new CAL());
E_paste = new JMenuItem("Paste");
E_paste.addActionListener(new CAL());
E_delete = new JMenuItem("Delete");
E_delete.addActionListener(new CAL());
E_find = new JMenuItem("Find...");
E_findnext = new JMenuItem("Find Next...");
E_replace = new JMenuItem("Replace...");
E_go = new JMenuItem("Go To...");
E_seletall = new JMenuItem("Selet All");
E_seletall.addActionListener(new CAL());
E_date = new JMenuItem("Time/Date");
E_date.addActionListener(new CAL());
//Format子菜单项
FM_word = new JMenuItem("Word Wrap");
FM_font = new JMenuItem("Font...");
FM_font.addActionListener(new CAL());
//View子菜单项
V_status = new JMenuItem("Status Bar");
V_status.setEnabled(false);
//Help子菜单项
H_view = new JMenuItem(" View Help ");
H_about = new JMenuItem("About NotePad");
H_about.addActionListener(new CAL());
jmb.add(File);
jmb.add(Edit);
jmb.add(Format);
jmb.add(View);
jmb.add(Help);
File.add(F_new);File.add(F_open);File.add(F_save);File.add(F_saveas);File.insertSeparator(3);
File.add(F_page);File.add(F_print);File.insertSeparator(12);File.add(F_exit);
Edit.add(E_undo);Edit.insertSeparator(3);Edit.add(E_cut);Edit.add(E_copy);Edit.add(E_paste);
Edit.add(E_delete);Edit.insertSeparator(12);Edit.add(E_find);Edit.add(E_findnext);
Edit.add(E_replace);Edit.add(E_go);Edit.insertSeparator(20);Edit.add(E_seletall);Edit.add(E_date);
Format.add(FM_word);Format.add(FM_font);
View.add(V_status);
Help.add(H_view);Help.insertSeparator(6);Help.add(H_about);
con.add(jsp , BorderLayout.CENTER);
ImageIcon ii = new ImageIcon("Notepad.gif");
mainframe.setIconImage(ii.getImage());
textarea.setSelectedTextColor(Color.red);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //获取显示器大小
mainframe.setJMenuBar(jmb);
mainframe.setSize(d);
mainframe.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if(text.equals(textarea.getText())){
JFileChooser chooser = new JFileChooser("D:\\");
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.txt", "java", "txt");
chooser.setFileFilter(filter);
chooser.showSaveDialog(mainframe);
try {
WriteText(chooser.getSelectedFile().getAbsoluteFile());
} catch (IOException e1) {
JOptionPane.showMessageDialog(mainframe, "请输入文件名!!!" , "Error" , JOptionPane.ERROR_MESSAGE);
}
}
else{
System.exit(0);
}
}
});
mainframe.setVisible(true);
}
public static void main(String[] args) {
new NotePad();
}
/*
* 文本读取
*/
private void ReadText(File fileName) throws IOException{
FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buf = new byte[(int)fileName.length()];
while((bis.read(buf))!=-1){
text = new String(buf);
textarea.setText(text);
}
bis.close();
fis.close();
}
/*
* 文本存储
*/
private void WriteText(File absoluteFile) throws IOException {
FileOutputStream fos = new FileOutputStream(absoluteFile);
PrintWriter pw = new PrintWriter(fos);
String str = textarea.getText();
pw.write(str);
pw.close();
}
/*
* 为菜单添加监听器
*/
class CAL implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==F_new){
mainframe.setTitle(title+ " - Notepad");
textarea.setText("");
}
if(e.getSource()==E_date){
int end = 0 ;
int start = 0 ;
start = textarea.getSelectionStart();
end = textarea.getSelectionEnd();
String end_s = textarea.getText().substring(0, start);
String end_e = textarea.getText().substring(end, textarea.getText().length());
textarea.setText(end_s+Datepad.getdate()+end_e);
textarea.setCaretPosition(start);
}
if(e.getSource()==E_seletall){
textarea.selectAll();
}
if(e.getSource()==E_delete){
int end = 0 ;
int start = 0 ;
start = textarea.getSelectionStart();
end = textarea.getSelectionEnd();
String end_s = textarea.getText().substring(0, start);
String end_e = textarea.getText().substring(end, textarea.getText().length());
textarea.setText(end_s+end_e);
textarea.setCaretPosition(start);
}
if(e.getSource()==E_copy){
textarea.copy();
}
if(e.getSource()==E_paste){
textarea.paste();
}
if(e.getSource()==E_cut){
textarea.cut();
}
if(e.getSource()==H_about){
new AboutBorder();
}
if(e.getSource()==FM_font){
fontDialog fd = new fontDialog(mainframe);
fd.showFontDialog();
Font font = fd.getFont();
textarea.setFont(font);
}
if(e.getSource()==F_save){
try {
WriteText(chooser.getSelectedFile().getAbsoluteFile());
} catch (IOException e1) {}
}
if(e.getSource()==F_exit){
System.exit(0);
}
if(e.getSource()==F_open){
chooser = new JFileChooser("D:\\");
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.txt", "java", "txt");
chooser.setFileFilter(filter);
chooser.showOpenDialog(mainframe);
try {
mainframe.setTitle(chooser.getSelectedFile().getName()+"--Notepad");
ReadText(chooser.getSelectedFile().getAbsoluteFile());
} catch (IOException e1) {
JOptionPane.showMessageDialog(mainframe, "文件不存在或不允许访问!!!" , "Error" , JOptionPane.ERROR_MESSAGE);
}
}
if(e.getSource()==F_saveas){
JFileChooser chooser = new JFileChooser("D:\\");
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.txt", "java", "txt");
chooser.setFileFilter(filter);
chooser.showSaveDialog(mainframe);
try {
WriteText(chooser.getSelectedFile().getAbsoluteFile());
} catch (IOException e1) {
JOptionPane.showMessageDialog(mainframe, "请输入文件名!!!" , "Error" , JOptionPane.ERROR_MESSAGE);
}
}
}
}
}
----------------------------------------