관리 메뉴

moozi

Java Event 처리 정리 본문

TIS_2020/빅데이터2020_1기

Java Event 처리 정리

moozi 2020. 9. 18. 09:45
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

class MyListener1 implements ActionListener{
	private JLabel label=null;
	@Override
	public void actionPerformed(ActionEvent e) {
		//EventEx.label.setText("버튼1이 클릭됨");
		this.label.setText("버튼1이 클릭됨");
	}	
	public void setLabel(JLabel label) {
		this.label=label;
	}
}

public class EventEx extends JFrame implements ActionListener {
	//public static JLabel label=null;	
	public  JLabel label=null;
	
	class MyListener2 implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			label.setText("버튼2가 클릭됨");
		}
	}	
	
	public EventEx() {
		this.setTitle("Event정리");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c=this.getContentPane();
		c.setLayout(new FlowLayout());
		
		label=new JLabel("이벤트 처리 메시지");
		label.setPreferredSize(new Dimension(320,50));
		c.add(label);
		
		JButton btn1=new JButton("버튼1");
		//btn1.addActionListener(new MyListener1());
		MyListener1 myListener1=new MyListener1();
		myListener1.setLabel(this.label);
		btn1.addActionListener(myListener1);
		
		c.add(btn1);
		JButton btn2 = new JButton("버튼2");
		btn2.addActionListener(new MyListener2());		
		c.add(btn2);
		
		
		JButton btn3 = new JButton("버튼3");
		btn3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				label.setText("버튼3이 클릭됨");			
			}});
		c.add(btn3);		
		
		JButton btn4 = new JButton("버튼4");
		btn4.addActionListener(this);
		c.add(btn4);
		
		
		this.setSize(350, 300);
		this.setVisible(true);
		
	}
	
	public static void main(String[] args) {
		new EventEx();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		label.setText("버튼4가 클릭됨");	
		
	}
}

'TIS_2020 > 빅데이터2020_1기' 카테고리의 다른 글

ANSI SQL inner join  (0) 2020.09.22
9/21 sql  (0) 2020.09.21
msvcr100.dll  (0) 2020.09.18
Java Event 처리 정리  (0) 2020.09.18
JTabbedPane  (0) 2020.09.17
Haksa  (0) 2020.09.14
0 Comments
댓글쓰기 폼