java实现图形记事本

import  java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import  javax.swing.*;

public class frame implements MouseListener{
    private JFrame  window;
    private JButton ok,clear;
    private JTextArea   content;
    private JScrollPane scroll;
    public  void    go() {
        window = new JFrame("First Window");
        //-------------------------------
        ok = new JButton("保存");
        clear = new  JButton("清除");
        JPanel  p = new JPanel();
        p.setLayout(new BorderLayout());
        p.add(BorderLayout.SOUTH,ok);
        p.add(BorderLayout.NORTH,clear);
        Font textFont = new Font("隶书",Font.PLAIN,30);
        Font font=new Font("隶书",Font.BOLD,30);
        ok.setForeground(Color.BLACK);
        ok.setFont(font);
        clear.setFont(font);
        ok.addMouseListener(this);
        clear.addMouseListener(this);
        //-------------------------------------
        content = new JTextArea();
        content.setLineWrap(true);
        content.setFont(textFont);
        scroll = new JScrollPane(content);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        ok.addMouseListener(this);
        window.getContentPane().add(BorderLayout.SOUTH,p);
        window.getContentPane().add(BorderLayout.CENTER, scroll);
        window.setSize(1000,800);
        window.setLocation(600, 300);//设置窗口大小
        window.setVisible(true);//显示窗口

    }
    public  static void main(String[] args) {
            frame   f = new frame();
            f.go();
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        if(content.getText().equals("")) return;

        if(e.getSource()==ok) {//getSource()判断事件的来源,返回一个object的对象
            File  f = new File("C:\\java.txt");//保存到c盘根目录
            if(!f.exists()) {
                try {
                    f.createNewFile();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            if(writeFile(f,content.getText(),false)) {
                System.out.println("写入文件成功");
            }
        }else if(e.getSource()==clear) {
            content.setText("");
        }

    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public static boolean writeFile(File  f,String data,boolean append) {//append标志位表示是否是添加还是覆盖
            boolean flag = false;
            try {
                FileWriter  fw = new FileWriter(f.getAbsolutePath(),append);
                BufferedWriter  bw = new BufferedWriter(fw);
                bw.write(data);
                bw.close();
                flag = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return flag;
    }
    public static boolean delFile(String fileName){
        Boolean bool = false;
        File file  = new File(fileName);
        try {
            if(file.exists()){
                file.delete();
                bool = true;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return bool;
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值