Uygulamanın tamamını indir

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Calistir extends JFrame implements ActionListener {
       String[] lbl_txt= {"Takım İsmi","Puan","Averaj","A Gol","Y Gol ","O Mac"};
       Connection baglanti;
       DefaultTableModel dtm; 
       JTable tbl;
        DefaultListModel<Integer>[] dlm;
        DefaultListModel<String> tkm_ad;
        DefaultListModel<String> tkm_isim;
        DefaultListModel<Integer> tkm_id;
        DefaultListModel<Integer>[] deger;
        DefaultListModel<Integer>[] yeni_deger;
   
        int[][] degerler; 
	public Calistir() {
		this.setTitle("Takım Bilgileri");
		this.setSize(450, 600);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
	    
		JPanel jpn_main=new JPanel();
		jpn_main.setPreferredSize(new Dimension(350, 600));
		jpn_main.setLayout(new FlowLayout());
		
		dtm=new DefaultTableModel();
		dtm.setColumnIdentifiers(lbl_txt);
		
		dlm=new DefaultListModel[4];
		deger=new DefaultListModel[lbl_txt.length-1];
		yeni_deger=new DefaultListModel[lbl_txt.length-1];
		
		tkm_ad=new DefaultListModel<String>();
	    tkm_isim=new DefaultListModel<String>();
		tkm_id=new DefaultListModel<Integer>();
	
	   for(int i=0;i<dlm.length;i++) {
		         dlm[i]=new DefaultListModel();
		     
	   }
	   for(int i=0;i<yeni_deger.length;i++) {
	         yeni_deger[i]=new DefaultListModel<Integer>();
	         deger[i]=new DefaultListModel<Integer>();
 }
	  
		         listdoldur();
		int yukseklik= dtm.getDataVector().size()*18;
		tbl=new JTable(dtm);
	    tbl.getColumnModel().getColumn(0).setPreferredWidth(100);
	    
	   for(int i=1;i<lbl_txt.length;i++)
		   tbl.getColumnModel().getColumn(i).setPreferredWidth(40);
	   
	    JScrollPane jscp=new JScrollPane(tbl);
	    jscp.setPreferredSize(new Dimension(300,yukseklik));
	     jscp.setMaximumSize(new Dimension(300, 500));
	    jpn_main.add(jscp);
	    
		JButton btn=new JButton("Maçlar");
		btn.setPreferredSize(new Dimension(100, 25));
		btn.addActionListener(this);
		jpn_main.add(btn);
		
		this.add(jpn_main);
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
	    this.setVisible(false);   
		new Maclar(this);
	}
	public void baglan() {
		try {
			Class.forName("org.sqlite.JDBC");
			baglanti=DriverManager.getConnection("jdbc:sqlite::resource:Odev.db");				
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	int sayac=0;
	public void listdoldur() {
		try {
			sayac=0;
			PreparedStatement sorgu=null;
			ResultSet veri=null;
			baglan();
			 sorgu=baglanti.prepareStatement("select * from Takimlar ");
			 veri=sorgu.executeQuery();
			  tkm_ad.clear();
			  tkm_id.clear();
			  
			  while(veri.next()) {
				tkm_ad.addElement(veri.getString("T_Ad"));
				tkm_id.addElement(veri.getInt("ID"));
			sayac++;
			  }
			veri.close();
			veri=null;
			 baglanti.close();
			 baglan();
			 sorgu=baglanti.prepareStatement("select * from Skorlar ");
			 veri=sorgu.executeQuery();
			 for(int i=0;i<dlm.length;i++)
				    dlm[i].clear();
			 while(veri.next()) {
				 dlm[0].addElement(veri.getInt("Takim1"));
				 dlm[1].addElement(veri.getInt("Takim2"));
				 dlm[2].addElement(veri.getInt("Skor1"));
				 dlm[3].addElement(veri.getInt("Skor2"));
			 }
			 veri.close();
			 baglanti.close();
			 degerler=new int[sayac][5];
			 hesapla();
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage()+"HATA Calistir");
		}	
	}
	public void hesapla() {
	          
		for(int i=0;i<dlm[2].size();i++) {
				int[] skor= {dlm[2].getElementAt(i),dlm[3].getElementAt(i)};
		int[] takim= {dlm[0].getElementAt(i),dlm[1].getElementAt(i)};
        int[] puan= {0,0},averaj= {0,0},y_gol= {skor[1],skor[0]};
		           if(skor[0]==skor[1]) {
                	puan[0]=1;
                	puan[1]=1;  
                   }			
                   else if(skor[0]>skor[1]) {
                	   puan[0]=3; 
                       puan[1]=0;
                   }	   
                   else {
                	   puan[0]=0;
                       puan[1]=3;
                   }
		           averaj[0]=skor[0]-skor[1];
		           averaj[1]=skor[1]-skor[0];
		   
		        for(int j=0;j<takim.length;j++) {
		        	for(int k=0;k<tkm_id.size();k++) {
		        		  if(takim[j]==tkm_id.getElementAt(k))
		        		  {    
		        			 degerler[k][0]+=puan[j];
		        			 degerler[k][1]+=averaj[j];
		        			 degerler[k][2]+=skor[j];
		        			 degerler[k][3]+=y_gol[j];
		        			 degerler[k][4]+=1;
		        		
		        			  break;
		        		  }
		        	}        	
		       }
		       
		}
		
		for(int i=0;i<sayac;i++) {

			deger[0].addElement(  degerler[i][0]);
			deger[1].addElement( degerler[i][1]);
			deger[2].addElement(degerler[i][2]);
			deger[3].addElement( degerler[i][3]);
			deger[4].addElement( degerler[i][4]);
      }  
		
		 int indis=0;
	        tkm_isim.clear();
	        for(int i=0;i<yeni_deger.length;i++)
	        	yeni_deger[i].clear();
	      int syc=0;
			  while(deger[0].size()!=0) {
				 int buyuk=deger[0].getElementAt(0);
				 indis=0;
				for(int j=1;j<deger[0].size();j++) {
			               if(deger[0].getElementAt(j)>buyuk) 
			               {   
			                   buyuk=deger[0].getElementAt(j);
			            	   indis=j;
			                 
			               }
				}
			
			   tkm_isim.addElement( tkm_ad.getElementAt(indis));
					yeni_deger[0].addElement (deger[0].getElementAt(indis));
					yeni_deger[1].addElement (deger[1].getElementAt(indis));
					yeni_deger[2].addElement(deger[2].getElementAt(indis));
					yeni_deger[3].addElement (deger[3].getElementAt(indis));
					yeni_deger[4].addElement (deger[4].getElementAt(indis));
			
				  for(int k=0;k<deger.length;k++)
	                  deger[k].remove(indis);
				     tkm_ad.remove(indis);
				    
			 }
		  dtm.getDataVector().removeAllElements();//Table tamamen temizlemek için
		  dtm.fireTableDataChanged();
		  for(int i=0;i<tkm_isim.size();i++) {
				
	    	  String[] veriler= {tkm_isim.getElementAt(i),yeni_deger[0].getElementAt(i)+"",yeni_deger[1].getElementAt(i)+"",yeni_deger[2].getElementAt(i)+"",yeni_deger[3].getElementAt(i)+"",yeni_deger[4].getElementAt(i)+""};
	    	  dtm.addRow(veriler);
		}
	
	}
	
public static void main(String[] arg) {
	
 new Calistir();

}
}
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Maclar extends JFrame implements ActionListener,ListSelectionListener,WindowListener {
       String[] lbl_txt= {"1.Takım İsmi","2.Takım İsmi","1.T Skor","2.T Skor"};
	   DefaultListModel<String>[] dlm;
       JList<String>[] jlist;
       JComboBox<String>[] jcb;
       JComboBox<Integer>[] jcb_id;
       JTextField[] jtext;
       DefaultListModel<Integer>[] Y_Mac; 
       Connection baglanti;
	   Calistir frm1;
       public Maclar(Calistir frm1) {
    	this.frm1=frm1;  
		this.setTitle("Takım Bilgileri");
		this.setSize(500, 600);
		this.setLayout(new FlowLayout());
		this.addWindowListener(this);
		
		JPanel jpn_main=new JPanel();
		jpn_main.setPreferredSize(new Dimension(500, 600));
		jpn_main.setLayout(new FlowLayout());
		
		dlm=new DefaultListModel[lbl_txt.length];
		jlist=new JList[dlm.length];
		
		jcb=new JComboBox[2];
		Y_Mac=new DefaultListModel[jcb.length];
		jtext=new JTextField[jcb.length];
		jcb_id=new JComboBox[3];
		
		for(int i=0;i<dlm.length;i++) {
			JPanel jpn=new JPanel();
			jpn.setPreferredSize(new Dimension(100, 500));
			
			JLabel jlb=new JLabel(lbl_txt[i]);
			jlb.setPreferredSize(new Dimension(100, 25));
			jpn.add(jlb);
			
		    dlm[i]=new DefaultListModel<String>();
			jlist[i]=new JList<String>(dlm[i]);
			jlist[i].addListSelectionListener(this);
			JScrollPane jscp=new JScrollPane(jlist[i]);
			jscp.setPreferredSize(new Dimension(100, 300));
			jpn.add(jscp);
             
		
			if(i<jcb.length) {
				Y_Mac[i]=new DefaultListModel<Integer>();
			    jcb[i]=new JComboBox<String>();
			    jcb[i].setPreferredSize(new Dimension(100, 25));
			    jpn.add(jcb[i]);
			}
			if(i>=jcb.length) {
			jtext[i-jcb.length]=new JTextField();
			jtext[i-jcb.length].setPreferredSize(new Dimension(100, 25));
			jpn.add(jtext[i-jcb.length]);
			}
			if(i<jcb_id.length) 
		  jcb_id[i]=new JComboBox<Integer>();//idler için
			
			if(i==0) {
				  JButton btn=new JButton("Ekle");
				  btn.setPreferredSize(new Dimension(100, 25));	  
				  btn.addActionListener(this);
				  jpn.add(btn);	
					}
		  jpn_main.add(jpn);
		}
		combodoldur();
		listdoldur();
		this.add(jpn_main);
		this.setVisible(true);
	}
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
	   JList secilen=(JList)arg0.getSource();
		for(int i=0;i<jlist.length;i++)
	               jlist[i].setSelectedIndex(secilen.getSelectedIndex());
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
	
		int secilen1=jcb[0].getSelectedIndex();//1. takım eğer bir sey secmezsek -1 degeri döner
		int secilen2=jcb[1].getSelectedIndex();//2.takım
		int[] takim_id= {jcb_id[0].getItemAt(secilen1),jcb_id[1].getItemAt(secilen2)};
		if(secilen1>-1&&secilen2>-1&&jtext[0].getText().length()>0&&jtext[1].getText().length()>0&&takim_id[0]!=takim_id[1]&&Mac_kontrol(takim_id[0],takim_id[1])) {
			try {
				int[] skor= {Integer.parseInt(jtext[0].getText()),Integer.parseInt(jtext[1].getText())};
				baglan();
				PreparedStatement sorgu=baglanti.prepareStatement("insert into Skorlar(Takim1,Takim2,Skor1,Skor2) values(?,?,?,?)");
				sorgu.setInt(1, takim_id[0]);
				sorgu.setInt(2, takim_id[1]);
				sorgu.setInt(3, skor[0]);
				sorgu.setInt(4, skor[1]);
				sorgu.executeUpdate();
				baglanti.close();
				listdoldur();       
				           for(int i=0;i<jtext.length;i++)
				        	   jtext[i].setText("");	
				frm1.listdoldur();           
			} catch (Exception e) {
			JOptionPane.showMessageDialog(this,e.getMessage()+"HAta Maclar");
			}
		}
		
		else
		JOptionPane.showMessageDialog(this,"Aynı Maç tekrar yapılamaz veya Aynı takımlar maç yapamazlar skor bilgileri doğru girilmelidir");
	}
	public boolean Mac_kontrol(int takim1,int takim2) {
	  	
		for(int i=0;i<dlm[0].getSize();i++) 
			if(Y_Mac[0].getElementAt(i)==takim1&&Y_Mac[1].getElementAt(i)==takim2)
				return false;
	return true;
	}
	

	public void baglan() {
		try {
			Class.forName("org.sqlite.JDBC");
			baglanti=DriverManager.getConnection("jdbc:sqlite::resource:Odev.db");				
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	public void combodoldur() {
		try {
			baglan();
			PreparedStatement sorgu=baglanti.prepareStatement("select ID,T_Ad from Takimlar");
			ResultSet veri=sorgu.executeQuery();
			for(int i=0;i<jcb.length;i++)
				jcb[i].removeAllItems();
			
			while(veri.next()) {
				jcb[0].addItem(veri.getString("T_Ad"));
				jcb[1].addItem(veri.getString("T_Ad"));
			    jcb_id[0].addItem(veri.getInt("ID"));
			    jcb_id[1].addItem(veri.getInt("ID"));
			}
			veri.close();
			baglanti.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
		
	}
	
  public void listdoldur() {
	  try {
		baglan();
		PreparedStatement sorgu=baglanti.prepareStatement("select s.ID as 'ID', t.T_Ad as 'T1_Ad',(select T_Ad  from Takimlar where ID=s.Takim2) as 'T2_Ad',s.Takim1 as 'Takim1',s.Takim2 'Takim2',s.Skor1 as 'Skor1',s.Skor2 as 'Skor2' from Takimlar t inner join Skorlar s on t.ID=s.Takim1 ");
	   ResultSet veri=sorgu.executeQuery();
	   for(int i=0;i<dlm.length;i++)
		       dlm[i].clear();
	   jcb_id[2].removeAllItems();
	   for(int i=0;i<Y_Mac.length;i++)
	       Y_Mac[i].clear();
	   while(veri.next()) {
		   jcb_id[2].addItem(veri.getInt("ID"));
		   dlm[0].addElement(veri.getString("T1_Ad"));
		   dlm[1].addElement(veri.getString("T2_Ad"));
		   dlm[2].addElement(veri.getInt("Skor1")+"");
		   dlm[3].addElement(veri.getInt("Skor2")+"");
		   Y_Mac[0].addElement(veri.getInt("Takim1"));
		   Y_Mac[1].addElement(veri.getInt("Takim2"));
	   }
	veri.close();
	baglanti.close();
	  } catch (Exception e) {
		  JOptionPane.showMessageDialog(this, e.getMessage());
	}
  }
@Override
public void windowActivated(WindowEvent arg0) {
	// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
	
}
@Override
public void windowClosing(WindowEvent arg0) {
	// TODO Auto-generated method stub
	frm1.setVisible(true);//kapanış olurken diğer formu görünür yap	
}
@Override
public void windowDeactivated(WindowEvent arg0) {
	// TODO Auto-generated method stub	
}
@Override
public void windowDeiconified(WindowEvent arg0) {
	// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
	// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
	// TODO Auto-generated method stub
}
}