Java-2(2015) 14. Hafta Swing Uygulamaları

java2_2015_hafta14_resim1

 

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

public class soru1 implements ActionListener
{

JTextField jtf;
soru1()
{

JFrame jfrm = new JFrame("Soru1");
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setSize(250,150);
jfrm.setResizable(false);
jfrm.setLayout(new GridLayout(4,1));

JPanel jpn1 = new JPanel();
JPanel jpn2 = new JPanel();
JPanel jpn3 = new JPanel();
JPanel jpn4 = new JPanel();

jtf = new JTextField(10);
jpn1.add(jtf);

JButton jbtn0 = new JButton("0");
JButton jbtn1 = new JButton("1");
jpn2.add(jbtn0);
jpn2.add(jbtn1);
JButton jbtnarti = new JButton("+");
JButton jbtneksi = new JButton("-");
JButton jbtnve = new JButton("VE");
JButton jbtnveya = new JButton("VEYA");
jpn3.add(jbtnarti);
jpn3.add(jbtneksi);
jpn3.add(jbtnve);
jpn3.add(jbtnveya);

JButton jbtnesittir = new JButton("=");
jpn4.add(jbtnesittir);
jbtn0.addActionListener(this);
jbtn1.addActionListener(this);

jbtnarti.addActionListener(this);
jbtneksi.addActionListener(this);
jbtnve.addActionListener(this);
jbtnveya.addActionListener(this);
jbtnesittir.addActionListener(this);



jfrm.add(jpn1);
jfrm.add(jpn2);
jfrm.add(jpn3);
jfrm.add(jpn4);
jfrm.setVisible(true);
}

int islem;
int sayi1,sayi2;

public void actionPerformed(ActionEvent ae)
{

if(ae.getActionCommand().equals("0"))
{

if(jtf.getText().length()<8)
	jtf.setText(jtf.getText()+"0");
}
if(ae.getActionCommand().equals("1"))
{
if(jtf.getText().length()<8)
	jtf.setText(jtf.getText()+"1");
}


if(jtf.getText().length()>0){
	if(islem==0)
	{
		if(ae.getActionCommand().equals("+"))
			islem=1;
		if(ae.getActionCommand().equals("-"))
			islem=2;
		if(ae.getActionCommand().equals("VE"))
			islem=3;
		if(ae.getActionCommand().equals("VEYA"))
			islem=4;
	
		if(islem>0)
		{
			sayi1=Integer.parseInt(jtf.getText()); // birinci sayi
			jtf.setText("");	
		}

	}else
	{
		if(ae.getActionCommand().equals("="))
		{
			sayi2=Integer.parseInt(jtf.getText()); // ikinci sayi
			hesapla();
			islem=0;
		}
	}
}

}
public void hesapla()
{
String sonuc="";
int b1=0,b2=0,elde=0;
for(int i=0;i<8;i++)
{
	b1=sayi1%10;
	b2=sayi2%10;
	sayi1=(sayi1-b1)/10;	
	sayi2=(sayi2-b2)/10;	
	int bassonuc=0;
	if(islem<3)
	{ // + ya da -
	
		if(islem==2){
			if(b2==1)
				b2=0;
			else
				b2=1;		
		if(i==0)	
			elde=1;
		}
	bassonuc=b1+b2+elde;
	elde=0;
	if(bassonuc>1)
	{
		elde=1;
		bassonuc-=2;
	}
	}
	
	if(islem==3)
	{ // ve
		if(b1==1 && b2==1)
			bassonuc=1;
	}
	
	if(islem==4)
	{ // veya
		if(b1==0 && b2==0)
			bassonuc=0;
		else
			bassonuc=1;
	}
sonuc=bassonuc+sonuc;
}

if(sonuc.length()>0)
jtf.setText(sonuc);
else
jtf.setText("0");
	
}
public static void main(String[] asd)
{

new soru1();

}

}

 

Share