JAVA ÖRNEK KODLARIM
Merhaba arkadaşlar,
Programlama.com 'da paylaşmış olmuduğum örnek kodları sizinle burda paylaşmak istiyorum...
Örnek kod 1
Örnek kod 2
Örnek kod 3
Bir access database ile Soru bankası uygulaması..
Yardımları için Ramin Orucov ve Erhan Bagdemir ve tüm java forum üyelerine teşekkürler..
Hepinize iyi çalışmalar...
Programlama.com 'da paylaşmış olmuduğum örnek kodları sizinle burda paylaşmak istiyorum...
Örnek kod 1
import java.awt.*;
import java.awt.event.*;
class Puzzle extends Frame implements ActionListener {
Button btn[][];
String str[]={"","1","2","3","4","5","6","7","8"};
int sira[]=new int[10];
int bos_satir;
int bos_sutun;
boolean yukari=false;
boolean saga=false;
boolean sola=false;
boolean asagi=false;
boolean calisti=false;
public Puzzle(String title){
super(title);
setLayout(new GridLayout(3,3));
int sayac=0;
int sayac2=0;
boolean devam=true;
boolean calıs=false;
int say;
do{
say=(int)Math.round(Math.random()*8);
sira[sayac]=say;
for (int i=0;i
if (sira[i]==sira[sayac]) sayac--;
}
sayac++;
if (sayac==9) devam=false;
}while(devam);
btn=new Button[3][3];
int k=0;
for(int m=0;m<3;m++)
for(int l=0;l<3;l++){
int a=sira[k];
if (a==0){
bos_satir=m;
bos_sutun=l;
System.out.println (m);
System.out.println (l);
btn[m][l]=new Button(str[a]);
btn[m][l].setEnabled(false);
btn[m][l].setVisible(false);
btn[m][l].addActionListener(this);
add(btn[m][l]);
}else{
btn[m][l]=new Button(str[a]);
add(btn[m][l]);
btn[m][l].addActionListener(this);
}
k++;
}
System.out.println (bos_satir);
System.out.println (bos_sutun);
hesapla(bos_satir,bos_sutun);
}
public void hesapla(int a,int b){
if (a-1>=0) yukari=true;
if (a+1<=2) asagi=true;
if (b+1<=2) saga=true;
if (b-1>=0) sola=true;
}
public void sifirla(){
yukari=false;
asagi=false;
saga=false;
sola=false;
}
public void actionPerformed(ActionEvent e){
if (yukari){
if (e.getSource()==btn[bos_satir-1][bos_sutun]){
String l=btn[bos_satir-1][bos_sutun].getLabel();
btn[bos_satir][bos_sutun].setLabel(l);
btn[bos_satir][bos_sutun].setVisible(true);
btn[bos_satir][bos_sutun].setEnabled(true);
btn[bos_satir-1][bos_sutun].setVisible(false);
bos_satir--;
sifirla();
hesapla(bos_satir,bos_sutun);calisti=true;
}
}
if (asagi){
if (!calisti){
if (e.getSource()==btn[bos_satir+1][bos_sutun]){
String l=btn[bos_satir+1][bos_sutun].getLabel();
btn[bos_satir][bos_sutun].setLabel(l);
btn[bos_satir][bos_sutun].setVisible(true);
btn[bos_satir][bos_sutun].setEnabled(true);
btn[bos_satir+1][bos_sutun].setVisible(false);
bos_satir++;
sifirla();
hesapla(bos_satir,bos_sutun);calisti=true;
}
}
}
if (sola){
if (!calisti){
if (e.getSource()==btn[bos_satir][bos_sutun-1]){
String l=btn[bos_satir][bos_sutun-1].getLabel();
btn[bos_satir][bos_sutun].setLabel(l);
btn[bos_satir][bos_sutun].setVisible(true);
btn[bos_satir][bos_sutun].setEnabled(true);
btn[bos_satir][bos_sutun-1].setVisible(false);
bos_sutun--;
sifirla();
hesapla(bos_satir,bos_sutun);calisti=true;
}
}
}
if (saga){
if (!calisti){
if (e.getSource()==btn[bos_satir][bos_sutun+1]){
String l=btn[bos_satir][bos_sutun+1].getLabel();
btn[bos_satir][bos_sutun].setLabel(l);
btn[bos_satir][bos_sutun].setVisible(true);
btn[bos_satir][bos_sutun].setEnabled(true);
btn[bos_satir][bos_sutun+1].setVisible(false);
bos_sutun++;
sifirla();
hesapla(bos_satir,bos_sutun);calisti=true;
}
}
}
calisti=false;
btn[bos_satir][bos_sutun].setEnabled(false);
kontrol();
}
public void kontrol(){
boolean k=false;
int a=1;
int sayac=0;
for(int m=0;m<3;m++)
for(int l=0;l<3;l++){
if (a==9) break;
if (btn[m][l].getLabel().equals(""+a)) sayac++;
a++;
}
if (sayac==8) System.out.println ("You win");
}
public static void main(String[] args){
Puzzle t=new Puzzle("Grid Layout");
t.setSize(300,200);
t.show();
}
}
Örnek kod 2
import java.awt.*;
import java.awt.event.*;
public class DXBall extends Frame implements KeyListener {
Button b;
Panel p;
static int x,y,w,h;
static DXBall d;
DXBall(){
this.setTitle("JAVABALL");
this.setSize(400,400);
x=200;y=370; w=100; h=15;
this.setLayout(null);
b=new Button("\t\t\t\t\t\t\t");
b.setEnabled(false);
b.setBounds(x,y,w,h);
this.add(b);
new Stone(this);
this.addKeyListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
this.setVisible(true);
new Top(d,this.getGraphics());
}
public void keyTyped(KeyEvent ke){
}
public void keyPressed(KeyEvent ke){
if (KeyEvent.VK_LEFT==ke.getKeyCode()){
x=x-4;
b.setBounds(x,y,w,h);
}
if (KeyEvent.VK_RIGHT==ke.getKeyCode()){
x=x+4;
b.setBounds(x,y,w,h);
}
}
public void keyReleased(KeyEvent ke){
}
public static void main(String[] args){
d=new DXBall();
}
}
class Top extends Thread {
static DXBall b;
static Graphics g;
static int x,y,w,h,dx,dy;
Stone s;
Thread t;
int oldy;
boolean durdur=false;
Top(DXBall a,Graphics g){
this.b=a;
this.g=g;
x=250;y=370;w=10;h=10;
dx=6;
dy=3;
this.start();
draw();
}
public void draw(){
g.setColor(Color.black);
g.fillOval(x,y,w,h);
}
public void run(){
try {
while(true){
move();
Thread.sleep(1);
vur(x,y);
this.sleep(2);
}
}
catch (InterruptedException ex) {
System.out.println ("Problem var!!");
}
}
public void move(){
try {
g.fillOval(x,y,w,h);
Thread.sleep(15);
g.clearRect(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
if (x<10){
x=10; dx=-dx;
}
if (x+dx>400){
x=390; dx=-dx;
}
if (y<10){
y=10; dy=-dy;}
if ((y+dy)>(b.y-15)){
if (((x-b.x)<90)>0)) {
y=b.y-20; dy=-dy;
}else this.destroy();
}
x=x+dx;
y=y+dy;
}
catch (Exception ex) {}
}
public void vur(int x,int y){
for (int i = 0; i<5;>
for (int j = 0; j<5;>
if (s.stone[i][j].getBounds().contains(x,y) && s.stone[i][j].isVisible()){
s.stone[i][j].setVisible(false);
dx=-dx;
dy=-dy;
}
}
}
}
}
class Stone{
static Button[][] stone;
static int x,y,w,h;
static DXBall a;
static int siray[]=new int[5];
Stone(DXBall d){
this.a=d;
stone=new Button[5][5];
x=0;
y=40;
w=80;
h=20;
for(int j=0;j<5;j++)
for(int i=0;i<5;i++){
stone[j][i]=new Button("\t\t\t\t");
stone[j][i].setBackground(Color.blue);
stone[j][i].setEnabled(false);
stone[j][i].setBounds(x+80*i,y+20*j,w,h);
siray[i]=y+20*j;
d.add(stone[j][i]);
}
y=stone[4][1].getY();
}
}
Örnek kod 3
Bir access database ile Soru bankası uygulaması..
Yardımları için Ramin Orucov ve Erhan Bagdemir ve tüm java forum üyelerine teşekkürler..
import java.util.Timer;
import java.util.TimerTask;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.*;
public class MenuGUI extends JFrame implements WindowListener,ActionListener,ItemListener
{
private int Id=0;
private String soru;
private String chcA;
private String chcB;
private String chcC;
private String chcD;
public ButtonGroup bg;
public JRadioButton choiceA;
private JRadioButton choiceB;
private JRadioButton choiceC;
private JRadioButton choiceD;
private Graphics g;
private JButton sbmt;
private JButton strt;
private JPanel panelCard;
private JPanel panelStart;
private JPanel panelChoice;
private JPanel panelCommand;
private JPanel panelChoice1;
private JPanel panelQues;
private JPanel panelStrt;
private JFrame frame;
private JLabel timerTable;
static java.sql.Connection baglanti=null;
static java.sql.ResultSet SonucVeriler=null;
static Statement sorgulama=null;
public JLabel etiket;
private String nAnswer;
private String oAnswer;
private int question;
private String answerbyuser;
private boolean dVy;
boolean done=false;
boolean done1=false;
Timetut timer=new Timetut();
public MenuGUI(String baslik) {
frame=new JFrame(baslik);
choiceA = new JRadioButton("choice A");
choiceB = new JRadioButton("choice B");
choiceC = new JRadioButton("choice C");
choiceD = new JRadioButton("choice D");
timerTable=new JLabel("Time:");
bg=new ButtonGroup();
bg.add(choiceA);
bg.add(choiceB);
bg.add(choiceC);
bg.add(choiceD);
sbmt=new JButton("Submit");
strt=new JButton("Start");
sbmt.setVisible(false);
panelCommand= new JPanel();
panelQues=new JPanel();
panelCommand.setBorder(new EtchedBorder(EtchedBorder.RAISED));
panelChoice=new JPanel(new BorderLayout());
panelStart=new JPanel(new BorderLayout());
panelStart.setBorder(new EtchedBorder(EtchedBorder.RAISED));
panelStrt=new JPanel();
panelStrt.add(strt);
panelChoice.setBorder(new EtchedBorder(EtchedBorder.RAISED));
panelChoice1=new JPanel();
panelChoice1.add(choiceA);
panelChoice1.add(choiceB);
panelChoice1.add(choiceC);
panelChoice1.add(choiceD);
panelCard= new JPanel(new BorderLayout());
panelCard.add("South",panelChoice);
panelCard.add("Center",panelQues);
panelCard.setBorder(new EtchedBorder(EtchedBorder.RAISED));
etiket= new JLabel("For Starting Please Push Start");
JOptionPane.showMessageDialog(null,"Java Quize Hosgeldiniz","Quiz Programi",JOptionPane.INFORMATION_MESSAGE);
//PROGRAM ICINDE KULLANILACAK COMPONENTLARIN TANITILMASI
}
public void pencereyiGoster()
{ Container cnt =frame.getContentPane();
cnt.setLayout(new BorderLayout(10,10));
cnt.add("Center",panelCard);
cnt.add("South",panelCommand);
panelStart.add("East",timerTable);
panelStart.add("Center",panelStrt);
panelChoice.add("North",panelStart);
panelChoice.add("Center",panelChoice1);
panelCommand.add(sbmt);
panelQues.add(etiket);
sbmt.addActionListener(this);
strt.addActionListener(this);
choiceA.addItemListener(this);
choiceB.addItemListener(this);
choiceC.addItemListener(this);
choiceD.addItemListener(this);
frame.setSize(800,600);
frame.setLocation(200,100);
frame.addWindowListener(this);
frame.setVisible(true);
}
//DATABASE'E BAGLANTI YAPIYORUZ..
private static void baglan(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
baglanti=DriverManager.getConnection("jdbc:Odbc:soru_java");
sorgulama=baglanti.createStatement();
}
catch (Exception ex) {
}
}
public static void main(String[] args) {
MenuGUI penc = new MenuGUI("Soru Bankasi");
// Create application frame.
penc.pencereyiGoster();
// penc.baglan();
}
public void actionPerformed ( ActionEvent e)
{
if (e.getActionCommand().equals("Exit"))
{
done=true;
System.out.print(done);
int confirm = JOptionPane.showOptionDialog(null,
"Really Exit?", "Exit Confirmation",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, null, null);
if (confirm==0) { System.exit(0); }
}
if (e.getSource()==sbmt)
{
try{
timer();
soruGoster();
dVy=controlEt();
upDate(dVy);
}catch(Exception exc){}
}
if (e.getSource()==strt){
timer.start();
sbmt.setVisible(true);
strt.setVisible(false);
soruGoster(); }
}
/**
* Shutdown procedure when run as an application.
*/
public void windowClosing(WindowEvent e) {
done=true;
int confirm = JOptionPane.showOptionDialog(frame,
"Really Exit?", "Exit Confirmation",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, null, null);
if (confirm == 0) {
System.exit(0);
}
}
//VERİLER DATABASE'DEN ÇEKİLİYOR..
private void soruGoster(){
try{
done1=false;
oAnswer=nAnswer;
Id++;
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Connection baglanti=DriverManager.getConnection("jdbc:Odbc:soru_java");
// Statement sorgulama=baglanti.createStatement();
baglan();
SonucVeriler=sorgulama.executeQuery("Select * FROM Table1 ORDER BY Id");
for(int i=0;i
Id=(SonucVeriler.getInt("Id"));
soru=(SonucVeriler.getString("Soru"));
chcA=(SonucVeriler.getString("ChoiceA"));
chcB=(SonucVeriler.getString("ChoiceB"));
chcC=(SonucVeriler.getString("ChoiceC"));
chcD=(SonucVeriler.getString("ChoiceD"));
nAnswer=(SonucVeriler.getString("Answer"));
}catch (Exception e){}
try{ if (sorgulama!=null)
sorgulama.close();
if(baglanti!=null)
baglanti.close();
}catch(SQLException sqle){}
Font font=new Font(null,Font.BOLD,18);
etiket.setFont(font);
etiket.setText("Question"+Integer.toString(Id)+"."+soru);
choiceA.setText(chcA);
choiceB.setText(chcB);
choiceC.setText(chcC);
choiceD.setText(chcD);
if (soru.equals("")){
JOptionPane.showMessageDialog(null,"Testiniz bitmiştir.");
frame.dispose();
evaluate();
}
soru="";
chcA="";
chcB="";
chcC="";
chcD="";
}
public void timer(){
timer.stop();
timer.start();
}
private boolean controlEt (){
if (oAnswer.equals(answerbyuser)){
return true;
}
else{
return false;
}
}
private void upDate(boolean onay){
try
{
question=Id-1;
baglan();
String upDateString="Update Table1 SET TrueorFalse ="+onay+" WHERE Id ="+question;
sorgulama.executeUpdate(upDateString);
}catch(Exception ex){}
}
public void evaluate(){
int correct=0;
try {
baglan();
SonucVeriler=sorgulama.executeQuery("Select Id,TrueorFalse FROM Table1 ORDER BY Id");
System.out.println (SonucVeriler.getRow());
while (SonucVeriler.next()){
System.out.println (SonucVeriler.getRow());
Id=(SonucVeriler.getInt("Id"));
dVy=(SonucVeriler.getBoolean("TrueorFalse"));
System.out.println (dVy);
if (dVy==true) correct++;
}
JOptionPane.showMessageDialog(null,"Dogru sayınız :"+correct);
}
catch (Exception ex) {
System.out.println (ex.getMessage());
}
}
class Timetut implements Runnable {
Thread thrd;
int seconds=20;
public void run(){
while (seconds!=0){
try{
if (done==true){try{ wait();}catch(Exception e){} }
if (done1==true){thrd.stop();start();}
Thread.sleep(1000); seconds=seconds-1; timerTable.setText("Time :"+seconds);}
catch(InterruptedException e){}
if (seconds==0){JOptionPane.showMessageDialog(null,"Süreniz Doldu.... :(","Quiz Programi",JOptionPane.INFORMATION_MESSAGE);
seconds=20; soruGoster();}
}
}
public void start() {
if ( thrd== null ) {
thrd = new Thread(this);
seconds=20;
thrd.start();
}
}
public void stop() {
if ( thrd != null ) {
thrd.stop();
thrd = null;
} }
}
public void itemStateChanged (ItemEvent ie){
JRadioButton rb=(JRadioButton)ie.getItemSelectable();
answerbyuser=rb.getText();
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowOpened (WindowEvent e) {}
}
Hepinize iyi çalışmalar...
Merhaba, kodun originalini buradan buldum :)
YanıtlaSilhttp://www.programlama.com/sys/c2html/view.php?DocID=5258
Evet.. Size tekrar teşekkür ederim.. O dönemde javaya yeni başlamıştım :)
YanıtlaSil