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

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

import javax.swing.*;


public class listeornek extends JFrame implements ActionListener {

	JTextField jtf;
	JList jl;
	DefaultListModel dlm;
	public listeornek()
	{
		this.setTitle("JList Ornek");
		this.setSize(200,400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		
		
		JPanel jpn_main = new JPanel();
		jpn_main.setPreferredSize(new Dimension(190, 390));
		
		JPanel jpn1 = new JPanel();
		jpn1.setPreferredSize(new Dimension(180, 280));
		
		dlm = new DefaultListModel();
		jl= new JList(dlm);
		
		JScrollPane jscp = new JScrollPane(jl);
		jscp.setPreferredSize(new Dimension(180, 270));
		
		jpn1.add(jscp);
		
		
		JPanel jpn2 = new JPanel();
		jtf= new JTextField();
		jtf.setPreferredSize(new Dimension(180, 25));
		jpn2.add(jtf);
		
		
		JPanel jpn3 = new JPanel();
		JButton jbtn1= new JButton("Ekle");
		jbtn1.addActionListener(this);
		jbtn1.setActionCommand("ekle");
		JButton jbtn2= new JButton("Temizle");
		jbtn2.addActionListener(this);
		
		jpn3.add(jbtn1);
		jpn3.add(jbtn2);
				
		jpn_main.add(jpn1);
		jpn_main.add(jpn2);
		jpn_main.add(jpn3);
		this.add(jpn_main);
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		if(arg0.getActionCommand().equals("ekle"))
		{
			if(jtf.getText().length()>0)
			{
				dlm.addElement(jtf.getText());
				jtf.setText("");
			}
		}
		else
		{
			dlm.clear();
		}
		
	}

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

	}

}

 

Share