import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MyFrame01 extends JFrame {
private JButton redButton, blueButton, yellowButton, bgButton;
private JPanel panel;
private String prevBtn = "";
// Frame에는 배경색을 줄 수가 없다
public MyFrame01() {
setTitle("버튼이벤트1");
// setSize(300, 250);
// 위치와 사이즈를 동시에 지정가능
setBounds(400, 300, 300, 200);
setLayout(new FlowLayout());
setVisible(true);
panel = new JPanel();
redButton = new JButton("빨간색");
blueButton = new JButton("파란색");
yellowButton = new JButton("노란색");
bgButton = new JButton("배경색");
redButton.setBackground(Color.WHITE);
blueButton.setBackground(Color.WHITE);
yellowButton.setBackground(Color.WHITE);
bgButton.setBackground(Color.WHITE);
redButton.addActionListener(new Event());
blueButton.addActionListener(new Event());
yellowButton.addActionListener(new Event());
bgButton.addActionListener(new Event());
add(redButton);
add(blueButton);
add(yellowButton);
add(bgButton);
add(panel);
}
class Event implements ActionListener {
private void setInit(String prevBtn) {
if(prevBtn.equals("빨간색")){
redButton.setBackground(Color.WHITE);
redButton.setForeground(Color.BLACK);
}else if(prevBtn.equals("파란색")){
blueButton.setBackground(Color.WHITE);
blueButton.setForeground(Color.BLACK);
}else if(prevBtn.equals("노란색")){
yellowButton.setBackground(Color.WHITE);
yellowButton.setForeground(Color.BLACK);
}
}
private void changeInit(String btn) {
if(btn.equals("빨간색")){
redButton.setBackground(Color.RED);
redButton.setForeground(Color.WHITE);
}else if(btn.equals("파란색")){
blueButton.setBackground(Color.BLUE);
blueButton.setForeground(Color.WHITE);
}else if(btn.equals("노란색")){
yellowButton.setBackground(Color.YELLOW);
yellowButton.setForeground(Color.WHITE);
}
}
@Override
public void actionPerformed(ActionEvent e) {
String source = e.getActionCommand();
// Object btn = e.getSource();
JButton btn = (JButton) e.getSource();
switch (source) {
case "빨간색":
setInit(prevBtn);
changeInit(source);
prevBtn = source;
break;
case "파란색":
setInit(prevBtn);
changeInit(source);
prevBtn = source;
break;
case "노란색" :
setInit(prevBtn);
changeInit(source);
prevBtn = source;
case "배경색" :
panel.setBackground(Color.GRAY);
default:
break;
}
}
}
}
public class ex01{
public static void main(String[] args) {
MyFrame01 frame01 = new MyFrame01();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame02 extends JFrame{
JButton button;
public MyFrame02() {
setTitle("외부 이벤트 예제");
setSize(300, 250);
setVisible(true);
setLayout(new FlowLayout());
button = new JButton("클릭");
button.addActionListener(new MyListener());
add(button);
}
}
// 독립적인 클래스로 이벤트 처리기 작성
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// getSource() 이벤트를 발생시킨 객체를 식별, Object 반환, 타입 변환
JButton button = (JButton) e.getSource();
if(e.getSource() == button) {
button.setText("마침내 버튼이 눌러졌습니다");
}
}
}
public class ex02 {
public static void main(String[] args) {
MyFrame02 frame02 = new MyFrame02();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame03 extends JFrame{
JButton button;
public MyFrame03() {
setTitle("inner 이벤트 예제");
setSize(300, 250);
setVisible(true);
setLayout(new FlowLayout());
button = new JButton("클릭");
button.addActionListener(new MyListener());
add(button);
}
// inner class
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// getSource() 이벤트를 발생시킨 객체를 식별, Object 반환, 타입 변환
JButton button = (JButton) e.getSource();
if(e.getSource() == button) {
button.setText("마침내 버튼이 눌러졌습니다");
}
}
}
}
public class ex03 {
public static void main(String[] args) {
MyFrame03 frame03 = new MyFrame03();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame04 extends JFrame implements ActionListener {
private JPanel panel;
private JButton button;
public MyFrame04() {
setTitle("동시 상속 이벤트 예제");
setSize(300, 250);
setVisible(true);
setLayout(new FlowLayout());
button = new JButton("클릭");
button.addActionListener(this);
add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
// getSource() 이벤트를 발생시킨 객체를 식별, Object 반환, 타입 변환
if(e.getSource() == button) {
button.setText("마침내 버튼이 눌러졌습니다");
}
}
}
public class ex04 {
public static void main(String[] args) {
MyFrame04 frame04 = new MyFrame04();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame05 extends JFrame implements ActionListener {
private JPanel panel;
private JButton button;
public MyFrame05() {
setTitle("asd 클래스 이벤트 예제");
setSize(300, 250);
setVisible(true);
setLayout(new FlowLayout());
button = new JButton("클릭");
add(button);
// 무명(익명) 클래스로 이벤트 처리기를 작성, 클래스 정의 + 객체 생성 동시 작성
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// getSource() 이벤트를 발생시킨 객체를 식별, Object 반환, 타입 변환
if(e.getSource() == button)
button.setText("마침내 버튼이 눌러졌습니다");
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
public class ex05 {
public static void main(String[] args) {
MyFrame05 frame05 = new MyFrame05();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame06 extends JFrame {
JButton button;
JPanel panel;
public MyFrame06() {
setTitle("rgb");
setSize(300, 250);
setVisible(true);
setLayout(new FlowLayout());
button = new JButton("클릭");
panel = new JPanel();
panel.setSize(100, 100);
add(button);
add(panel);
button.addMouseListener(new MyListner2());
}
class MyListner2 extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
int r = (int)(Math.random() * 256);
int g = (int)(Math.random() * 256);
int b = (int)(Math.random() * 256);
if(e.getClickCount() == 2) {
panel.setBackground(new Color(r,g,b));
}
}
}
}
public class ex06 {
public static void main(String[] args) {
MyFrame06 frame06 = new MyFrame06();
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import javax.swing.*;
class MyFrame07 extends JFrame {
JButton rockBtn, scissorsBtn, paperBtn;
JPanel btnPanel, enemyPanel, resultPanel;
JLabel enemyLabel, resultLabel;
public MyFrame07() {
setTitle("가위바위보");
setSize(600, 400);
setVisible(true);
enemyLabel = new JLabel("상대방",JLabel.CENTER);
// textField는 가운데 정렬이 없기때문에 .setHorizontalAlignment(0);
resultLabel = new JLabel("결과");
btnPanel = new JPanel(new GridLayout(0, 3));
enemyPanel = new JPanel();
enemyPanel.setBackground(Color.WHITE);
resultPanel = new JPanel();
resultPanel.setBackground(Color.WHITE);
rockBtn = new JButton();
scissorsBtn = new JButton();
paperBtn = new JButton();
rockBtn.addActionListener(new GameEvent());
scissorsBtn.addActionListener(new GameEvent());
paperBtn.addActionListener(new GameEvent());
rockBtn.setBackground(Color.WHITE);
scissorsBtn.setBackground(Color.WHITE);
paperBtn.setBackground(Color.WHITE);
rockBtn.setIcon(new ImageIcon("D:/mms/2021-03-03/src/바위.jpg"));
scissorsBtn.setIcon(new ImageIcon("D:/mms/2021-03-03/src/가위.jpg"));
paperBtn.setIcon(new ImageIcon("D:/mms/2021-03-03/src/보자기.jpg"));
btnPanel.add(rockBtn);
btnPanel.add(scissorsBtn);
btnPanel.add(paperBtn);
enemyPanel.add(enemyLabel);
resultPanel.add(resultLabel);
add(BorderLayout.NORTH, enemyPanel);
add(BorderLayout.CENTER, btnPanel);
add(BorderLayout.SOUTH, resultPanel);
}
class GameEvent implements ActionListener {
private void enemyResult(int num) {
switch (num) {
case 0:
enemyLabel.setText("바위");
break;
case 1:
enemyLabel.setText("가위");
break;
case 2:
enemyLabel.setText("보");
break;
default:
break;
}
}
private void gameResult(int num, String player) {
switch (player) {
case "Rock":
if(num == 0) setResult(2);
else if(num == 1) setResult(1);
else setResult(3);
break;
case "Scissor":
if(num == 0) setResult(3);
else if(num == 1)setResult(2);
else setResult(1);
break;
default:
if(num == 0) setResult(1);
else if(num == 1)setResult(3);
else setResult(2);
}
}
private void setResult (int num) {
switch (num) {
case 1:
resultLabel.setText("이겼습니다");
resultPanel.setBackground(Color.GREEN);
resultLabel.setForeground(Color.WHITE);
break;
case 2:
resultLabel.setText("비겼습니다");
resultPanel.setBackground(Color.ORANGE);
resultLabel.setForeground(Color.WHITE);
break;
default:
resultLabel.setText("졌습니다");
resultPanel.setBackground(Color.RED);
resultLabel.setForeground(Color.WHITE);
}
}
@Override
public void actionPerformed(ActionEvent e) {
int num = (int)(Math.random()*3);
enemyResult(num);
gameResult(num, e.getActionCommand());
}
}
}
public class ex07 {
public static void main(String[] args) {
MyFrame07 frame07 = new MyFrame07();
Scanner scanner = new Scanner(System.in);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyFrame08 extends JFrame {
JButton button;
public MyFrame08() {
setSize(300, 200);
setTitle("마우스엔터익사잇이벤트");
setLayout(new FlowLayout());
setVisible(true);
button = new JButton("엔터");
button.setBackground(Color.YELLOW);
button.addMouseListener(new mouseEvent());
add(button);
}
class mouseEvent implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
button.setBackground(Color.RED);
button.setForeground(Color.WHITE);
}
@Override
public void mouseExited(MouseEvent e) {
button.setBackground(Color.YELLOW);
button.setForeground(Color.BLACK);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
public class ex08 {
public static void main(String[] args) {
MyFrame08 frame08 = new MyFrame08();
}
}