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

📄 gante.java

📁 简单的甘特图 数据库用XML
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.xml.parsers.*;
import org.apache.crimson.tree.*;
import org.w3c.dom.*;

public class GanTe extends JFrame implements ActionListener
{
	private int width,height;
	private JLabel ID,Task,Start,End;
	private JButton Display,Submit,Gantt;
	private JTextField text[];
	private JPanel p1,p2;
	private int id,cdate;
	private String t[];
	private int asdf[];
	
	public int getCdate()
	{
		return cdate;	
	}
	
	public String[] getT()
	{
		return t;	
	}
	
	public int[] getAsdf()
	{
		return asdf;	
	} 

	public GanTe()
	{
		super("Gantt Chart");
			
		ID=new JLabel("            ID");
		Task=new JLabel("            Task");
		Start=new JLabel("   Start (dd-mm-yy)");
		End=new JLabel("   End (dd-mm-yy)");

		Display=new JButton("Display");
		Submit=new JButton("Submit");
		Gantt=new JButton("Gantt");

		Display.addActionListener(this);
		Submit.addActionListener(this);
		Gantt.addActionListener(this);
		
		text=new JTextField[40];
		
		p1=new JPanel();
		p2=new JPanel();
		
		Container c=getContentPane();
		
		p1.setLayout(new GridLayout(11,4));
		p2.setLayout(new FlowLayout());
		
		p1.add(ID);
		p1.add(Task);
		p1.add(Start);
		p1.add(End);
		
		for(int i=0;i<40;i++)
		{
			text[i]=new JTextField();
			p1.add(text[i]);	
		}
		
		p2.add(Display);
		p2.add(Submit);
		p2.add(Gantt);
		
		c.add(p1,BorderLayout.CENTER);
		c.add(p2,BorderLayout.SOUTH);
		
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);	
			}	
		});
				
		width=getToolkit().getScreenSize().width;
      	height=getToolkit().getScreenSize().height;
      	setBounds(width/4,height/4,width/2,height/2);
      	setResizable(false);
      	show();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("Display"))
		{
			File f=new File("aaa.xml");
			if(f.isFile())
			{
				try
				{
    				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    				DocumentBuilder builder=factory.newDocumentBuilder();
    				Document doc=builder.parse("aaa.xml");

    				NodeList nl =doc.getElementsByTagName("Data");
    
    				for (int i=0;i<nl.getLength();i++)
    				{
      					Element node=(Element) nl.item(i);
      					text[i*4].setText(node.getElementsByTagName("ID")
      						.item(0).getFirstChild().getNodeValue());
      					text[i*4+1].setText(node.getElementsByTagName("Task")
      						.item(0).getFirstChild().getNodeValue());
      					text[i*4+2].setText(node.getElementsByTagName("Start")
      						.item(0).getFirstChild().getNodeValue());
      					text[i*4+3].setText(node.getElementsByTagName("End")
      						.item(0).getFirstChild().getNodeValue());

					}
				}
				catch(Exception x)
				{
					x.printStackTrace();	
				}
			}
			else
			{
				JOptionPane.showMessageDialog(null,"没有找到XML文件!");	
			}
		}
		if(e.getActionCommand().equals("Submit"))
		{
			id=1;
			
				for(int i=0;i<40;i++)
				{
					if(i%4==0)
					{
						if(i!=0)
						{
							if(text[i].getText().length()==0)
							{
								WriteXml(id-1);
								cdate=sdate(text[2].getText(),text[i-1].getText());
								break;
							}
							else
							{
								if(text[i].getText().equals(Integer.toString(id)))
								{
									//System.out.println(id);
									id++;	
									continue;
								}
								else
								{
									//System.out.println(id);
									Error("ID");
									break;	
								}
							}	
						}
						if(text[i].getText().length()==0)
						{
							Error("ID");
							break;
						}
						else
						{
							if(text[i].getText().equals(Integer.toString(id)))
							{
								id++;	
								//System.out.println(id);
							}
							else
							{
								Error("ID");
								break;	
							}
						}
						
					
					}
				
					if(i%4==1)
					{
						if(text[i].getText().length()==0)
						{
							Error("Task");
							break;
						}
					}
				
					if(i%4==2)
					{
						if(!isdate(i))
						{
							Error("Start");
							break;
						}
						else
						{
							if(i!=2)
							{
								if(Integer.parseInt(text[i-3].getText().substring(0,2))!=30)
								{
									if(Integer.parseInt(text[i].getText().substring(3,5))==
										Integer.parseInt(text[i-3].getText().substring(3,5))&&
										Integer.parseInt(text[i].getText().substring(0,2))-1==
										Integer.parseInt(text[i-3].getText().substring(0,2)))
									{
									
									}
									else
									{
										Error("Start");
										break;	
									}	
								}
								else
								{
									if(Integer.parseInt(text[i].getText().substring(3,5))-1==
									   Integer.parseInt(text[i-3].getText().substring(3,5))&&
									   Integer.parseInt(text[i].getText().substring(0,2))==01)
									{
										
									}
									else
									{
										Error("Start");
										break;	
									}	
								}	
							}
						}
					}
					if(i%4==3)
					{
						if(isdate(i)==true)
						{
							if(Integer.parseInt(text[i].getText().substring(3,5))>
									Integer.parseInt(text[i-1].getText().substring(3,5)))
							{
								if(i==39)
								{
									WriteXml(id-1);
									cdate=sdate(text[2].getText(),text[i-1].getText());
								}
							}
							else if(Integer.parseInt(text[i].getText().substring(3,5))==
									Integer.parseInt(text[i-1].getText().substring(3,5)))
							{
								if(Integer.parseInt(text[i].getText().substring(0,2))>=
									Integer.parseInt(text[i-1].getText().substring(0,2)))
								{
									if(i==39)
									{
										WriteXml(id-1);
										cdate=sdate(text[2].getText(),text[i-1].getText());
									}
								}
								else
								{
									Error("End");
									break;	
								}	
							}			
								
							else
							{
								Error("End");
								break;	
							}
						}
						else
						{
							Error("End");
							break;
						}	
					}	
				//System.out.println(i);	
				}

		}

	
		
		if(e.getActionCommand().equals("Gantt"))
		{
			asdf=new int[10];
			String start="",end="";
			File f=new File("aaa.xml");
			if(f.isFile())
			{
				try
				{
    				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    				DocumentBuilder builder=factory.newDocumentBuilder();
    				Document doc=builder.parse("aaa.xml");

    				NodeList nl =doc.getElementsByTagName("Data");
    				t=new String[nl.getLength()];
    				for (int i=0;i<nl.getLength();i++)
    				{
      					Element node=(Element)nl.item(i);

      					t[i]=node.getElementsByTagName("Task")
      					.item(0).getFirstChild().getNodeValue();

						asdf[i]=sdate(node.getElementsByTagName("Start")
      						.item(0).getFirstChild().getNodeValue(),
      						node.getElementsByTagName("End")
      						.item(0).getFirstChild().getNodeValue());
      					//System.out.println(asdf[i]);
      				
      					if(i==0)
      					{
      						start=node.getElementsByTagName("Start")
      									.item(0).getFirstChild().getNodeValue();
      					}
      					if(i==nl.getLength()-1)
      					{
      						end=node.getElementsByTagName("End")
      									.item(0).getFirstChild().getNodeValue();	
      					}
      				
      				}

      				cdate=sdate(start,end);
      			
    			}
    			catch(Exception x)
				{
					x.printStackTrace();
				}
			
				new Gantt(this);
			}
			else
			{
				JOptionPane.showMessageDialog(null,"没有找到XML文件!");	
			}
		}
		
	}
	
	public boolean isdate(int i)
	{
		if(text[i].getText().length()==8)
		{
			if(Integer.parseInt(text[i].getText().substring(6,8))==04)
			{
				if(Integer.parseInt(text[i].getText().substring(3,5))>=1&&
					Integer.parseInt(text[i].getText().substring(3,5))<=12)
				{
					if(Integer.parseInt(text[i].getText().substring(0,2))>=1&&
						Integer.parseInt(text[i].getText().substring(0,2))<=30)	
					{
						return true;	
					}
					else
					{
						return false;
					}
				}
				else
				{
					return false;	
				}
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;	
		}
	}	
	
	public void Error(String s)
	{	
		JOptionPane.showMessageDialog(null,s+"输入有误");
		
	}
	
	public void WriteXml(int t)
	{
		Document doc;
		Element Gantt,Data; 
		Element ID = null; 
		Element Task = null; 
		Element Start = null; 
		Element End = null;

		try
		{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder=dbf.newDocumentBuilder();
			doc = builder.newDocument();
			if (doc != null) 
			{ 
				Gantt = doc.createElement("Gantt"); 
				
				for(int j=1;j<=t;j++)
				{
					Data = doc.createElement("Data");
					//Data.setAttribute("No",String.valueOf(j)); 
					Gantt.appendChild(Data);

					ID = doc.createElement("ID");
					ID.appendChild(doc.createTextNode(text[(j-1)*4].getText()));
					Data.appendChild(ID);

					Task = doc.createElement("Task");
					Task.appendChild(doc.createTextNode(text[(j-1)*4+1].getText()));
					Data.appendChild(Task);

					Start = doc.createElement("Start");
					Start.appendChild(doc.createTextNode(text[(j-1)*4+2].getText()));
					Data.appendChild(Start);

					End = doc.createElement("End");
					End.appendChild(doc.createTextNode(text[(j-1)*4+3].getText()));
					Data.appendChild(End);

				}	
				
				doc.appendChild(Gantt);
				JOptionPane.showMessageDialog(null,String.valueOf(t)+"条记录添加成功!");
				((XmlDocument)doc).write(new FileOutputStream("aaa.xml"));


			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
	
	public int sdate(String start,String end)
	{
		int s;
		s=(Integer.parseInt(end.substring(3,5))-Integer.parseInt(start.substring(3,5)))*30;
		s=s+(Integer.parseInt(end.substring(0,2))-Integer.parseInt(start.substring(0,2)))+1;
		return s;
	}
	
	public static void main(String args[])
	{
		new GanTe();

	}
}

class Gantt extends JFrame
{
	private int index;
	private JLabel  task[];
	private Label date[][];
	private JPanel pa1,pa2;
	private JTextField text[];
	private Color color[]={Color.red,Color.blue,Color.green,Color.yellow,Color.black,
							Color.cyan,Color.gray,Color.lightGray,Color.magenta,Color.orange};
	public Gantt(GanTe gan)
	{
		super("Gantt Chart");
		index=0;
		//System.out.println(gan.getT().length);
		//System.out.println(gan.getCdate());
		text=new JTextField[gan.getCdate()];
		task=new JLabel[gan.getT().length+1];
		task[gan.getT().length]=new JLabel();
		pa1=new JPanel();
		pa2=new JPanel();

		date=new Label[gan.getT().length][gan.getCdate()];

		for(int i=0;i<gan.getCdate();i++)
		{
			text[i]=new JTextField();
			text[i].setEnabled(false);
			pa2.add(text[i]);
			if((i+1)%7==0)
			{
				text[i].setBackground(Color.BLACK);
			}
		}

		pa1.add(task[gan.getT().length]);
		for(int i=0;i<gan.getT().length;i++)
		{
			for(int j=0;j<gan.getCdate();j++)
			{
				date[i][j]=new Label();
				pa2.add(date[i][j]);

			}
			task[i]=new JLabel(gan.getT()[i]);
			pa1.add(task[i]);
		}
		
		for(int i=0;i<gan.getT().length;i++)
		{
			for(int j=0;j<gan.getAsdf()[i];j++)
			{
				//System.out.println(index);
				date[i][index].setBackground(color[i]);
				index++;

			}

		}

		Container c=getContentPane();
		pa1.setLayout(new GridLayout(gan.getT().length+1,1,10,10));	
		pa2.setLayout(new GridLayout(gan.getT().length+1,gan.getCdate(),0,50));
		
		
		c.add(pa1,BorderLayout.WEST);
		c.add(pa2,BorderLayout.CENTER);
		setSize(800,gan.getT().length*70);
		show();
	}	
	
	
}

⌨️ 快捷键说明

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