Java-2(2015) 4. Hafta Swing Uygulamaları
import javax.swing.*; import java.awt.*; import java.awt.event.*; class kodla implements ActionListener{ JTextField jtf1,jtf2; kodla() { JFrame jfrm = new JFrame("Şifrele-Çöz"); jfrm.setLayout(new FlowLayout()); jfrm.setSize(200,110); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jtf1= new JTextField(10); jtf2= new JTextField(10); jtf1.setActionCommand("birinci"); // jtf2.setActionCommand("ikinci"); jtf1.addActionListener(this); jtf2.addActionListener(this); jfrm.add(jtf1); jfrm.add(jtf2); jfrm.setVisible(true); } public void actionPerformed(ActionEvent ae) { JTextField kaynak,hedef; int sayi=1; if(ae.getActionCommand().equals("birinci")) { kaynak=jtf1; hedef=jtf2; sayi=1; //jtf2.setText(jtf1.getText()); } else { kaynak=jtf2; hedef=jtf1; sayi=-1; //jtf1.setText(jtf2.getText()); } StringBuilder str = new StringBuilder(kaynak.getText()); for(int i=0;i<str.length(); i++) { str.setCharAt(i,(char)(str.charAt(i)+sayi*i)); } hedef.setText(str.toString()); } public static void main(String[] asds) { new kodla(); } }
import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; class yerlesim{ yerlesim() { JFrame jfrm = new JFrame("Yerlesim Ornegi"); //jfrm.setLayout(new FlowLayout()); jfrm.setLayout(new GridLayout(3,3,5,5)); jfrm.setSize(500,200); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel[] jlab= new JLabel[9]; Border border = BorderFactory.createEtchedBorder(); for(int i=0;i<9;i++) { jlab[i]= new JLabel("Label_"+(i+1)); jlab[i].setBorder(border); jfrm.add(jlab[i]); int dusey,yatay; if(i%2==0) { dusey= SwingConstants.BOTTOM; yatay= SwingConstants.RIGHT; } else { dusey= SwingConstants.TOP; yatay= SwingConstants.LEFT; } jlab[i].setVerticalAlignment(dusey); jlab[i].setHorizontalAlignment(yatay); } jfrm.setVisible(true); } public static void main(String[] asdasd) { new yerlesim(); } }
import javax.swing.*; import java.awt.*; import java.awt.event.*; class secimkutusu implements ActionListener{ JLabel sonuc; JCheckBox[] jcb; secimkutusu() { JFrame jfrm = new JFrame("Seçim Kutusu"); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setSize(220,300); jfrm.setLayout(new GridLayout(8,1)); JLabel jlab1= new JLabel("Seçim Yapınız:"); jfrm.add(jlab1); jcb = new JCheckBox[6]; String[] bilgiler={"C++","C#","Java","Grafik","BMG","YMG"}; for(int i=0;i<bilgiler.length;i++) { jcb[i]= new JCheckBox(bilgiler[i]); jfrm.add(jcb[i]); jcb[i].addActionListener(this); } sonuc = new JLabel("Seçim Yapılmadı!"); jfrm.add(sonuc); jfrm.setVisible(true); } public void actionPerformed(ActionEvent ae) { String str = ""; for(int i=0;i<jcb.length;i++) { if(jcb[i].isSelected()) { if(str.length()>0){str+=",";} str+=jcb[i].getText(); } } if(str.length()==0) str="Seçim Yapılmadı!"; sonuc.setText(str); } public static void main(String[] asdasds) { new secimkutusu(); } }
import javax.swing.*; import java.awt.*; import java.awt.event.*; class radiobutton implements ActionListener{ JLabel sonuc; radiobutton() { JFrame jfrm = new JFrame("Seçim Kutusu"); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setSize(240,300); jfrm.setLayout(new GridLayout(6,1)); JLabel jlab1= new JLabel("Türkiye'nin başkenti neresidir?"); jfrm.add(jlab1); String[] bilgiler={"Hatay","İstanbul","Ankara","Adana"}; ButtonGroup bg = new ButtonGroup(); for(int i=0;i<bilgiler.length;i++) { JRadioButton jrb= new JRadioButton(bilgiler[i]); jfrm.add(jrb); bg.add(jrb); jrb.addActionListener(this); } sonuc = new JLabel("Soruyu Cevaplayınız!"); jfrm.add(sonuc); jfrm.setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("Ankara")) sonuc.setText("Tebrikler, Doğru Bildiniz!!"); else sonuc.setText("Hatalı oldu sanki?"); } public static void main(String[] asdasds) { new radiobutton(); } }