목차
시스템 스크린 사이즈 갖고오기
java.awt.Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screen = kit.getScreenSize();
int w, h;
w = screen.width/2;
h = screen.height/2;
Dimension app = new Dimension(500, 800);
setSize(app);
setResizable(false);
setLocation(w-app.width/2, h-app.height/2);
setVisible(true);
Event 만들기
윈도우 리스너 가져오기
public class Ex02 extends Frame implements WindowListener{
public Ex02() {
addWindowListener(this);
setBounds(0, 100, 400, 300);
setVisible(true);
}
public static void main(String[] args) {
new Ex02();
}
@Override
public void windowOpened(WindowEvent e) {
System.out.println("call windowOpened");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("call windowClosing"); // 창의 "x"버튼을 눌렀을 떄
// setVisible(false); // 창을 안보이게 하기
// System.exit(0); // jvm 강제로 종료시키기
dispose(); // window를 deactivate 시킨 후 windowClosed를 호출한다.
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("call windowClosed"); // 종료시 후행작업을 수행한다.
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("call windowIconified"); // 창 최소화
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("call windowDeiconified"); // 창 최소화 해제
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("call windowActivated");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("call windowDeactivated");
}
}
public class Ex03 implements WindowListener {
static Frame f;
public static void main(String[] args) {
f = new Frame();
f.addWindowListener(new Ex03());
f.setBounds(0, 100, 200, 200);
f.setVisible(true);
}
@Override
public void windowClosing(WindowEvent e) {
f.dispose();
}
마우스로 버튼 클릭시 이벤트 발생
MouseListener
public class Ex04 extends Frame implements MouseListener{
// Button btn;
JLabel la;
public Ex04() {
setLayout(null);
ImageIcon icon = new ImageIcon("airplane.jpg");
//// btn = new Button("button");
// btn = new JLabel();
// btn.setIcon(icon);
// btn.setSize(200, 100);
// btn.setLocation(100, 100);
//// btn.addMouseListener(this);
// this.addMouseListener(this); // 창과 마우스의 상호작용 반환
// add(btn);
// Toolkit kit = Toolkit.getDefaultToolkit();
// Image img = kit.createImage("airplane.png");
la = new JLabel();
la.setIcon(icon);
la.setSize(200, 100);
la.setLocation(100, 100);
this.addMouseListener(this);
add(la);
setBounds(100, 100, 400, 300);
setVisible(true);
}
public static void main(String[] args) {
new Ex04();
}
@Override
public void mousePressed(MouseEvent e) {
// System.out.println("call mousePressed");
int x = e.getX();
int y = e.getY();
la.setLocation(x-50, y-50);
}
}
MouseMotionListender
public class Ex05 extends Frame implements MouseMotionListener {
JLabel la;
public Ex05() {
setLayout(null);
addMouseMotionListener(this);
ImageIcon icon = new ImageIcon("airplane.jpg");
la = new JLabel(icon);
// la.setIcon(icon);
// la.setSize(icon.getIconWidth(), icon.getIconHeight());
la.setSize(200, 100);
la.setLocation(100, 100);
add(la);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex05();
}
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("call mouseDragged");
}
@Override
public void mouseMoved(MouseEvent e) {
// System.out.println(e.getX() + ", " + e.getY());
int x = e.getX();
int y = e.getY();
la.setLocation(x-50, y-50);
}
}
키보드 클릭시 이벤트 발생
public class Ex06 extends Frame implements KeyListener{
TextField tf;
public Ex06() {
setLayout(new FlowLayout());
tf = new TextField(15);
tf.addKeyListener(this);
add(tf);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex06();
}
@Override
public void keyTyped(KeyEvent e) {
// 문자(폰트)를 타입했을때만 호출됨
System.out.print("call keyTyped:" + e.getKeyCode() + ", " + e.getKeyChar());
System.out.println(" => " + tf.getText()); // ab
}
@Override
public void keyPressed(KeyEvent e) {
// 모든 키보드를 눌렀을때 호출됨
System.out.print("call keyPressed:" + e.getKeyCode() + ", " + e.getKeyChar());
System.out.println(" => " + tf.getText()); // ab
}
@Override
public void keyReleased(KeyEvent e) {
System.out.print("call keyReleased");
System.out.println(" => " + tf.getText()); // abc
}
}
public class Ex07 extends Frame implements KeyListener{
JLabel la;
public Ex07() {
setLayout(new FlowLayout());
// TextField tf = new TextField();
// Button btn = new Button("new Button");
la = new JLabel("new Button");
la.setSize(50, 50);
la.setLocation(100, 100);
this.addKeyListener(this);
// add(tf);
// add(btn);
add(la);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex07();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
// System.out.println("call" + e.getKeyCode());
if(e.getKeyCode() == 37) {
if(la.getX()-100 >= 0+10)
la.setLocation(la.getX()-100, la.getY());
}
if(e.getKeyCode() == 39) {
if(la.getX()+100 <= this.getWidth()-10)
la.setLocation(la.getX()+100, la.getY());
}
if(e.getKeyCode() == 38) {
if(la.getY()-100 >= 0+5)
la.setLocation(la.getX(), la.getY()-100);
}
if(e.getKeyCode() == 40) {
if(la.getY()+100 <= this.getHeight()-5)
la.setLocation(la.getX(), la.getY()+100);
}
}
}
TextListener
public class Ex08 extends Frame implements TextListener{
public Ex08() {
setLayout(new FlowLayout());
TextField tf = new TextField(15);
tf.addTextListener(this);
add(tf);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex08();
}
@Override
public void textValueChanged(TextEvent e) {
TextField tf = (TextField)e.getSource();
System.out.println(tf.getText());
}
}
ItemListener
public class Ex09 extends Frame implements ItemListener{
public Ex09() {
setLayout(new FlowLayout());
Choice list1 = new Choice();
list1.setFont(new Font("", 0, 15));
list1.add("item1");
list1.add("item2");
list1.add("item3");
list1.add("item4");
// list1.addItemListener(this);
List list2 = new List(4, true);
list2.setFont(new Font("", 0, 15));
list2.add("item1");
list2.add("item2");
list2.add("item3");
list2.add("item4");
list2.add("item5");
list2.addItemListener(this);
add(list1);
add(list2);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex09();
}
@Override
public void itemStateChanged(ItemEvent e) {
System.out.println(e.getItem());
System.out.println(e.getSource());
System.out.println(e.getStateChange());
Object[] arr = e.getItemSelectable().getSelectedObjects();
System.out.println(java.util.Arrays.deepToString(arr));
}
}
ActionListener
- 컴포넌트 고유의 이벤트를 실행한다.
public class Ex10 extends Frame implements ActionListener{
public Ex10() {
setLayout(new FlowLayout());
Button btn = new Button("Button");
// btn.addActionListener(this);
TextField tf = new TextField(15);
tf.addActionListener(this); // enter를 쳤을때 호출된다.
add(btn);
add(tf);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex10();
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("call actionPerformed");
}
}
두개 이상의 ActionListener 사용하기
public class Ex10 extends Frame implements ActionListener{
Button btn;
TextField tf;
public Ex10() {
setLayout(new FlowLayout());
btn = new Button("Button");
btn.addActionListener(this);
tf = new TextField(15);
tf.setEchoChar('*');
tf.addActionListener(this); // enter를 쳤을때 호출된다.
add(btn);
add(tf);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex10();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == tf)
System.out.println(e.getActionCommand());
if(e.getSource() == btn)
System.out.println("button clicked");
}
}
public class Ex10 extends Frame implements ActionListener{
Button btn;
TextField tf;
class BtnEvent implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button clicked");
}
}
public Ex10() {
setLayout(new FlowLayout());
btn = new Button("Button");
btn.addActionListener(new BtnEvent());
tf = new TextField(15);
tf.setEchoChar('*');
tf.addActionListener(this); // enter를 쳤을때 호출된다.
add(btn);
add(tf);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex10();
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
혹은
public class Ex10 extends Frame implements ActionListener{
Button btn;
TextField tf;
public Ex10() {
setLayout(new FlowLayout());
btn = new Button("Button");
// btn.addActionListener(new ActionListener(){
//
// @Override
// public void actionPerformed(ActionEvent e) {
// System.out.println("button clicked");
// }
//
// });
btn.addActionListener(e -> System.out.println("클릭"));
tf = new TextField(15);
tf.setEchoChar('*');
tf.addActionListener(this); // enter를 쳤을때 호출된다.
add(btn);
add(tf);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex10();
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
입력된 TextField 문자를 안보이게 하는 법
public class Ex10 extends Frame implements ActionListener{
public Ex10() {
setLayout(new FlowLayout());
TextField tf = new TextField(15);
tf.setEchoChar('*');
tf.addActionListener(this); // enter를 쳤을때 호출된다.
add(tf);
setBounds(0, 200, 500, 600);
setVisible(true);
}
public static void main(String[] args) {
new Ex10();
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
Anonymous class를 이용해 WindowListener를 implement 하지 않고 windowCLosing 적용하기
class Temp implements WindowListener{
// @Override methods
}
public class Ex10 extends Frame implements ActionListener{
this.addWindowListener(new Temp() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}
});
public class Ex10 extends Frame implements ActionListener{
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}
});
학생성적관리프로그램
package com.Day13;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.util.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Ex13_answer implements ActionListener {
List<String[]> data = new ArrayList<>();
static TextField[] adds;
static Panel center = new Panel();
static Frame f;
static Font ft;
public static void main(String[] args) {
f = new Frame("학생성적관리프로그램 (ver 0.9.0)");
f.setLayout(new BorderLayout());
Panel top = new Panel();
top.setLayout(new FlowLayout());
center.setLayout(new FlowLayout());
ft = new Font("", Font.BOLD, 20);
adds = new TextField[5];
Label[] las = {
new Label("ID"),
new Label("Name"),
new Label("Kor"),
new Label("Eng"),
new Label("Math"),
};
for(int i = 0; i < adds.length; i++) {
adds[i] = new TextField(8);
adds[i].setFont(ft);
las[i].setFont(ft);
top.add(las[i]);
top.add(adds[i]);
}
Button addBtn = new Button("Insert");
addBtn.addActionListener(new Ex13_answer());
addBtn.setFont(ft);
top.add(addBtn);
f.add(top, BorderLayout.NORTH);
f.add(center, BorderLayout.CENTER);
java.awt.Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screen = kit.getScreenSize();
int w, h;
w = screen.width/2;
h = screen.height/2;
f.setBounds(w, h, 1000, 600);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String[] row = new String[] {
adds[0].getText(),
adds[1].getText(),
adds[2].getText(),
adds[3].getText(),
adds[4].getText(),
};
data.add(row);
centerValid();
// TextField[] col = new TextField[5];
// col[0] = new TextField(adds[0].getText(), 8);
// col[1] = new TextField(adds[1].getText(), 8);
// col[2] = new TextField(adds[2].getText(), 8);
// col[3] = new TextField(adds[3].getText(), 8);
// col[4] = new TextField(adds[4].getText(), 8);
//
// Panel row = new Panel();
// for(int i = 0; i < col.length; i++) {
// row.add(col[i]);
// }
// Button edit = new Button("Edit");
// Button del = new Button("Delete");
// row.add(edit);
// row.add(del);
// center.add(row);
// for(int i = 0; i < adds.length; i++) {
// adds[i].setText("");
// }
// f.validate();
}
void centerValid() {
center.removeAll();
Panel group1 = new Panel();
group1.add(new Label("ID\t\t\t\t"));
group1.add(new Label("Name\t\t\t\t"));
group1.add(new Label("Kor\t\t\t\t"));
group1.add(new Label("Eng\t\t\t\t"));
group1.add(new Label("Math\t\t\t\t\t\t\t"));
center.add(group1);
group1.setFont(ft);
for(int i = 0; i < data.size(); i++) {
String[] row = data.get(i);
Panel group = new Panel();
group.add(new TextField(row[0], 8));
group.add(new TextField(row[1], 8));
group.add(new TextField(row[2], 8));
group.add(new TextField(row[3], 8));
group.add(new TextField(row[4], 8));
Button btn1 = new Button("Edit");
Button btn2 = new Button("Delete");
group.add(btn1);
group.add(btn2);
group.setFont(ft);
center.add(group);
}
for(int i = 0; i < adds.length; i++) {
adds[i].setText("");
}
f.validate();
}
}
'100일 챌린지 > 빅데이터기반 인공지능 융합 서비스 개발자' 카테고리의 다른 글
Day 15 - 새 창 띄우기, I/O에 버퍼 달기 (0) | 2024.08.09 |
---|---|
Day 14 - 예외 처리, I/O (0) | 2024.08.08 |
Day12 - 내부 클래스, GUI (0) | 2024.08.06 |
Day 11 - generic, 순서가 없는 자료구조(Enumeration),그리고 Map (0) | 2024.08.05 |
Day10 - 동적할당, 자료 구조, List와 Set (0) | 2024.08.02 |