GUI 에 임의의 창을 만들고 각종 이벤트를 사용해 보겠다.
사용할 이벤트는 'KeyListener', 'MouseListener', 'ComponentListener' 이다.
00. 입력하고 실행시켜보자.
01. 창이 뜨는것을 확인할 수 있다.
02. 마우스 창내에 들어갔다 나갔다 했던것을 확인할 수 있다.
03. 누른것도 확인할 수 있다.
04. 만들어놓은 버튼을 누르면 색이 바뀌는 것을 확인할 수 있다.
05. 다음 버튼역시 지정해놓은 색으로 작동됨을 확인할 수 있다.
06. 누를 때마다 랜덤하게 색이 나온다.
※. 코드
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class MyFrame extends JFrame implements ActionListener,
ComponentListener, KeyListener, MouseListener
{
private JButton yellow;
private JButton green;
private JButton random;
private JTextArea debug;
private JTextField input;
private JPanel panel = new JPanel();
public MyFrame(){
this.setSize(500, 400);
this.setTitle("No Title");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public MyFrame(String title)
{
this.setSize(500, 400);
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Programming Here
this.setLayout(new BorderLayout());
panel.setLayout(new FlowLayout());
yellow = new JButton("Yellow");
green = new JButton("Green");
random = new JButton("Random");
debug = new JTextArea(10, 40);
JScrollPane scroll = new JScrollPane(debug);
input = new JTextField(40);
panel.add(yellow);
panel.add(green);
panel.add(random);
panel.add(scroll);
panel.add(input);
this.add(panel, BorderLayout.CENTER);
yellow.addActionListener(this);
green.addActionListener(this);
random.addActionListener(this);
panel.addComponentListener(this);
input.addKeyListener(this);
panel.addMouseListener(this);
this.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int count = e.getClickCount();
String info = "mouseClicked, " + x + ", " + y + "# " + count;
debug.append(info + "\n");
}
@Override
public void mouseEntered(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int count = e.getClickCount();
String info = "mouseEntered, " + x + ", " + y + "# " + count;
debug.append(info + "\n");
}
@Override
public void mouseExited(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int count = e.getClickCount();
String info = "mouseExited, " + x + ", " + y + "# " + count;
debug.append(info + "\n");
}
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int count = e.getClickCount();
String info = "mousePressed, " + x + ", " + y + "# " + count;
debug.append(info + "\n");
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
@Override
public void keyPressed(KeyEvent e) {
// A (CTRL, SHFT, ALT) = (true false true)
char c = e.getKeyChar();
String info = "(CTRL, SHFT, ALT) = (" + e.isControlDown() + e.isShiftDown() + e.isAltDown() + ")";
info = "PRESS " + c + info;
debug.append(info + "\n");
}
@Override
public void keyReleased(KeyEvent e) {
char c = e.getKeyChar();
String info = "(CTRL, SHFT, ALT) = (" + e.isControlDown() + e.isShiftDown() + e.isAltDown() + ")";
info = "RELEASE " + c + info;
debug.append(info + "\n");
}
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
String info = "(CTRL, SHFT, ALT) = (" + e.isControlDown() + e.isShiftDown() + e.isAltDown() + ")";
info = "TYPED " + c + info;
debug.append(info + "\n");
}
@Override
public void componentHidden(ComponentEvent e) {
debug.append("componentHidden \n");
}
@Override
public void componentMoved(ComponentEvent e) {
debug.append("componentMoved \n");
}
@Override
public void componentResized(ComponentEvent e) {
debug.append("componentResized \n");
}
@Override
public void componentShown(ComponentEvent e) {
debug.append("componentShown \n");
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == yellow)
{
panel.setBackground(Color.YELLOW);
}
if(e.getSource() == green)
{
panel.setBackground(Color.GREEN);
}
if(e.getSource() == random)
{
int r, g, b;
r = (int)(Math.random() * 256);
g = (int)(Math.random() * 256);
b = (int)(Math.random() * 256);
Color c = new Color(r, g, b);
panel.setBackground(c);
}
}
}
public class Test {
public static void main(String[] args) {
MyFrame frame = new MyFrame("GUI TEST");
//MyFrame frame = new MyFrame();
}
}