Nesne Tabanlı Programlama-2 (Bahar-2016) Hafta-4/1-1

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class checkbox extends JFrame implements ActionListener {
	
	JCheckBox jcb1,jcb2,jcb3;
	public checkbox()
	{
		this.setTitle("CheckBox Örneği");
		this.setSize(300,150);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		JPanel jp_main = new JPanel();
		jp_main.setPreferredSize(new Dimension(290, 140));
		JPanel jpn1 = new JPanel();
		JPanel jpn2 = new JPanel();
		
		jpn1.setPreferredSize(new Dimension(280, 60));
		jpn2.setPreferredSize(new Dimension(280, 65));
		jp_main.add(jpn1);
		jp_main.add(jpn2);
		
		jpn1.setLayout(new GridLayout(1,3));
		jpn2.setLayout(new FlowLayout());
		
		jcb1 = new JCheckBox("Seçim-1");
		jcb2 = new JCheckBox("Seçim-2");
		jcb3 = new JCheckBox("Seçim-3");
		
		/*jcb1.addActionListener(new ActionListener(){@Override
		public void actionPerformed(ActionEvent arg0) {
			JOptionPane.showMessageDialog(null, "Hello World!");
			
		}});*/
		
		
		jpn1.add(jcb1);
		jpn1.add(jcb2);
		jpn1.add(jcb3);
		
		JButton jbtn1= new JButton("Göster");
		JButton jbtn2= new JButton("Temizle");
		
		jbtn1.addActionListener(this);
		jbtn1.setActionCommand("goster");
		jbtn2.addActionListener(this);
		
		jpn2.add(jbtn1);
		jpn2.add(jbtn2);
		
		this.add(jp_main);
		this.setVisible(true);
	}
	

	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		if(arg0.getActionCommand().equals("goster"))
			goster();
		else
			temizle();
	
	}

	private void goster()
	{
		String yaz="";
		
		
		if(jcb1.isSelected())
			yaz+=jcb1.getText();
		if(jcb2.isSelected())
		{
			if(yaz.length()>0) yaz+=" ; ";
			yaz+=jcb2.getText();
		}
		
		if(jcb3.isSelected())
		{
			if(yaz.length()>0) yaz+=" ; ";
			yaz+=jcb3.getText();
		}
		
		if(yaz.length()==0)
			yaz="Seçili Eleman Yok!";
		
		JOptionPane.showMessageDialog(null, yaz);
		
	}
	private void temizle()
	{
		jcb1.setSelected(false);
		jcb2.setSelected(false);
		jcb3.setSelected(false);
		
	}
	
	public static void main(String[] args) {
		new checkbox();

	}

}

 

Share