JAVA

11일차

구자룡 2021. 4. 8. 09:05

//일단 버튼만 누르면 빠져나오는 프로그램.(이벤트처리됨)

import java.awt.*;
import java.awt.event.*;
public class Exam01 extends Frame{
	private Button bt = new Button("확인");
	private GridBagLayout gbl = new GridBagLayout();//화면의 정중앙
	public Exam01(String title) {
		super(title);//super은 상위 생성자를 호출한다는 뜻이다.
		
		this.init();
		this.start();//Event추가 or Thread추가되는 메소드임.
		
		super.setSize(500,300);
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frm = super.getSize();
		int xpos = (int)(screen.getWidth()/2-frm.getWidth()/2);
		int ypos = (int)(screen.getHeight()/2-frm.getHeight()/2);
		super.setLocation(xpos,ypos);
		
		super.setResizable(false);
		super.setVisible(true);		
	}
	public void init() {
		this.setLayout(gbl);
		this.add(bt);
		
	}
	public void start() {
		A ap = new A();
		bt.addActionListener(ap);
	}
	
	public static void main(String[] ar) {
		new Exam01("제목");
		
	}

}
class A implements ActionListener{
//	public void aaa() {
//		System.exit(0);
//	}
	public void actionPerformed(ActionEvent e) {
		System.exit(0);
	}
}

//ActionListener를 사용하여 처리하는 이벤트방법.

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

public class Exam02 extends JFrame{
	public Exam02() {
		setTitle("Actrion 이벤트리스너예제");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//진행되는 thread(작은 cpu)제거시켜줌
		Container c = getContentPane();
		JButton btn = new JButton("Action");
		btn.addActionListener(new MyExam());
		c.add(btn);
		
		setSize(350,150);
		setVisible(true);
	}
	public static void main(String[] ar) {
		new Exam02();
}
}
class MyExam implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		JButton b = (JButton)e.getSource();//이벤트소스버튼 알아내기.
		if(b.getText().equals("Action"))
			b.setText("액션");
		else
			b.setText("Action");
	}
}

//MouseListener를 사용하여 처리하는 이벤트처리 프로그램.

import java.awt.*;
import java.awt.event.*;
public class Exam03 extends Frame{
	private Button bt = new Button("확인");
	private GridBagLayout gbl = new GridBagLayout();
	
	public Exam03(String title) {
		super(title);//super은 상위 생성자를 호출한다는 뜻이다.
		super.setSize(500,300);
	
		this.init();//화면의 초기화를 위한 메소드
		this.start();//Event추가 처리를 하는 메소드
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frm = super.getSize();
		int xpos = (int)(screen.getWidth()/2-frm.getWidth()/2);
		int ypos = (int)(screen.getHeight()/2-frm.getHeight()/2);
		super.setLocation(xpos,ypos);
		
		super.setResizable(false);
		super.setVisible(true);		
	}
	public void init() {
		this.setLayout(gbl);
		this.add(bt);
	}
	public void start() {
		AA ap = new AA();
		bt.addMouseListener(ap);
	}
	
	public static void main(String[] ar) {
		new Exam03("제목");
		
	}

}
	class AA implements MouseListener{
		public void mouseClicked(MouseEvent e) {
			System.exit(0);
		}
		public void mousePressed(MouseEvent e) {}
		public void mouseEntered(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
	}


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

public class Exam552 extends JFrame {
	private JLabel la = new JLabel("Hello");
	
	public Exam552() {
		setTitle("Mouse 이벤츠 예제");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		c.addMouseListener(new MyMouseListener());
		
		c.setLayout(null);
		la.setSize(50,20);
		la.setLocation(30,30);
		c.add(la);
		
		setSize(250,250);
		setVisible(true);
	}
	class MyMouseListener implements MouseListener {
		public void mousePressed(MouseEvent e) {
			int x = e.getX();
			int y = e.getY();
			la.setLocation(x,y);
		}
		public void mousePeleased(MouseEvent e) {}
		public void mouseClicked(MouseEvent e) {}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
	}
	
	public static void main(String[] ar) {
		new Exam552();
	}
}

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

public class Exam563 extends JFrame {
	private JLabel la = new JLabel();
	
	public Exam563() {
		setTitle("Key Code 예제 : F1키 초록색, % 키 노란색");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		c.addKeyListener(new MyKeyListener());
		c.add(la);
		setSize(300,150);
		setVisible(true);
		
		c.setFocusable(true);
		c.requestFocus();
	}
	
	class MyKeyListener extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			Container contentPane = (Container)e.getSource();
			
			la.setText(KeyEvent.getKeyText(e.getKeyCode())+"키가 입력되었음");
			
			if(e.getKeyChar() == '%')
				contentPane.setBackground(Color.YELLOW);
			else if(e.getKeyCode() == KeyEvent.VK_F1)
				contentPane.setBackground(Color.GREEN);
		}
	}
	public static void main(String[] ar) {
		new Exam563();
	}
}

'JAVA' 카테고리의 다른 글

13일차  (0) 2021.04.09
12일차  (0) 2021.04.08
10일차  (0) 2021.04.07
9일차  (0) 2021.04.07
8일차  (0) 2021.04.05