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

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();
}

}
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();
}

}
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();
}

}
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();
}

}
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();
}

}
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();
}

}

Share