Java-2(2015) 8. Hafta Swing Uygulamaları
1 2 3 4 5 6 7 8 9 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class sinav implements ActionListener { JLabel jlab_soru; JRadioButton[] jrbs=new JRadioButton[5]; DefaultListModel verilencevap,sorular; int soruindex=0; sinav() { JFrame jfrm = new JFrame("Sınav Soruları"); jfrm.setSize(400,210); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setLayout(new GridLayout(3,1)); jlab_soru= new JLabel("Soru 1: xyz"); JPanel jp1= new JPanel(); jp1.add(jlab_soru); jp1.setLayout(new GridLayout(1,1)); // sola dayali JPanel jp2= new JPanel(); jp2.setLayout(new GridLayout(3,2)); ButtonGroup bg = new ButtonGroup(); for(int i=0;i<5;i++) { jrbs[i] = new JRadioButton("A"+i); jp2.add(jrbs[i]); bg.add(jrbs[i]); } JPanel jp3= new JPanel(); jp3.setLayout(new GridLayout(1,2)); JButton jbtn_geri= new JButton("<< Geri"); JButton jbtn_ileri= new JButton("İleri >>"); jbtn_geri.addActionListener(this); jbtn_geri.setActionCommand("geri"); jbtn_ileri.addActionListener(this); JPanel jp3_1 = new JPanel(); JPanel jp3_2 = new JPanel(); jp3_1.add(jbtn_geri); jp3_2.add(jbtn_ileri); jp3.add(jp3_1); jp3.add(jp3_2); jfrm.add(jp1); jfrm.add(jp2); jfrm.add(jp3); soruyukle(); sorugetir(0); jfrm.setVisible(true); } public int secenek() { for(int i=0;i<jrbs.length;i++) { if(jrbs[i].isSelected()) return (i+1); // gelen secenek 1-5 arasinda } return 0; } public void soruyukle() { Connection conn = baglan(); try{ verilencevap = new DefaultListModel(); sorular = new DefaultListModel(); String sql="select soru,A,B,C,D,E,cevap from sorular;"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while(rs.next()) { String veri=rs.getString("soru")+";"+rs.getString("A")+";"+rs.getString("B")+";"+rs.getString("C")+";"+rs.getString("D")+";"+rs.getString("E")+";"+rs.getString("cevap"); //System.out.println(veri); verilencevap.addElement("0"); sorular.addElement(veri); } } catch(Exception ex) { } try{ conn.close(); } catch(Exception ex) { } } public void sorugetir(int index) { int sorusayisi=sorular.getSize(); if(index>-1 && index<=sorusayisi) { int secim=secenek(); // secilen radiobutton bilgisi alindi verilencevap.setElementAt(secim+"",soruindex); } if(index>-1 && index<sorusayisi) { soruindex=index; String soru =sorular.getElementAt(index).toString(); String[] parca=soru.split(";"); jlab_soru.setText("Soru "+(index+1)+": "+parca[0]); String[] harf ={"A)","B)","C)","D)","E)"}; for(int i=0;i<5;i++) { jrbs[i].setText(harf[i]+" "+parca[i+1]); // secenekler yukleniyor if(verilencevap.getElementAt(index).equals((i+1)+"")) jrbs[i].setSelected(true); } if(verilencevap.getElementAt(index).equals("0")) jrbs[0].setSelected(true); } if(index>=sorusayisi) { String sonuclar=""; for(int i=0;i<sorusayisi;i++) { String vcevap=verilencevap.getElementAt(i).toString(); String soru =sorular.getElementAt(i).toString(); String[] parca=soru.split(";"); String gcevap=parca[6]; // cevap sonuclar+=(i+1)+") "; if(gcevap.equals(vcevap)) sonuclar+=" Doğru\n"; else sonuclar+=" Yanlış\n"; } JOptionPane.showMessageDialog(null,sonuclar,"Sonuçlar",1); } } public Connection baglan() { Connection conn=null; try{ Class.forName("org.sqlite.JDBC"); conn=DriverManager.getConnection("jdbc:sqlite:hafta8.db"); } catch(Exception ex) { JOptionPane.showMessageDialog(null,"Bağlantı yapılamadı!","Hata",0); } return conn; } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("geri")) { sorugetir(soruindex-1); } else { sorugetir(soruindex+1); } } public static void main(String[] aaa) { new sinav(); } } |
Hocam kilit noktalara biraz daha açıklama satırı yazarsanız güzel olur 🙂
anlamadığınız yerleri sorarsanız cevaplarım
Veritabanını da koyma imkanınız var mı hocam.elle oluşturduğum vt de bazı hatalar alıyorum
Yazının sonunda link var oradan indirebilirsiniz