Java-2 2016 Hafta-7 Örnek-1

java2_16_hafta7_ornek1

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

public class jlistornek implements ActionListener {

	JList jlist;
	DefaultListModel dlm;
	JTextField jtf; 
	JLabel jlab_say;
	public jlistornek() {
		JFrame jfrm = new JFrame("List Ornegi");
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jfrm.setSize(150,300);
		jfrm.setLayout(new FlowLayout());
		
	
		JPanel jpn1 = new JPanel();
		jpn1.setPreferredSize(new Dimension(130,200));
		
		JPanel jpn2 = new JPanel();
		jpn2.setLayout(new GridLayout(3,1));
		jpn2.setPreferredSize(new Dimension(130,100));
		JPanel jpn2_1 = new JPanel();
		JPanel jpn2_2 = new JPanel();
		JPanel jpn2_3 = new JPanel();
		jpn2.add(jpn2_1);
		jpn2.add(jpn2_2);
		jpn2.add(jpn2_3);
		
		jfrm.add(jpn1);
		jfrm.add(jpn2);

		
		dlm = new DefaultListModel();
		jlist = new JList(dlm);
		JScrollPane jsp = new JScrollPane(jlist);
		jsp.setPreferredSize(new Dimension(130,190));
		jpn1.add(jsp);

		
		
		jtf = new JTextField(10);
		jpn2_1.add(jtf);
		
		JButton jbtn_ekle = new JButton("Ekle");
		jbtn_ekle.addActionListener(this);
		jbtn_ekle.setActionCommand("ekle");
		JButton jbtn_sil = new JButton("Sil");
		jbtn_sil.addActionListener(this);
	
		jpn2_2.add(jbtn_ekle);
		jpn2_2.add(jbtn_sil);
		
		jlab_say = new JLabel("Eleman sayisi: 0");
		jpn2_3.add(jlab_say);
				
		//jfrm.pack();
		jfrm.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		
		if(ae.getActionCommand().equals("ekle"))
		{
			// ekle
			if(jtf.getText().length()>0)
			{
				dlm.addElement(jtf.getText());
				jtf.setText("");
			}	
		}
		else
		{
			int secilen=jlist.getSelectedIndex();
			if(secilen>-1)
				dlm.remove(secilen);
		}
		
		say();
		

	}
	
	private void say()
	{
		int say=dlm.getSize();
		jlab_say.setText("Eleman sayisi: "+say);
		
		
	}

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

	}

}

 

Share