Java-2 – YO2015 – Hafta – 3

java2_yo2015_hafta3_resim1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class soru {
	String soru;
	String[] cevaplar;
	int dogrucevap;
}

public class ornek1 implements ActionListener {

	JRadioButton[] jrb;
	JLabel jlab;
	soru[] sor = new soru[2];
	int sorusirasi = 0;

	public ornek1() {

		sor[0] = new soru();
		sor[0].soru = "Türkiye'nin başkenti neresidir?";
		sor[0].cevaplar = new String[] { "Ankara", "İzmir", "Hatay",
				"İstanbul", "Adana" };
		sor[0].dogrucevap = 0;

		sor[1] = new soru();
		sor[1].soru = "CPU nedir?";
		sor[1].cevaplar = new String[] { "Rastgele erişilebilir bellek", "Merkezi işlem birimi", "Çözünürlük",
				"Klavye", "Projeksiyon" };
		sor[1].dogrucevap = 1;

		JFrame jfrm = new JFrame("hafta-3,ornek-1");
		jfrm.setSize(250, 250);
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jfrm.setLayout(new GridLayout(7, 1));

		jlab = new JLabel();
		JPanel jpn = new JPanel();
		jpn.setLayout(new FlowLayout(FlowLayout.LEFT));
		jpn.add(jlab);
		jfrm.add(jpn);
		jrb = new JRadioButton[5];

		ButtonGroup bg = new ButtonGroup();
		for (int i = 0; i < jrb.length; i++) {
			jpn = new JPanel();
			jpn.setLayout(new FlowLayout(FlowLayout.LEFT));
			jrb[i] = new JRadioButton();
			jpn.add(jrb[i]);
			bg.add(jrb[i]);
			jfrm.add(jpn);
		}
		jrb[0].setSelected(true);

		jpn = new JPanel();
		JButton jbtn = new JButton("Kontrol Et");
		jbtn.addActionListener(this);
		jpn.add(jbtn);
		jfrm.add(jpn);
		soruyukle();
		jfrm.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		int secilen = -1;
		for (int i = 0; i < jrb.length; i++) {
			if (jrb[i].isSelected()) {
				secilen = i;
				break;
			}
		}
		if (secilen > -1) {
			String sonuc = "Cevap hatalı!";

			boolean durum = false;
			if (sor[sorusirasi].dogrucevap == secilen) {
				sonuc = "Cevap dogru!";
				durum = true;
			}
			JOptionPane.showMessageDialog(null, sonuc);

			if (durum) {
				sorusirasi++;
				soruyukle();
			}
		}

	}

	private void soruyukle() {

		if (sor.length > sorusirasi) {

			jlab.setText(sor[sorusirasi].soru);

			for (int i = 0; i < jrb.length; i++) {
				jrb[i].setText(sor[sorusirasi].cevaplar[i]);
			}

			jrb[0].setSelected(true);

		} else
			sorusirasi--;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		new ornek1();

	}
}

java2_yo2015_hafta3_resim2

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ornek2 implements ActionListener {

	JCheckBox[] jcb;
	JLabel jlab;

	public ornek2() {

		String[] secim = new String[] { "Seçim1", "Seçim2", "Seçim3", "Seçim4" };
		JFrame jfrm = new JFrame("hafta-3, ornek2");
		jfrm.setSize(200, 200);
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jfrm.setLayout(new GridLayout((secim.length + 1), 1));

		jcb = new JCheckBox[secim.length];

		for (int i = 0; i < jcb.length; i++) {
			jcb[i] = new JCheckBox(secim[i]);
			jfrm.add(jcb[i]);
			jcb[i].addActionListener(this);
		}

		jlab = new JLabel("Seçim yapınız!");
		jfrm.add(jlab);
		jfrm.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		String sonuc = "";

		for (int i = 0; i < jcb.length; i++) {
			if (jcb[i].isSelected()){
				if(sonuc.length()>0) sonuc+=";";	
				sonuc += jcb[i].getText();
		
			}
		}
		if (sonuc.length() > 0)
			jlab.setText(sonuc);
		else
			jlab.setText("Seçim Yok!");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ornek2();
	}

}

java2_yo2015_hafta3_resim3

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ornek3 implements ActionListener, ListSelectionListener {

	JTextField jtf;
	JComboBox<String> jcb;

	DefaultListModel<String> lurun, lmiktar;
	JList<String> jls1, jls2;

	public ornek3() {

		String[] urunler = new String[] { "Elma", "Karpuz", "Kiraz", "Erik" };

		JFrame jfrm = new JFrame("hafta-3, ornek-3");
		jfrm.setSize(350, 200);
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jfrm.setLayout(new GridLayout(1, 2));
		JPanel jpn1 = new JPanel();
		JPanel jpn2 = new JPanel();
		jfrm.add(jpn1);
		jfrm.add(jpn2);
		lurun = new DefaultListModel<String>();
		lmiktar = new DefaultListModel<String>();
		jls1 = new JList<String>(lurun);
		jls2 = new JList<String>(lmiktar);
		JScrollPane jscp1 = new JScrollPane(jls1);
		JScrollPane jscp2 = new JScrollPane(jls2);
		jscp1.setPreferredSize(new Dimension(160, 130));
		jscp2.setPreferredSize(new Dimension(160, 130));
		jpn1.add(jscp1);
		jpn2.add(jscp2);
		jtf = new JTextField();
		jcb = new JComboBox<String>(urunler);
		jpn1.add(jcb);
		jcb.setPreferredSize(new Dimension(160, 20));
		jpn2.add(jtf);
		jtf.setPreferredSize(new Dimension(160, 20));

		JButton jbtn1 = new JButton("Ekle");
		JButton jbtn2 = new JButton("Kapat");
		jbtn1.setPreferredSize(new Dimension(160, 30));
		jbtn2.setPreferredSize(new Dimension(160, 30));
		jpn1.add(jbtn1);
		jpn2.add(jbtn2);

		jls1.addListSelectionListener(this);
		jls2.addListSelectionListener(this);

		jbtn1.addActionListener(this);
		jbtn2.addActionListener(this);
		jbtn1.setActionCommand("ekle");
		jfrm.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("ekle")) {
			if (jtf.getText().length() > 0 && jcb.getSelectedIndex() > -1) {
				try {

					Integer.parseInt(jtf.getText());
					lurun.addElement(jcb.getSelectedItem().toString());
					lmiktar.addElement(jtf.getText());

					jtf.setText("");
					jcb.setSelectedIndex(0);

				} catch (Exception ex) {

				}

			}

		} else
			System.exit(0); // programi kapatmak icin
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		
		JList<String> jl=(JList<String>)e.getSource();
		int secilen=jl.getSelectedIndex();
		
		if(secilen>-1)
		{
			jls1.setSelectedIndex(secilen);
			jls2.setSelectedIndex(secilen);
		}
		
		
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ornek3();

	}

}

 

Share