每日一记代码

package day01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class AddJFrame extends JFrame implements ActionListener {

    //定义标题输入框
    JTextField titleText  = new JTextField();

    //定义内容的输入区域
    JTextArea contentText = new JTextArea();

    //定义保存按钮
    JButton save = new JButton("保存");

    //定义取消按钮
    JButton cancel = new JButton("取消");

    public AddJFrame(){
        //初始化界面
        initFrame();

        //初始化组件
        initView();

        //让界面展示出来
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj == save) {

            System.out.println("保存按钮被点击了");
            try {

                BufferedReader duqu  = new BufferedReader(new FileReader("D:\\bc2\\rijileixi\\wnejie\\shuzi"));
                String s = duqu.readLine();
                duqu.close();

                int i = Integer.parseInt(s);
                i++;

               /* FileReader shuzi = new FileReader("D:\\bc2\\rijileixi\\wnejie\\shuzi");
                int read = shuzi.read();
                shuzi.close();
                read++;
                char xd = (char) read;
                String zhuanhuan = xd + "";
                int i = Integer.parseInt(zhuanhuan);*/

                int k = 0;
                if (pdff( i )) {
                    k = pdshuzi();
                    ObjectOutputStream xie = new ObjectOutputStream(new FileOutputStream("D:\\bc2\\rijileixi\\xuleihualei\\rijixingxi&" + k));
                    xie.writeObject(new webbao.documentation(titleText.getText(), contentText.getText(),k));
                    xie.close();
                }else {
                    ObjectOutputStream xie = new ObjectOutputStream(new FileOutputStream("D:\\bc2\\rijileixi\\xuleihualei\\rijixingxi&" + i));
                    xie.writeObject(new webbao.documentation(titleText.getText(), contentText.getText(),i));
                    xie.close();
                }

                String s1 = String.valueOf(i);
                BufferedWriter xiexie = new BufferedWriter(new FileWriter("D:\\bc2\\rijileixi\\wnejie\\shuzi"));
                xiexie.write(s1);
                xiexie.close();



            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

            try {
                new NoteJFrame();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            } catch (ClassNotFoundException classNotFoundException) {
                classNotFoundException.printStackTrace();
            }
            setVisible(false);


        } else if (obj == cancel) {
            System.out.println("取消按钮被点击了");
            try {
                new NoteJFrame();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            } catch (ClassNotFoundException classNotFoundException) {
                classNotFoundException.printStackTrace();
            }
            setVisible(false);
        }


    }

    public int pdshuzi() {
        int k = 0;
        File duqu = new File("D:\\bc2\\rijileixi\\xuleihualei");
        File[] files = duqu.listFiles();
        for (File file : files) {
            String name = file.getName();
            String substring = name.substring(11);
            int i1 = Integer.parseInt(substring);
            if (i1 > k) {
                k = i1;
            }
        }


        return k + 1;
    }

        public boolean pdff ( int bd ){
            File duqu = new File("D:\\bc2\\rijileixi\\xuleihualei");
            File[] files = duqu.listFiles();
            for (File file : files) {
                String name = file.getName();
                String substring = name.substring(11);
                int i1 = Integer.parseInt(substring);
                if (i1 == bd) {
                    return true;
                }
            }
            return false;
        }

    private void initView() {
        //定义最上面的每日一记
        JLabel title = new JLabel("每日一记");
        title.setBounds(220, 20, 584, 50);
        title.setFont(new Font("宋体", Font.BOLD, 32));
        this.getContentPane().add(title);

        //定义文字:标题
        JLabel subject = new JLabel("标题");
        subject.setBounds(70,90,100,30);
        subject.setFont(new Font("宋体",Font.PLAIN,16));
        this.getContentPane().add(subject);

        //定义文字:内容
        JLabel content = new JLabel("内容");
        content.setBounds(70,140,100,30);
        content.setFont(new Font("宋体",Font.PLAIN,16));
        this.getContentPane().add(content);


        //设置标题的输入框
        titleText.setBounds(120,90,426,30);
        titleText.setFont(new Font("宋体",Font.PLAIN,16));
        this.getContentPane().add(titleText);

        //设置内容的输入框
        contentText.setBounds(120,140,426,300);
        contentText.setFont(new Font("宋体",Font.PLAIN,16));
        this.getContentPane().add(contentText);

        //设置保存按钮
        save.setBounds(132,466,140,40);
        save.setFont(new Font("宋体",Font.PLAIN,24));
        save.addActionListener(this);
        this.getContentPane().add(save);

        //设置取消按钮
        cancel.setBounds(312,466,140,40);
        cancel.setFont(new Font("宋体",Font.PLAIN,24));
        cancel.addActionListener(this);
        this.getContentPane().add(cancel);

    }


    //对添加界面的一些设置
    private void initFrame() {
        //设置界面的宽高
        this.setSize(600, 600);
        //设置界面的标题
        this.setTitle("每日一记(添加日记)");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件
        this.setLayout(null);
        //设置背景颜色
        this.getContentPane().setBackground(new Color(212,212,212));
    }


}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值