Java-2(2015) 3. 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 |
import javax.swing.*; class ilkpencere{ ilkpencere(){ JFrame jfrm = new JFrame("İlk penceremiz"); jfrm.setSize(275,100); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //JLabel jlab1 = new JLabel("bu bir etiket"); JLabel jlab2 = new JLabel("<html><h1><b>asdasd</b></h1></html>"); //jfrm.add(jlab1); // jfrm.getContentPane.add() // java 1.5 version once jfrm.add(jlab2); jfrm.setVisible(true); } public static void main(String[] asds) { new ilkpencere(); } } |
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 |
import javax.swing.*; import java.awt.*; class ornek2{ ornek2(){ JFrame jfrm = new JFrame("İlk penceremiz"); jfrm.setSize(275,100); jfrm.setLayout(new FlowLayout()); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel jlab1 = new JLabel("bu bir etiket"); JLabel jlab2 = new JLabel("ikinci etiket"); jfrm.add(jlab1); jfrm.add(jlab2); jfrm.setVisible(true); } public static void main(String[] asds) { new ornek2(); } } |
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 |
import javax.swing.*; import java.awt.*; import java.awt.event.*; class ornek3 implements ActionListener{ JLabel jlab1; ornek3(){ JFrame jfrm = new JFrame("İlk penceremiz"); jfrm.setSize(250,100); jfrm.setLayout(new FlowLayout()); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jlab1 = new JLabel("bu bir etiket degisecek"); JButton jbtn1 = new JButton("Değiştir"); jbtn1.addActionListener(this); jfrm.add(jlab1); jfrm.add(jbtn1); jfrm.setVisible(true); } public void actionPerformed(ActionEvent ae) { jlab1.setText("degisti"); } public static void main(String[] asds) { new ornek3(); } } |
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 |
import javax.swing.*; import java.awt.*; import java.awt.event.*; class ornek4 implements ActionListener{ JLabel jlab1; JTextField jtf1,jtf2; ornek4(){ JFrame jfrm = new JFrame("İlk penceremiz"); jfrm.setSize(150,140); jfrm.setLayout(new FlowLayout()); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jtf1= new JTextField(10); jtf2= new JTextField(10); jlab1 = new JLabel(""); JButton jbtn1 = new JButton("Topla"); jbtn1.addActionListener(this); jfrm.add(jtf1); jfrm.add(jtf2); jfrm.add(jlab1); jfrm.add(jbtn1); jfrm.setVisible(true); } public void actionPerformed(ActionEvent ae) { int sayi1,sayi2; sayi1=Integer.parseInt(jtf1.getText()); sayi2=Integer.parseInt(jtf2.getText()); jlab1.setText("Sonuc:"+(sayi1+sayi2)); } public static void main(String[] asds) { new ornek4(); } } |
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 |
import javax.swing.*; import java.awt.*; import java.awt.event.*; class ornek5 implements ActionListener{ JLabel jlab1; JTextField jtf1,jtf2; ornek5(){ JFrame jfrm = new JFrame("İlk penceremiz"); jfrm.setSize(200,150); jfrm.setLayout(new FlowLayout()); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jtf1= new JTextField(10); jtf2= new JTextField(10); jlab1 = new JLabel(""); JButton jbtn1 = new JButton("Topla"); JButton jbtn2 = new JButton("Çıkar"); jbtn1.addActionListener(this); jbtn2.addActionListener(this); jfrm.add(jtf1); jfrm.add(jtf2); jfrm.add(jlab1); jfrm.add(jbtn1); jfrm.add(jbtn2); jfrm.setVisible(true); } public void actionPerformed(ActionEvent ae) { int sayi1,sayi2; sayi1=Integer.parseInt(jtf1.getText()); sayi2=Integer.parseInt(jtf2.getText()); if(ae.getActionCommand().equals("Topla")){ jlab1.setText("Sonuc:"+(sayi1+sayi2)); } else { jlab1.setText("Sonuc:"+(sayi1-sayi2)); } } public static void main(String[] asds) { new ornek5(); } } |
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 |
import javax.swing.*; import java.awt.*; import java.awt.event.*; class hesapmak implements ActionListener { int sayi1,sayi2,islem=0; JTextField jtf; hesapmak() { JFrame jfrm = new JFrame("Hesap Makinası"); jfrm.setLayout(new FlowLayout()); jfrm.setSize(175,200); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jtf=new JTextField(10); jfrm.add(jtf); jtf.setEnabled(false); // text alani pasiflesir for(int i=1;i<10;i++) { JButton jbtn = new JButton(i+""); jbtn.addActionListener(this); //jbtn.setActionCommand("sayi"); jfrm.add(jbtn); } JButton jbtnarti = new JButton("+"); JButton jbtneksi = new JButton("-"); JButton jbtnesittir = new JButton("="); jbtnarti.addActionListener(this); jbtneksi.addActionListener(this); jbtnesittir.addActionListener(this); jfrm.add(jbtnarti); jfrm.add(jbtneksi); jfrm.add(jbtnesittir); jfrm.setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("+")) { sayi1=Integer.parseInt(jtf.getText()); jtf.setText(""); islem=1; }else if(ae.getActionCommand().equals("-")) { sayi1=Integer.parseInt(jtf.getText()); jtf.setText(""); islem=2; }else if(ae.getActionCommand().equals("=")) { if(islem>0) { sayi2=Integer.parseInt(jtf.getText()); if(islem==1) { sayi1+=sayi2; } else { sayi1-=sayi2; } jtf.setText(sayi1+""); sayi1=0; sayi2=0; islem=-1; } }else { // sayi if(islem<0) { jtf.setText(""); islem=0; } jtf.setText(jtf.getText()+ae.getActionCommand()); } } public static void main(String[] asdasd) { new hesapmak(); } } |
Hocam merhabalar. bu örnekte 1-9 arasındaki sayılar için oluşturdugunuz butonlara listener eklimisiz. Fakat actionperformed de çağırmamışız.o butonların hangi sayilari tetikleyecegini nasıl yazdıracaz? bi döngüyle textfield ı her rakam için setlesek , sonra en son ki degeri get ile alıp işleme alsak çok mu uzun olur ? hastasnız rahatsız ediyorum böyle . şimdiden ilginiz için tesekkür ederim hocam mailinizi bekliyorum. .
butonlara actioncommand atamadığınız sürece getactioncommand bilgisi buton ismi ile ayni olur. Soruda if ile test etmedim çünkü
jtf.setText(jtf.getText()+ae.getActionCommand());
kod satırı ile sayıyı zaten kullanıyoruz. Bu kod bu örnek için yeterli olmuştu.