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

📄 schedule.java

📁 约会管理系统
💻 JAVA
字号:
package P3;
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.*; 
import javax.swing.border.*;
import javax.swing.event.*; 
import javax.swing.table.*;

public class Schedule extends JPanel{
	private Calendar calendar; 
	private JPanel timePanel=new JPanel();
	private JLabel dateLabel=new JLabel();
	private JPanel schedulePanel=new JPanel();
	private String dateString;
	private AbstractTableModel daysModel;
	private JTable daysTable; 
	private Activities totalActivities;
	private JButton b1=new JButton("Update");
	private JPanel buttonPanel=new JPanel();
	private JPanel tablePanel=new JPanel();
	public DetailDialog detailPanel;
	
	private void setDate(){
		StringBuilder sb=new StringBuilder();
		sb.append(calendar.get(Calendar.YEAR));
		sb.append("-");
		sb.append(calendar.get(Calendar.MONTH)+1);
		sb.append("-");
		sb.append(calendar.get(Calendar.DATE));
		dateString=sb.toString();
	}
	public Schedule(Calendar calendar,Activities totalActivities){
		this.calendar=calendar;
		this.totalActivities=totalActivities;
	    setDate();
	    setLayout(new BorderLayout());
	    init();
	}
	public void update(){
		setDate();
		dateLabel.setText(dateString);
		daysModel.fireTableDataChanged();
	}
	public void init(){
		dateLabel.setText(dateString);
		timePanel.add(dateLabel,new FlowLayout());
		
		daysModel = new AbstractTableModel() { 
            public int getRowCount() { 
                return 24; 
            }

            public int getColumnCount() { 
                return 2; 
            }

            public String getColumnName(int column){
            	if(column==0) return "Hour";
            	else return "Schedule";
            }
            public Object getValueAt(int row, int column) { 
                if(column==0) return "  "+(row+1);
                int year=calendar.get(Calendar.YEAR);
                int day=calendar.get(Calendar.DAY_OF_YEAR);
                Activities aoy=(Activities) totalActivities.get(year);
                if(aoy!=null){
                	Activities aod=(Activities) aoy.get(day);
                	if(aod!=null){
                		if (aod.containsKey(row)){ 
                            return ((Activity) aod.get(row)).getTitle(); 
                        }
                	}
                }
                return null; 
            } 
        };
        daysTable = new JTable(daysModel); 
        daysTable.setCellSelectionEnabled(true);
        daysTable.setGridColor(Color.GRAY);
        daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        daysTable.setRowHeight(30);
        daysTable.getColumnModel().getColumn(0).setMaxWidth(40);

        daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() { 
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
                                                               boolean hasFocus, int row, int column) { 
                    String text = (value == null) ? "" : value.toString(); 
                    JLabel cell = new JLabel(text); 
                    cell.setOpaque(true); 
                    if (isSelected&&column==1) { 
                        cell.setForeground(Color.WHITE); 
                        cell.setBackground(Color.CYAN);
                    }else { 
                        cell.setForeground(Color.BLACK); 
                        cell.setBackground(Color.WHITE); 
                    } 
                    return cell; 
                } 
            });
        daysModel.fireTableDataChanged();
        schedulePanel.setLayout(new BorderLayout(10,10));
        schedulePanel.add(timePanel,BorderLayout.NORTH);
        tablePanel.add(new JScrollPane(daysTable));
        schedulePanel.add(tablePanel,BorderLayout.CENTER);
        b1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				DetailDialog dlg=new DetailDialog(null);
				dlg.setVisible(true);
                dlg.updateView();
			}
		});
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.add(b1);
		schedulePanel.add(buttonPanel,BorderLayout.SOUTH);
		add(schedulePanel,BorderLayout.CENTER);
	}
	
	public Activity getSelected(){
		int row=daysTable.getSelectedRow();
		if(row==-1) return null;
        int year=calendar.get(Calendar.YEAR);
        int day=calendar.get(Calendar.DAY_OF_YEAR);
        Activities aoy=(Activities) totalActivities.get(year);
        if(aoy!=null){
        	Activities aod=(Activities) aoy.get(day);
        	if(aod!=null){
        		if (aod.containsKey(row)){ 
                    return (Activity) aod.get(row); 
                }
        	}
        }
        return null;
	}
	
	private class DetailDialog extends JDialog{
		private JTextArea
		  title=new JTextArea(1,30),
		  location=new JTextArea(1,30),
		  description=new JTextArea(20,30);
		private JPanel
			titlePanel=new JPanel(),
			locationPanel=new JPanel(),
		    descriptionPanel=new JPanel();
		private JPanel textPanel=new JPanel();
		private JPanel buttonPanel=new JPanel();
		private JPanel containerPanel=new JPanel();
		private JButton
		  b1=new JButton("OK"),
		  b2=new JButton("Delete"),
		  b3=new JButton("Cancel");
		private JPanel headline=new JPanel();
		private Activity activity;
		private int hour;
		
		public DetailDialog(JFrame parent){
			hour=daysTable.getSelectedRow();
			activity=getSelected();
			if(activity==null)b2.setEnabled(false);
			else b2.setEnabled(true);
			b1.setEnabled(true);
			headline.add(new JLabel("Detail Information"));
			setLayout(new BorderLayout());
			add(BorderLayout.NORTH,headline);
			setEditable(true);
			titlePanel.setBorder(new TitledBorder("Title:"));
			locationPanel.setBorder(new TitledBorder("Location:"));
			descriptionPanel.setBorder(new TitledBorder("Description:"));
			titlePanel.add(title);
			locationPanel.add(location);
			descriptionPanel.add(new JScrollPane(description));
			textPanel.setLayout(new BorderLayout());
			textPanel.add(BorderLayout.NORTH,titlePanel);
			textPanel.add(BorderLayout.CENTER,locationPanel);
			textPanel.add(BorderLayout.SOUTH,descriptionPanel);
			textPanel.setBorder(new TitledBorder(""));
			initButton();

			buttonPanel.add(b1);
			buttonPanel.add(b2);
			buttonPanel.add(b3);
			containerPanel.setBorder(new CompoundBorder(new MatteBorder(0,0,0,10,getBackground()),new LineBorder(Color.LIGHT_GRAY)));
			containerPanel.setLayout(new BorderLayout(10,10));
			containerPanel.add(BorderLayout.NORTH,textPanel);
			containerPanel.add(BorderLayout.CENTER,buttonPanel);
			add(BorderLayout.CENTER,containerPanel);
			setSize(500,630);
		}
		public void updateView(){
			setActivity(getSelected());
        	setHour(daysTable.getSelectedRow());
			if(hour==-1){
				b1.setEnabled(false);
				title.setText("");
				location.setText("");
				description.setText("");
				return;
			}
			b1.setEnabled(true);
			if(activity==null){
				title.setText("");
				location.setText("");
				description.setText("");
			}
			else{
				title.setText(activity.getTitle());
				location.setText(activity.getLocation());
				description.setText(activity.getDescription());
			}
		}
		
		private void setEditable(boolean b){
			title.setEditable(b);
			location.setEditable(b);
			description.setEditable(b);
		}
		public void setHour(int hour){
			this.hour=hour;
		}
		public void setActivity(Activity activity){
			this.activity=activity;
			if(activity==null)b2.setEnabled(false);
			else b2.setEnabled(true);
		}
		public void initButton(){
			b1.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					String t=title.getText().trim();
					String l=location.getText().trim();
					String d=description.getText().trim();
					if(t.equals("")||l.equals("")||d.equals("")){
						JOptionPane.showMessageDialog(null,"Information incomplete!","Warning!",JOptionPane.WARNING_MESSAGE);
					}else{
						if(activity==null){
							activity=new Activity();
							int year=calendar.get(Calendar.YEAR);
			                int day=calendar.get(Calendar.DAY_OF_YEAR);
							activity.setDate(calendar.get(Calendar.DAY_OF_MONTH));
							activity.setMonth(calendar.get(Calendar.MONTH)+1);
							activity.setYear(calendar.get(Calendar.YEAR));
							activity.setHours(hour);
							activity.setTitle(t);
							activity.setDescription(d);
							activity.setLocation(l);
							Activities aoy=(Activities) totalActivities.get(year);
			                if(aoy==null){
			                	aoy=new Activities();
			                	totalActivities.put(year,aoy);
			                }
			               	Activities aod=(Activities) aoy.get(day);
			                if(aod==null){
			                	aod=new Activities();
			                	aoy.put(day, aod);
			                }
			                aod.put(hour,activity);
						}
						else{
							activity.setDate(calendar.get(Calendar.DAY_OF_MONTH));
							activity.setMonth(calendar.get(Calendar.MONTH)+1);
							activity.setYear(calendar.get(Calendar.YEAR));
							activity.setHours(hour);
							activity.setTitle(t);
							activity.setDescription(d);
							activity.setLocation(l);
						}
						daysTable.setValueAt(activity.getTitle(), hour, 1);
						daysModel.fireTableDataChanged();
						activity=null;
						hour=-1;
						updateView();
						b1.setEnabled(false);
						setEditable(false);
					}
					dispose();
				}
			});
			b2.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					if(activity!=null){
						Object[] options={"Yes","No"};
						int sel=JOptionPane.showOptionDialog(
								null, "Sure to delete ?","Remind",JOptionPane.DEFAULT_OPTION,
								JOptionPane.WARNING_MESSAGE, null, options, options[0]);
						if(sel!=JOptionPane.CLOSED_OPTION&&sel!=1){
							activity=null;
							((Activities) ((Activities) totalActivities.get(calendar.get(Calendar.YEAR))).get(calendar.get(Calendar.DAY_OF_YEAR))).remove(hour);
							b2.setEnabled(false);
							b1.setEnabled(false);
							daysTable.setValueAt(null, hour, 1);
							hour=-1;
							daysModel.fireTableDataChanged();
							updateView();
							setEditable(false);
						}						
					}
					dispose();
				}
			});
			b3.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					dispose();
				}
			});
		}
	}
	public static void main(String[] args){
		DateOrganizerSystem dateOrganizerSystem=new DateOrganizerSystem();
		ExecutorService exec=Executors.newCachedThreadPool();
		exec.execute(dateOrganizerSystem);
		DateOrganizerSystem.run(dateOrganizerSystem);
		exec.shutdown();
	}
}

⌨️ 快捷键说明

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