화면 색상을 바꿔주는 이벤트 리스너 생성

package GUI;

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

public class MyFrame extends JFrame {
    private JButton b1;
    private JButton b2;
    private JPanel p;
    Mylis listener = new Mylis();

    public MyFrame() {
        this.setSize(300, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("이벤트 예제");
        p = new JPanel();
        b1 = new JButton("노란색");
        b1.addActionListener(listener);
        p.add(b1);
        b2 = new JButton("핑크색");
        b2.addActionListener(listener);
        p.add(b2);
        this.add(p);
        this.setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private class Mylis implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == b1) {
                p.setBackground(Color.yellow);
            } else if (e.getSource() == b2) {
                p.setBackground(Color.pink);
            }
        }
    }

    public static void main(String[] args) {
        MyFrame t = new MyFrame();
    }
}

Untitled

Untitled

Untitled

키패드 만들기

package GUI;

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

public class KeyPad extends JFrame implements ActionListener {
    private JTextField txt;
    private JPanel pl;

    public KeyPad() {
        txt = new JTextField(20);
        add(txt, BorderLayout.NORTH);
        pl = new JPanel();
        pl.setLayout(new GridLayout(3, 3));
        add(pl, BorderLayout.CENTER);
        for (int i = 1; i <= 9; i++) {
            JButton btn = new JButton("" + i);
            btn.addActionListener(this);
            btn.setPreferredSize(new Dimension(100, 30));
            pl.add(btn);
        }
        pack();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        txt.setText(txt.getText() + actionCommand);
    }

    public static void main(String[] args) {
        KeyPad k = new KeyPad();
    }
}

Untitled

가위 바위 보 게임

package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class RockPeperScissor extends JFrame implements ActionListener {
    final int SCISSOR = 0;
    final int ROCK = 1;
    final int PEPER = 2;
    int draw = 0;
    int win = 0;
    int defeat = 0;
    private JPanel p;
    private JLabel output, information;
    private JButton rock, paper, scissor;

    public RockPeperScissor() {
        setTitle("가위, 바위, 보");
        setSize(400, 150);

        p = new JPanel();
        p.setLayout(new GridLayout(0, 3));

        information = new JLabel("아래의 버튼 중에서 하나를 클릭하시오!");
        output = new JLabel("Good Luck");
        rock = new JButton("0: 가위");
        paper = new JButton("1: 바위");
        scissor = new JButton("2: 보");
        rock.addActionListener(this);
        paper.addActionListener(this);
        scissor.addActionListener(this);
        p.add(rock);
        p.add(paper);
        p.add(scissor);

        add(information, BorderLayout.NORTH);
        add(p, BorderLayout.CENTER);
        add(output, BorderLayout.SOUTH);
        setVisible(true);
    }

    public static void main(String[] args) {
        RockPeperScissor r = new RockPeperScissor();
    }

    public void actionPerformed(ActionEvent e) {
        JButton b = (JButton) e.getSource();
        int user = Integer.parseInt("" + b.getText().charAt(0));

        Random ra = new Random();
        int computer = ra.nextInt(3);
        if (user == computer) {
            draw++;
            output.setText("인간과 컴퓨터가 비겼음" +
                    "비긴 횟수: " + draw + "이긴 횟수: " + win + "진 횟수: " + defeat);
        } else if (user == (computer + 1) % 3) {
            win++;
            output.setText("인간: " + user + "컴퓨터: " + computer + "인간 승리" +
                    " 비긴 횟수: " + draw + " 이긴 횟수: " + win + " 진 횟수: " + defeat);
        } else {
            defeat++;
            output.setText("인간: " + user + "컴퓨터: " + computer + "컴퓨터 승리" +
                    " 비긴 횟수: " + draw + " 이긴 횟수: " + win + " 진 횟수: " + defeat);
        }
    }
}

Untitled

키보드 이벤트 이미지 이동시키기

package GUI;

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

public class MoveImage extends JFrame {
    int img_x = 150, img_y = 150;
    JButton button;

    public MoveImage() {
        setSize(1280, 720);
        button = new JButton("");
        ImageIcon icon = new ImageIcon("c:\\\\a.png");

        button.setIcon(icon);
        JPanel p = new JPanel();
        p.setLayout(null);
        button.setLocation(img_x, img_y);
        button.setSize(500, 600);
        p.add(button);
        p.requestFocus();
        p.setFocusable(true);
        p.addKeyListener(new KeyListener() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keycode = e.getKeyCode();
                switch (keycode) {
                    case KeyEvent.VK_UP:
                        img_y -= 10;
                        break;
                    case KeyEvent.VK_DOWN:
                        img_y += 10;
                        break;
                    case KeyEvent.VK_LEFT:
                        img_x -= 10;
                        break;
                    case KeyEvent.VK_RIGHT:
                        img_x += 10;
                        break;
                }
                button.setLocation(img_x, img_y);
            }

            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
        add(p);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        MoveImage m = new MoveImage();
    }
}