⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notebook.java

📁 基于java开发的个人数字助理
💻 JAVA
字号:
/*
 * NoteBook.java
 *
 * Created on 2008年3月13日, 下午4:46
 */

package pda;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;

/**
 *
 * @author  bzu
 */
public class NoteBook extends javax.swing.JFrame implements Runnable{
    
    /** Creates new form NoteBook */
    public NoteBook() {
        super("备忘录");
        initComponents();
        //Thingshow();
        ShowThing();
        if(clockThread==null)
        {
            clockThread=new Thread(this,"Clock");
            clockThread.start();
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jToolBar1 = new javax.swing.JToolBar();
        add = new javax.swing.JButton();
        del = new javax.swing.JButton();
        newwindow = new javax.swing.JButton();
        time1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        thingTextArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        timeTextArea = new javax.swing.JTextArea();
        thing = new javax.swing.JLabel();
        time2 = new javax.swing.JLabel();
        time3 = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        Menu = new javax.swing.JMenu();
        addMenuItem = new javax.swing.JMenuItem();
        refMenuItem = new javax.swing.JMenuItem();

        add.setText("\u6dfb\u52a0");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        jToolBar1.add(add);

        del.setText("\u5220\u9664");
        del.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delActionPerformed(evt);
            }
        });

        jToolBar1.add(del);

        newwindow.setText("\u5237\u65b0");
        newwindow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newwindowActionPerformed(evt);
            }
        });

        jToolBar1.add(newwindow);

        time1.setText("\u73b0\u5728\u65f6\u523b\uff1a");

        thingTextArea.setColumns(16);
        thingTextArea.setRows(5);
        jScrollPane1.setViewportView(thingTextArea);

        timeTextArea.setColumns(10);
        timeTextArea.setRows(5);
        jScrollPane2.setViewportView(timeTextArea);

        thing.setText("\u63d0\u9192\u4e8b\u52a1\uff1a");

        time2.setText("\u63d0\u9192\u65f6\u95f4\uff1a");

        time3.setEditable(false);
        time3.setBorder(null);

        Menu.setText("\u529f\u80fd");
        addMenuItem.setText("\u6dfb\u52a0");
        addMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addMenuItemActionPerformed(evt);
            }
        });

        Menu.add(addMenuItem);

        refMenuItem.setText("\u5237\u65b0");
        refMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refMenuItemActionPerformed(evt);
            }
        });

        Menu.add(refMenuItem);

        jMenuBar1.add(Menu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(thing, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                            .addComponent(time1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(time2, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(time3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(time1)
                            .addComponent(time3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(thing)
                    .addComponent(time2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))
                .addContainerGap())
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
    }// </editor-fold>//GEN-END:initComponents

    private void refMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
        this.setVisible(false);
        NoteBook nb=new NoteBook();
        nb.setVisible(true);
    }//GEN-LAST:event_refMenuItemActionPerformed

    private void addMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
        this.setVisible(false);
        AddNote an=new AddNote();
        an.time();
        an.setVisible(true);
    }//GEN-LAST:event_addMenuItemActionPerformed

    private void newwindowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newwindowActionPerformed
// TODO 将在此处添加您的处理代码:
        this.setVisible(false);
        NoteBook nb=new NoteBook();
        nb.setVisible(true);
    }//GEN-LAST:event_newwindowActionPerformed

    private void delActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delActionPerformed
// TODO 将在此处添加您的处理代码:
        file=new File("src/database/pda.mdb");//经典所在:不用配置数据源
        path="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+file.getAbsolutePath();//获取绝对路径
        //delSql="delete from notebook where thing=?";
        delSql="delete from notebook where thing=? or time=?";
        delThingStr=thingTextArea.getSelectedText();
        delTimeStr=timeTextArea.getSelectedText();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:pda");
            //Connection con=DriverManager.getConnection(path);
            ResultSet rs;
            PreparedStatement pst=con.prepareStatement(delSql);
            pst.setString(1,delThingStr);
            pst.setString(2,delTimeStr);
            //JOptionPane.showMessageDialog(null,thingTextArea.getSelectedText());
            pst.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.setVisible(false);
        NoteBook nb=new NoteBook();
        nb.setVisible(true);
    }//GEN-LAST:event_delActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
// TODO 将在此处添加您的处理代码:
        this.setVisible(false);
        AddNote an=new AddNote();
        an.time();
        an.setVisible(true);
        
    }//GEN-LAST:event_addActionPerformed

    public void run() {
        while(clockThread!=null)
        {
            reshow();
            try
            {
                clockThread.sleep(1000);
            }catch(InterruptedException e)
            {
            }
        }
    }

    private void reshow() {
        file=new File("src/database/pda.mdb");//经典所在:不用配置数据源
        path="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+file.getAbsolutePath();//获取绝对路径
        Calendar now=new GregorianCalendar();
        time=now.get(now.YEAR)+"-"+(now.get(now.MONTH)+1)+"-"+now.get(now.DAY_OF_MONTH)+"  "+now.get(now.HOUR_OF_DAY)+":"+now.get(now.MINUTE)+":"+now.get(now.SECOND);
        time3.setText(time);
        sql="select * from notebook ORDER BY time";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:pda");
            //Connection con=DriverManager.getConnection(path);
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
                thingStr=rs.getString(1);
                timeStr=rs.getString(2);
                if(time.equals(timeStr)){
                    JOptionPane.showMessageDialog(null,thingStr);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void ShowThing(){
        file=new File("src/database/pda.mdb");//经典所在:不用配置数据源
        path="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+file.getAbsolutePath();//获取绝对路径
        sql="select * from notebook ORDER BY time";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:pda");
            //Connection con=DriverManager.getConnection(path);
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
                thingStr=rs.getString(1);
                timeStr=rs.getString(2);
                thingTextArea.append(thingStr+'\n');
                timeTextArea.append(timeStr+'\n');
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    /*public static void main(String args[]) {
        NoteBook nb=new NoteBook();
        nb.setVisible(true);
    }*/
    
    /**
     * @param args the command line arguments
     */
         
    public void Thingshow(){//用文件的思路
        try{
                File file1=new File("note.txt");
                FileReader fr1=new FileReader(file1);
                BufferedReader br1=new BufferedReader(fr1);
                String s;
                while((s=br1.readLine())!=null)
                    thingTextArea.append(s+'\n');
                br1.close();
                fr1.close();
                
                File file2=new File("time.txt");
                FileReader fr2=new FileReader(file2);
                BufferedReader br2=new BufferedReader(fr2);
                String t;
                while((t=br2.readLine())!=null)
                    timeTextArea.append(t+'\n');
                br2.close();
                fr2.close();
            }catch (Exception e){}
    }
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JMenu Menu;
    private javax.swing.JButton add;
    private javax.swing.JMenuItem addMenuItem;
    private javax.swing.JButton del;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton newwindow;
    private javax.swing.JMenuItem refMenuItem;
    private javax.swing.JLabel thing;
    private javax.swing.JTextArea thingTextArea;
    private javax.swing.JLabel time1;
    private javax.swing.JLabel time2;
    private javax.swing.JTextField time3;
    private javax.swing.JTextArea timeTextArea;
    // 变量声明结束//GEN-END:variables
    Thread clockThread;
    String time,sql,thingStr,timeStr,delThingStr,delTimeStr,delSql,path;
    File file;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -