/**
 * @version  2.0
 * @author sharks
*/
/**
 * Instruction
 * this version will use IO
 * apply file to store data
 * it just a table double click table can edit the course
 * */
import java.io.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import java.awt.*;
import java.awt.event.*;

/**
 *Graphic
 *7 JTalbe add to JScrollPane
 *
 * */

/**
 * I should use cardlayout.there are seven days a week
 * and ten classes one day(as Big class is five )
 * so every day should take 5 rows and 3 columns (5,3)
 *
 * */
/**
 * how to generate file
 * first get info from table
 * second when click button get info from table and write to file
 * when opening this software read file from local
 * when renew info must reload file
 * */
class TC extends JFrame implements ActionListener,java.io.Serializable{ //Serializable this interface can write to file

    private JButton[] btn = new JButton[10];

    JScrollPane[] sp = new JScrollPane[7];

    private CardLayout c = new CardLayout();

    private JPanel p = new JPanel(c);

    private JPanel pbottom = new JPanel();

    private JMenuItem[] week = new JMenuItem[7];

    private JMenuBar mb = new JMenuBar();
    private JMenu[] jm = new JMenu[3];

    //evert day has a Table  ,so everyday generate a file ,so just give values
    //seven panel respentsevent days for sunday
    private JTable[] mt = new JTable[7];

    String temp = null ;

    int current = 0;

    JMenuItem ul = null;
    JMenuItem lk = null;

    //table headers
            String headers[] = {"When","Name","Teacher","Class"};
            // table data
            Object[][] cdata = new Object[5][4];

    public void launch(){
        //get previous
        //Bottom Panel

        btn[0] = new JButton("Pervious");
        btn[1] = new JButton("Next");
        btn[0].addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {

                c.previous(p);
                current = current-1;
                if(current<0)current = 6;
                try {
                    setData();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(current);
                        }});

        btn[1].addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {

                c.next(p);
                current ++ ;
                if(current>6)current=0;
                try {
                    setData();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                System.out.println(current);
            }});

        pbottom.add(btn[0]);
        pbottom.add(btn[1]);

        //setMenubar
        jm[0] = new JMenu("Week ");

        week[0] = new JMenuItem("Sun");
        week[1] = new JMenuItem("Mon");
        week[2] = new JMenuItem("Tue");
        week[3] = new JMenuItem("Wed");
        week[4] = new JMenuItem("Thu");
        week[5] = new JMenuItem("Fri");
        week[6] = new JMenuItem("Sat");

        for(int i = 0 ;i<7 ;i++){
            week[i].addActionListener(this);
            jm[0].add(week[i]);
            }
        //lock table
        jm[1] = new JMenu("Edit");
         ul = new JMenuItem("UnLock");
         lk = new JMenuItem("Lock  ");
         ul.addActionListener(this);
         lk.addActionListener(this);
        jm[1].add(ul);
        jm[1].add(lk);
        // load pamel
        jm[2] = new JMenu("Help");
        JMenuItem bl = new JMenuItem("MyBlog");
        bl.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String command = "explorer.exe http://www.cnblogs.com/oaks";
                try {
                    Runtime.getRuntime().exec(command);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }});
        JMenuItem he = new JMenuItem("Help");
        he.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                /*
                JPanel pp = new JPanel(new GridLayout(6,1,8,8));
                pp.add(new JLabel("1.you can select menubar to loacte week"));
                pp.add(new JLabel("2.double click table to edit this TableCourse"));
                pp.add(new JLabel("3.when you make it up,you must use 'Lock'menuitem to store the data you inputed"));
                pp.add(new JLabel("4.when the table is locked ,you can`t edit it ever until youclick 'UnLoak' menuitem"));
                p.add(pp,"7");
                c.show(p, "7");
                */
                String command = "notepad e:/TC/help.txt";
                try {
                    Runtime.getRuntime().exec(command);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }});
        jm[2].add(bl);
        jm[2].add(he);
        //add three menus
        for(int i = 0 ;i<3 ;i++){
        mb.add(jm[i]);
        }

        /**
         * help
         *
         * */

        /**
         * it display by values from object
         * before every displaying read file to give values
         * */

        for(int i = 0 ;i<7;i++){
        mt[i] = new JTable(cdata,headers);
        mt[i].getTableHeader().setPreferredSize(new Dimension(100,40));
        mt[i].setPreferredScrollableViewportSize(new Dimension(this.getWidth(),this.getHeight()));
        mt[i].setRowHeight(50);
        TableColumn column = null;
        int colunms = mt[i].getColumnCount();
        for(int j = 0; j < colunms; j++)
        {
            column = mt[i].getColumnModel().getColumn(j);

            column.setPreferredWidth(150);
        }    

        mt[i].setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        sp[i] = new JScrollPane(mt[i]);
        //sp[i].setSize(500, 500);
        // 0  == sunday
        p.add(sp[i],i+"");

        }

        //set panel content
        /**generate content
         * week panel should have a  common table but different values
         * just in different give table item different values
         * */  

        //add week panel to p[0] panel 1-8
        //panel start with 1 represent Panel1 and 7 represent sunday
        //at last it belongs panel
        //add monday first ,so it display mon day ,the first panel num1 

        setTitle("TC1.6");

        setBounds(400,180,600,386);

        this.setJMenuBar(mb);

        add(p,BorderLayout.CENTER);

        setResizable(false);
        add(pbottom,BorderLayout.SOUTH);
        //this.pack();
        try {
            setData();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        setVisible(true);
        //style on close this frame
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    @Override
    public void actionPerformed(ActionEvent e){
                //set events  for week menu
        for(int i = 0;i<7;i++){
            pbottom.setVisible(true);
            if(e.getSource().equals(week[i]))
                switch(i){
                case 0:

                    current = 0 ;
                    try {
                        setData();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    c.show(p, "0");
                    // 0 == sunday
                    break;
                case 1:
                    current = 1 ;
                    try {
                        setData();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                    c.show(p, "1");
                    break;

                case 2:
                    current = 2;
                    try {
                        setData();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    c.show(p, "2");

                    break;
                case 3:
                    current = 3 ;
                    try {
                        setData();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    c.show(p, "3");

                    break;
                case 4: 

                    current = 4 ;
                    try {
                        setData();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    c.show(p, "4");

                    break;
                case 5:
                    current = 5 ;
                    try {
                        setData();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    c.show(p, "5");

                    break;
                case 6:
                    current = 6 ;
                    try {
                        setData();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    c.show(p, "6");

                    break;

                default:
                    System.out.println("Occur error on switch!");
                }
        }

        //file store &&&&data read
        /**
         * lock means write file reload file and setEditable=No
         * first  I should know which panel is displaying
         * so I should write a method to get Which panel is displaying
         * */

        if(e.getSource().equals(lk)){
            String tmp = null ;

            /*

            for(int i = 0 ;i<5;i++){

                for(int j = 0;j<4;j++){

                    temp = (String)mt[0].getValueAt(i, j);
                    System.out.println(temp);
                    if(temp==null){
                        temp = "n";
                    }
                    try {
                        dos.writeUTF(temp);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                }
            }
            try {
                dos.flush();
                fos.close();
            dos.close();

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            */
            String filename = null;
            filename="e:/TC/week"+current+".dat";
            File file = new File(filename);
            FileOutputStream fos = null;
            try {

                    fos = new FileOutputStream(file);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            DataOutputStream dos = new DataOutputStream(fos);

            for(int i = 0 ;i<mt[current].getRowCount();i++){

                for(int j = 0;j<mt[current].getColumnCount();j++){
                    temp = (String)mt[current].getValueAt(i, j);

                    if(temp==null){

                        temp = "nl";
                    }
                    try {

                        dos.writeUTF(temp);
                        System.out.println(temp);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                }

            }

            try {
                    dos.flush();
                    fos.close();
                    dos.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            try {
                setData();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            mt[current].setEnabled(false);
            this.setTitle("TC1.6---Locked");
        }

        //file read & data store
        if(e.getSource().equals(ul)){
            mt[current].setEnabled(true);
            //System.out.println("this panel false");
            this.setTitle("Week At:"+current+"   TC1.6---UnLocked");
        }

    }
    /**
     * this method help get current panel
     * */
    /*
    public int  getCurrent(){

        if(current>6){
            //max of current is 6,never than 6 so ,never equal 0
            current = 0 ;
        }
        if(current <1 ){
            current  = 6 ;
        }
        return current ;
    }*/

    public void setData() throws IOException{

        String fn = null;
        Object o =new Object();
        fn="e:/TC/week"+current+".dat";
        File ff = new File(fn);
        FileInputStream fis = new FileInputStream(ff);
        DataInputStream dis = new DataInputStream(fis);

        for(int i = 0;i<5;i++){

            for(int j = 0;j<4;j++){

                fn=dis.readUTF();
                if(fn.equals("nl")){

                    fn = "";
                }
            //    System.out.println(fn);
                o = (Object)fn;
                mt[current].setValueAt(o, i, j);

            }
        }
        fis.close();
        dis.close();
        switch(current){
        case 0: this.setTitle("TC1.6 --Sunday");break;
        case 1: this.setTitle("TC1.6 --Monday");break;
        case 2: this.setTitle("TC1.6 --Tuesday");break;
        case 3: this.setTitle("TC1.6 --Wednesay");break;
        case 4: this.setTitle("TC1.6 --Thursday");break;
        case 5: this.setTitle("TC1.6 --Friday");break;
        case 6: this.setTitle("TC1.6 --Saturday");break;
        case 7: this.setTitle("TC1.6 --Help");break;
        default: this.setTitle("TC1.6 --Welcome");break;
        }

    }
    /**
     * copy files in execute package to local disk
     * it needs file object
     * */
    /*public void copyMethod(InputStream cpf,String mf)throws IOException{
        int tp = 0 ;

        BufferedInputStream is = new BufferedInputStream(cpf);

        String extr = "e:/TC/";
        File cpfd =new File(extr+mf);
        System.out.println(extr+mf);
        if(!cpfd.exists()){
            cpfd.createNewFile();
        }
        FileOutputStream os = new FileOutputStream(cpfd);

        while((tp = is.read())!=-1){
            os.write(tp);
        }
        os.flush();
        is.close();
        os.flush();
    }
    */
    public static void main(String[] args){
        String fname = null;
        TC t = new TC();
        File f = new File("e:/TC");
        if(!f.exists()){
        f.mkdir();

        }

        for(int i =0 ;i<7;i++){
            f = new File("e:/TC/week"+i+".dat");
            if(!f.exists()){
                try {
                    f.createNewFile();

                    FileOutputStream foo = null;
                try {
                    foo = new FileOutputStream(f);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                DataOutputStream doo = new DataOutputStream(foo);
                for(int j = 0;j<20;j++){
                    try {
                        doo.writeUTF("nl");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }
        f = new File("e:/TC/help.txt");

        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            FileWriter foos = null;

            try {
                foos = new FileWriter(f);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        BufferedWriter bos = new BufferedWriter(foos);

        try {
            bos.write("1.you can select menubar to loacte week");
            bos.newLine();
            bos.write("2.double click table to edit this TableCourse");
            bos.newLine();
            bos.write("3.when you make it up,you must use 'Lock'menuitem to store the data you inputed");
            bos.newLine();
            bos.write("4.when the table is locked ,you can`t edit it ever until youclick 'UnLoak' menuitem");
            bos.flush();
            foos.close();
            bos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }

        t.launch();

    }

}

TC1.6SourceCode java课程表的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. leetcode.图.207课程表-Java

    1. 具体题目 现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1].给定 ...

  3. Java实现 LeetCode 630 课程表 III(大小堆)

    630. 课程表 III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d 天时要完成 ...

  4. Java实现 LeetCode 210 课程表 II(二)

    210. 课程表 II 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0, ...

  5. Java实现 LeetCode 207 课程表

    207. 课程表 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] ...

  6. java单向加密算法小结(2)--MD5哈希算法

    上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...

  7. 从爬取湖北某高校hub教务系统课表浅谈Java信息抓取的实现 —— import java.*;

    原创文章与源码,如果转载请注明来源. 开发环境:Myeclipse,依赖包:apache-httpclient . Jsoup.base64 一.概述 整个系统用Java开发.我们现在要做的是类似于超 ...

  8. Java实验2-数据库编程

    目标:掌握Java数据库编程 内容: 学生选课系统包括如下数据库表 学生表:Student(Sno,Sname,Ssex,Sage,Sdept) 课程表:Course(Cno,Cname,Ccredi ...

  9. Java 多线程(1)-Thread和Runnable

    一提到Java多线程,首先想到的是Thread继承和Runnable的接口实现 Thread继承 public class MyThread extends Thread { public void ...

随机推荐

  1. background-attachment 制造视差滚动效果案例

    简介 background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动.可能的值有三个: scroll 默认值.背景图像会随着页面其余部分的滚动而移动. fixed 当页 ...

  2. Java多线程并发01——线程的创建与终止,你会几种方式

    本文开始将开始介绍 Java 多线程与并发相关的知识,多谢各位一直以来的关注与支持.关注我的公众号「Java面典」了解更多 Java 相关知识点. 线程的创建方式 在 Java 中,用户常用的主动创建 ...

  3. tersserorc的简单使用

    tesserocr 是 python 的一个 OCR 库,它是对 tesseract 做的一层 Python API 封装,所以他的核心是tesseract. tesseract 的安装见 https ...

  4. (转)协议森林15 先生,要点单吗? (HTTP协议概览)

    协议森林15 先生,要点单吗? (HTTP协议概览) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们在TCP流通信中说明了, ...

  5. 网页外链用了 target="_blank",结果悲剧了

    今天给大家分享一个 Web 知识点.如果你有过一段时间的 Web 开发经验,可能已经知道了.不过对于刚接触的新手来说,还是有必要了解一下的. 我们知道,网页里的a标签默认在当前窗口跳转链接地址,如果需 ...

  6. Billboard HDU - 2795(树状数组,单点修改,区间查询)

    题目链接:https://vjudge.net/problem/HDU-2795 思路:h = 1e9行不通,因为广告是1*w的,所以n个广告最多只需要 h = n的高度,那么h=2e5就可以接受了. ...

  7. JSTL、请求转发和URL重定向

    JSTL 为什么要使用JSTL? 因为在JSP中写JAVA代码很麻烦,而JSTL可以简化在JSp中写JAva代码的流程 如何使用JSTL? 准备工作: ①将JSTL依赖的jar包导入工程的WEB-IN ...

  8. jenkins-gitlab-harbor-ceph基于Kubernetes的CI/CD运用(三)

    从最基础镜像到业务容器 构建 [为gitlab项目部署做铺垫] 业务镜像设计规划 目录结构 # pwd /data/k8s/app/myapp # tree . . ├── dockerfile │  ...

  9. iOS包重签名工具,ipa文件重签名,快速签名,SignTool签名工具,好用的签名工具,App重签名

    新工具 ProjectTool 已上线 这是一款快速写白包工具,秒级别写H5游戏壳包,可视化操作,极易使用,支持Swift.Objecive-C双语言 QQ交流群:811715780 进入 Proje ...

  10. css3笔记系列-3.css中的各种选择器详解,不看后悔系列

    点击上方蓝色字体,关注我 最详细的css3选择器解析 ​ 选择器是什么? 比较官方的解释:在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. 最常见的 CSS 选择器是元素选择器.换句话说 ...