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)); } }
每日一记代码
最新推荐文章于 2024-11-15 10:17:44 发布