Giaodien
Giaodien
public class Giaodien extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private ListCSDL DSloinhac; private JPanel Lam_viec; DateFormat dinhdang = new SimpleDateFormat("hh:mm:ss a dd/MM/yyyy"); Date Ngaycapnhat; public JLabel thoigianht = new JLabel("Hien thi thoi gian"); public JTextField Loinhac = new JTextField(15); public JComboBox Gio = new JComboBox(); public JComboBox Phut = new JComboBox(); public JComboBox Sc = new JComboBox(); public JButton Them; public JButton Xoa; public JButton Thaychuong; JFileChooser fc; private JTable table; public JScrollPane scroll; private DefaultTableModel dtm; public JLabel thoigianclick; int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH); int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR); int dayht=0,monthht=0,yearht=0; String day = ""; JPanel d; JLabel l = new JLabel("", JLabel.CENTER); JButton[] button = new JButton[49]; String fileNhac = "Chuong.mp3"; public JPanel Hienthi(){ Lam_viec = new JPanel(); Lam_viec.setLayout(null); thoigianht= new JLabel(""); capnhatNgayGio(); thoigianht.setBounds(70,10,280,25); thoigianht.setFont(new Font("Tahoma", Font.BOLD, 20)); Lam_viec.add(thoigianht); JLabel Lloinhac = new JLabel("Li nhc"); Lloinhac.setBounds(70, 40, 50, 25); Lam_viec.add(Lloinhac); Loinhac = new JTextField(15); Loinhac.setBounds(130, 40, 200, 25); Lam_viec.add(Loinhac); Loinhac.setDocument(new JTextFieldLimit(10)); String gio[] = {"01","02","03","04","05","06","07","08","09","10","11","12"}; String phut[] = {"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14"," 15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"
,"31","32","33","34","35","36","37","38","39","40","41","42","43","44","45"," 46","47","48","49","50","51","52","53","54","55","56","57","58","59"}; String sc[] ={"AM","PM"}; JLabel Lgio = new JLabel("Hn gi"); Lgio.setBounds(70, 75, 50, 25); Lam_viec.add(Lgio); Gio = new JComboBox(gio); Gio.setBounds(130, 75, 50, 25); Lam_viec.add(Gio); Phut = new JComboBox(phut); Phut.setBounds(190, 75, 50, 25); Lam_viec.add(Phut); Sc = new JComboBox(sc); Sc.setBounds(250, 75, 60, 25); Lam_viec.add(Sc); //////////////////////////////////////// Date aaaa = new Date(); String ht = dinhdang.format(aaaa); String giocs = ht.trim(); String []test = giocs.split(" "); String []t2 = test[0].split(":"); Gio.setSelectedIndex(Integer.parseInt(t2[0])-1); Phut.setSelectedIndex(Integer.parseInt(t2[1])); if(test[1].equals("AM")==true){ Sc.setSelectedIndex(0); }else{ Sc.setSelectedIndex(1); } ///////////////Lch ngy//////////// JLabel Lngay = new JLabel("Hn ngy"); Lngay.setBounds(10, 110, 70, 25); Lam_viec.add(Lngay); String now = new SimpleDateFormat("dd/MM/yyyy").format(Ngaycapnhat); thoigianclick = new JLabel(now); thoigianclick.setBounds(130, 110, 150, 25); Lam_viec.add(thoigianclick); String[] header2 = { "CN", "T.2", "T.3", "T.4", "T.5", "T.6", "T.7" }; JPanel p1 = new JPanel(new GridLayout(7, 7)); p1.setPreferredSize(new Dimension(430, 120)); for (int x = 0; x < button.length; x++) { final int selection = x; button[x] = new JButton(); button[x].setFocusPainted(false); button[x].setBackground(Color.white); if (x > 6) button[x].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if(button[selection].getText()==""){ JOptionPane.showMessageDialog(null, "error"); } else{ //JOptionPane.showMessageDialog(nul l, "error 2"); day = button[selection].getActionCommand();
); }
thoigianclick.setText(setPickedDate() Xoa.setEnabled(false);
} JPanel p2 = new JPanel(new GridLayout(1, 3)); JButton previous = new JButton("<< Thng trc"); previous.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { month--; dayht=0; displayDate(); } }); p2.add(previous); p2.add(l); JButton next = new JButton("Thng sau >>"); next.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { month++; dayht=0; displayDate(); } }); p2.add(next); p1.setBounds(10, 140, 410, 150); p2.setBounds(10, 300, 400, 20); Lam_viec.add(p1, BorderLayout.CENTER); Lam_viec.add(p2, BorderLayout.SOUTH); //displayDate(); ////////////////table///////////////// String []header = {"Thi gian","Li nhc"}; dtm=new DefaultTableModel(header,0); table=new JTable(dtm); TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dtm); table.setRowSorter(sorter); table.getRowSorter().toggleSortOrder(0); scroll = new JScrollPane(table); scroll.setBounds(20, 340, 265, 180); Lam_viec.add(scroll); table.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { try { Xoa.setEnabled(true); int row=table.getSelectedRow(); String ln = table.getValueAt(row, 1).toString(); String giocs = table.getValueAt(row, 0).toString(); String []test = giocs.split(" ");
String []t2 = test[0].split(":"); String []t3 = test[2].split("/"); year = Integer.parseInt(t3[2]); month= Integer.parseInt(t3[1])-1; dayht= Integer.parseInt(t3[0]); displayDate(); Loinhac.setText(ln); Gio.setSelectedItem(t2[0]); Phut.setSelectedItem(t2[1]); Sc.setSelectedItem(test[1]); thoigianclick.setText(test[2]); } catch (Exception e1) { JOptionPane.showMessageDialog(null,"sai"); } } }); napCSDL(); displayDate(); //////////////////////Button//////////////////////// Them = new JButton("Thm"); Them.setBounds(305,370,100,25); Lam_viec.add(Them); //////click them////////////// Them.addActionListener(this); //////////////////// Xoa = new JButton("Xa"); Xoa.setBounds(305,420,100,25); Lam_viec.add(Xoa); Xoa.setEnabled(false); Xoa.addActionListener(this); Thaychuong = new JButton("i Chung"); Thaychuong.setBounds(305, 455, 100, 25); Lam_viec.add(Thaychuong); Thaychuong.addActionListener(this); fc = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("MP3", fc.setFileFilter(filter); return Lam_viec; } @SuppressWarnings("unused") public void actionPerformed(ActionEvent e) { if(e.getSource().equals(Them)) { try { String loinhac = Loinhac.getText(); String gio=Gio.getSelectedItem().toString(); String phut=Phut.getSelectedItem().toString(); String sc=Sc.getSelectedItem().toString(); String ngaygio = gio + ":" + phut + ":00 " + sc + " " + thoigianclick.getText(); //Date tgThem = (Date)dinhdang.parse(ngaygio); Date date1 = new Date(); Date tgThem=null; try { DateFormat formatter ;
"mp3");
dd/MM/yyyy");
thng");
} long diff = tgThem.getTime() - date1.getTime(); if(diff <= 0 || Loinhac.getText().equals("")==true) { JOptionPane.showMessageDialog(null,diff +" Nhp sai "+Ngaycapnhat.getTime()); } else { CSDL p=new CSDL(ngaygio,loinhac); String []row= {ngaygio,loinhac}; DSloinhac.ThemLoinhac(p); dtm.addRow(row); LuuCSDL(); JOptionPane.showMessageDialog(null," thm li nhc: '" + loinhac + "'\n lc:'" + ngaygio +"\n vo lch ca bn" ); Loinhac.setText(""); displayDate(); } /**/ }catch (Exception ex) { JOptionPane.showMessageDialog(null, "Li: \n" + ex.getMessage()); } } if(e.getSource().equals(Xoa)) { try{ if (table.getSelectedRowCount() > 0) { int psnhac = JOptionPane.showConfirmDialog(null,"Bn c thc s mun xa hay khng","Thng bo mi",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE); if(psnhac==0) { int realIndex = table.convertRowIndexToModel(table.getSelectedRow()); CSDL p=DSloinhac.GetCSDL(realIndex); dtm.removeRow(realIndex); DSloinhac.XoaLoinhac(p); XoaDL xoa = new XoaDL(p.getThoigian() +";"+p.getLoinhac()); displayDate(); Loinhac.setText(""); Xoa.setEnabled(false); table.repaint(); Date aaaa = new Date(); String ht = dinhdang.format(aaaa); String giocs = ht.trim(); String []test = giocs.split(" "); String []t2 = test[0].split(":");
Gio.setSelectedIndex(Integer.parseInt(t2[0])-1); Phut.setSelectedIndex(Integer.parseInt(t2[1])); if(test[1].equals("AM")==true){ Sc.setSelectedIndex(0); }else{ Sc.setSelectedIndex(1); } } } }catch(Exception ex){ JOptionPane.showMessageDialog(null, "Li \n" + ex.getMessage()); } } if (e.getSource() == Thaychuong) { int returnVal = fc.showOpenDialog(Giaodien.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); if(file.getName().endsWith(".MP3")==true || file.getName().endsWith(".mp3")==true){ fileNhac=""+file.getPath(); JOptionPane.showMessageDialog(null, " chn file " + file.getName()); } else JOptionPane.showMessageDialog(null, "Chn ng file nhc"); } } } public void displayDate() { for (int x = 7; x < button.length; x++){ button[x].setForeground(Color.black); button[x].setText(""); } SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM / yyyy"); Calendar cal = Calendar.getInstance(); cal.set(year, month, 1); l.setText("Thng " + sdf.format(cal.getTime())); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //napCSDL(); for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++,
day++){
[2].split("/"); + thoigiancsdl[2];
button[x].setForeground(Color.RED); } for(int y =0;y<DSloinhac.Count();y++){ CSDL p=DSloinhac.GetCSDL(y); String []thoigiancsdl = p.getThoigian().split(" ") String ngaycsdl = thoigiancsdl[0]; String thangnamcsdl = "Thng "+thoigiancsdl[1] + " / "
if(button[x].getText().equals(""+ngaycsdl)==true && l.getText().equals(""+thangnamcsdl)==true) button[x].setForeground(Color.GREEN); } } } public String setPickedDate() { if (day.equals("")) return day; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "dd/MM/yyyy"); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.set(year, month, Integer.parseInt(day)); return sdf.format(cal.getTime()); } void napCSDL(){ DSloinhac =new ListCSDL(); try { File f=new File("fileCSDL.dat"); if(!f.exists()) f.createNewFile(); Scanner input=new Scanner(new FileInputStream(f)); while(input.hasNextLine()) { String line=input.nextLine(); if(line.trim()!="") { String items[]=line.split(";"); CSDL p=new CSDL(); p.setThoigian(items[0]); p.setLoinhac(items[1]); DSloinhac.ThemLoinhac(p); dtm.addRow(items); } } input.close(); } catch (Exception e) { e.printStackTrace(); }
void LuuCSDL() { try { PrintWriter out =new PrintWriter( new FileOutputStream("fileCSDL.dat"),true); for (int i = 0; i < DSloinhac.Count(); i++) { CSDL p=DSloinhac.GetCSDL(i); out.println(p);
public void capnhatNgayGio() { Thread th = new Thread(new Runnable() { @SuppressWarnings("unused") public void run() { while(true) { Ngaycapnhat = new java.util.Date(); String dateTime = dinhdang.format(Ngaycapnhat); try { for (int i = 0; i < DSloinhac.Count(); i++) { CSDL p=DSloinhac.GetCSDL(i); if((dinhdang.parse(p.getThoigian()).getTime()Ngaycapnhat.getTime())/1000==0) { MP3 sds = new MP3(fileNhac); sds.play(); setVisible(true); setExtendedState(JFrame.NORMAL); String []options={"C","Khng"}; int psnhac=JOptionPane.showOptionDialog(null,p.getLoinhac()+"\n Bo li sau 1 pht ?","Thng bo mi",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,opti ons[0]); if(psnhac==1) { sds.stop(); String []row= {p.getThoigian(),p.getLoinhac()}; DSloinhac.XoaLoinhac(p); dtm.removeRow(i); XoaDL xoa = new XoaDL(p.getThoigian() +";"+p.getLoinhac()); displayDate(); table.repaint(); } else { sds.stop(); long dl = Ngaycapnhat.getTime()+61000l; String sau = dinhdang.format(new Date(dl)); String []row= {p.getThoigian(),p.getLoinhac()}; DSloinhac.XoaLoinhac(p);
dtm.removeRow(i); Thread.sleep(1000); SuaDL sua = new SuaDL(p.getThoigian()+";" +p.getLoinhac(),sau+";" +p.getLoinhac()); CSDL p1=new CSDL(sau,p.getLoinhac()); String []row1= {sau,p.getLoinhac()}; DSloinhac.ThemLoinhac(p1); dtm.addRow(row1); displayDate(); table.repaint(); } } } } catch (Exception e) { e.printStackTrace(); } thoigianht.setText(dateTime); thoigianht.updateUI(); } } }); th.start(); } public Giaodien(){ super("Lch bn"); setIconImage(Toolkit.getDefaultToolkit().getImage("icon.gif")); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); this.setBounds(dim.width/2 - 220, dim.height/2 -300, 440, getContentPane().add(Hienthi()); this.setResizable(false); } { public static void main(String args[]) Giaodien Main_win=new Giaodien(); Main_win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Main_win.setVisible(true);
570);
} }