java下的记事本

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);
				}
			}
		}
		 
	 }

}
----------------------------------------


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 日历记事本是一种用Java编程语言开发的软件应用程序,旨在帮助用户记录和管理日程安排和事件。用户可以使用该应用程序来创建、编辑和删除事件,并根据日期和时间来安排和跟踪这些事件。 Java 日历记事本通常具有以下功能: 1. 日历显示:应用程序通过呈现一个日历界面,让用户可以在不同日期之间切换和查看事件。用户可以通过点击特定日期来查看该日期的事件详情。 2. 事件创建:用户可以创建新事件,包括事件的名称、日期、时间、地点以及其他相关详细信息。用户可以选择是否重复事件,例如每天、每周、每月或每年重复。 3. 事件编辑和删除:用户可以编辑已存在的事件,包括修改事件的日期、时间、地点和其他详细信息。用户也可以删除不再需要的事件。 4. 提醒功能:Java 日历记事本通常提供事件提醒功能,以确保用户不会错过重要的事件。用户可以设置提醒时间,并在提醒时间到达时接收通知。 5. 搜索和过滤:为了方便用户查找和整理事件,Java 日历记事本通常提供搜索和过滤功能。用户可以通过关键词、日期、标签等条件来搜索和过滤事件,以快速找到所需的信息。 6. 数据存储:Java 日历记事本通常使用数据库或文件来存储用户的事件数据。这样,用户可以在不同设备之间同步和访问他们的事件。 总之,Java 日历记事本是一种方便的工具,用于帮助用户管理和记录重要的日程安排和事件。它提供了简单易用的界面和多样化的功能,以满足用户的个性化需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值