1 / 17

GUI 프로그래밍과 AWT(2)

GUI 프로그래밍과 AWT(2). 컴퓨터 공학실험 (I) 인공지능 연구실. AWT Event Model. Java 의 Event Model Event : 사용자가 Program 에 가한 행동 Event Source : Event 가 일어난 근원 Event Handler : 발생된 Event object 를 받아 적당한 행동을 취 하는 method. Event Handler(1). import java.awt.*;

barth
Download Presentation

GUI 프로그래밍과 AWT(2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GUI 프로그래밍과 AWT(2) 컴퓨터 공학실험(I) 인공지능 연구실

  2. AWT Event Model • Java의 Event Model • Event : 사용자가 Program에 가한 행동 • Event Source : Event가 일어난 근원 • Event Handler : 발생된 Event object를 받아 적당한 행동을 취 하는method

  3. Event Handler(1) • import java.awt.*; • public class ButtonTest { • public static void main(String args[]) { •         Frame f = new Frame("Button Test"); //Frame 생성 • Button b = new Button(“버튼1"); //Button 생성 • b.addActionListener(new ButtonHandler()); //addActionListener() method는 이 Button에 Event Handler를 지정 • f.add(b, "Center"); •         f.pack(); •         f.setVisible(true); • } • } 

  4. Event Handler(2) • import java.awt.event.*; •  public class ButtonHandler implements ActionListener { // interface 안에 있는 특정한 abstract method를 구현 • public void actionPerformed(ActionEvent e) { •         System.out.println("Button을 클릭하셨네요."); •         System.out.println("Button에 적힌 글은 " + e.getActionCommand()); •   } • } • Event Handler를 만들 때는 반드시 적당한 Event Handler Interface를 implements해야 한다 • addXXXListener()의 argument로 올 Event Handler class는 반드시 XXXListener interface를 implements 해야 한다.

  5. Method Categories and Interfaces

  6. AWT Component • Button • Checkbox • CheckboxGroup • Choice • Canvas • Label • TextField • TextArea • List • Dialog • ScrollPane • Menu

  7. AWT Component(1) - Button import java.awt.*; import java.awt.event.*; public class ButtonTest { Frame f ; Button b;  public static void main(String args[]) {        ButtonTest bt = new ButtonTest();         bt.go();  }  public void go() {         f = new Frame("Button Test");         b = new Button(“확인");     b.addActionListener(new ButtonHandler());  f.add(b, "Center");        f.pack();         f.setVisible(true);    }    class ButtonHandler implements ActionListener {         public void actionPerformed(ActionEvent ae) {                 if (ae.getActionCommand().equals("확인")) {….} // if (ae.getSource()==b){….}    }    } }

  8. AWT Component(2) – Check Box public class SampleCheckbox implements ItemListener {    private Frame f;    private Checkbox one, two;    public void go() {         f = new Frame("Sample Checkbox"); one = new Checkbox(“AI", true);         two = new Checkbox(“Lab", false);          one.addItemListener(this);         two.addItemListener(this);         …….. } public void itemStateChanged(ItemEvent ie) {         String state = "해제";         if (ie.getStateChange() == ItemEvent.SELECTED) {   state = "선택";    }         System.out.println (ie.getItem() + " " + state);    }

  9. AWT Component(3) – Radio Button public class TestCheckboxGroup implements ItemListener {    private Frame f;   private Checkbox one, two, three;    private CheckboxGroup cbg;    public void go() {         f = new Frame("Sample Checkbox");      cbg = new CheckboxGroup();         one = new Checkbox("대", cbg, true);    two = new Checkbox("중", cbg, false);         three = new Checkbox("소", cbg, false); ////////////////////////////// swing에서는 JRadioButton         one.addItemListener(this); two.addItemListener(this);  three.addItemListener(this); ……… } public void itemStateChanged(ItemEvent ev) {         String state = null;         if (ev.getStateChange() == ItemEvent.SELECTED) {                 state = "가 선택됨";                 System.out.println (ev.getItem() + " " + state);       } }

  10. AWT Component(3) – Combo Box public class SampleChoice implements ItemListener {    private Frame f;    private Choice choice; ///////// swing에서는 JComboBox    public void go() {         f = new Frame("Choice 연습");        choice = new Choice();         choice.addItem(“AI"); choice.addItem(“Lab"); choice.addItem("인공지능연구실");         choice.addItemListener(this); ……… } public void itemStateChanged(ItemEvent ev) {         String state = "deselected";         if (ev.getStateChange() == ItemEvent.SELECTED) {   state = "selected";    }         System.out.println (ev.getItem() + " " + state);    }

  11. AWT Component(3) – Canvas public class CanvasTest extends Canvas implements KeyListener{    int x=10, y=10, xh=10, yh=10;    public void paint(Graphics g) {  g.drawRect(x, y, xh, yh);    }    public void keyTyped(KeyEvent ev) {         if (ev.getKeyChar() == 'j') {  x -= 1;   x -= 1;   } else if (ev.getKeyChar() == 'l') {  x += 1;   x += 1; }         else if (ev.getKeyChar() == 'k') {  y += 1;   y += 1; }         else if (ev.getKeyChar() == 'i') {  y -= 1;  y -= 1;  }       repaint();    }    public void keyPressed(KeyEvent ev) { }    public void keyReleased(KeyEvent ev) { }    public static void main(String args[]) {         Frame f = new Frame("Canvas");         CanvasTest mc = new CanvasTest();         mc.setSize(150, 150);         f.add(mc, BorderLayout.CENTER);         mc.requestFocus();         mc.addKeyListener(mc);         f.pack();         f.setVisible(true);    } }

  12. AWT Component(3) – Label import java.awt.*; public class LabelTest{ public static void main(String args[]) {          Frame f = new Frame("이게 Label입니다.");   Label lb = new Label("사원정보");          f.add(lb);          f.pack();          f.setVisible(true); } }

  13. AWT Component(3) – Text Field import java.awt.*; import java.awt.event.*; public class TextFieldTest { private Frame f; private TextField tf; public void go() {          f = new Frame("TextField Test");       tf = new TextField(30);       tf.setEchoChar('*');          f.add(tf, BorderLayout.CENTER);          f.pack();          f.setVisible(true); } public static void main (String args[]) {          TextFieldTest tft = new TextFieldTest();          tft.go(); } }

  14. AWT Component(3) – Text Area import java.awt.*; import java.awt.event.*; public class TextAreaTest implements ActionListener { private Frame f; private TextArea ta, tac; private Button ok; public void go() {         f = new Frame("TextArea"); ta = new TextArea("Hello!", 4, 30);   tac = new TextArea(4, 30); ok = new Button("복사");         f.add(ta, BorderLayout.NORTH);       f.add(ok, BorderLayout.CENTER);         f.add(tac, BorderLayout.SOUTH);  ok.addActionListener(this);      f.pack();         f.setVisible(true); } public void actionPerformed(ActionEvent av) {          String text = ta.getText();        tac.setText(text); } public static void main (String args[]) {         TextAreaTest tt = new TextAreaTest();         tt.go(); } }

  15. AWT Component(3) – List import java.awt.*; import java.awt.event.*;    public class TestList implements ActionListener {    private Frame f;   private List list;    private Button b;     public void go() {          f = new Frame(“List"); list = new List(4, true); list.add(“AI");       list.add(“Lab");       list.add("인공지능연구실"); b = new Button("눌러~눌러~"); f.add(list, BorderLayout.CENTER);     f.add(b, BorderLayout.SOUTH);          b.addActionListener(this);      f.pack();         f.setVisible(true);     }    public void actionPerformed(ActionEvent ae) {          String [] selectedItems;         selectedItems = list.getSelectedItems(); for (int i = 0; i < selectedItems.length; i++) System.out.println(selectedItems[i]);     }     public static void main (String args[]) {          TestList ml = new TestList();         ml.go();    } }

  16. AWT Component(3) – Dialog Box import java.awt.*; import java.awt.event.*; public class TestDialog implements ActionListener { private Frame f; private FileDialog fd; private Dialog dl; private Button b1, b2, close; public void go() {          f = new Frame("FileDialog");        fd = new FileDialog(f, "파일 열기");// Dialog 만들기 dl = new Dialog(f, "dialog", true);   dl.add(new Label("여러분 안녕?"), BorderLayout.NORTH);          close = new Button("닫 기");   close.addActionListener(this);  dl.add(close, BorderLayout.CENTER); dl.pack(); b1 = new Button("FileDialog");  b2 = new Button("Dialog"); b1.addActionListener(this);          b2.addActionListener(this);         f.add(b1, BorderLayout.CENTER);         f.add(b2, BorderLayout.EAST);          f.pack();         f.setVisible(true); } public void actionPerformed( ActionEvent ae) {          String buttonPressed = ae.getActionCommand();         if (buttonPressed.equals("FileDialog")) {             fd.setVisible(true);       } else if (buttonPressed.equals("Dialog")) {            dl.setVisible(true);       } else if (buttonPressed.equals("닫 기")) { dl.setVisible(false);    } else {    fd.setVisible(false);    } }  public static void main (String args[]) {          TestDialog TestDialog = new TestDialog();     TestDialog.go(); } }

  17. AWT Component(3) – ScrollPane import java.awt.*;    public class TestScrollPane {     private Frame f;  private Image img; private Button b1, b2, b3, b4, b5, b6;   private Button b7, b8, b9, b10, b11, b12;     private Panel p; private ScrollPane sp; public void go() {          f = new Frame("Sample ScrollPane"); p = new Panel(); sp = new ScrollPane();  p.setLayout(new GridLayout(4,3)); b1 = new Button("사람 1"); b2 = new Button("사람 2"); b3 = new Button("사람 3"); b4 = new Button("사람 4");          b5 = new Button("사람 5"); b6 = new Button("사람 6"); b7 = new Button("사람 7"); b8 = new Button("사람 8");          b9 = new Button("사람 9"); b10 = new Button("사람 10");b11 = new Button("사람 11");b12 = new Button("사람 12");          p.add(b1); p.add(b2); p.add(b3); p.add(b4); p.add(b5); p.add(b6); p.add(b7); p.add(b8); p.add(b9); p.add(b10); p.add(b11); p.add(b12);    sp.add(p);     f.add(sp, BorderLayout.CENTER);     f.setSize(100,100);        f.setVisible(true);     }     public static void main (String args[]) {          TestScrollPane TestScrollPane = new TestScrollPane();          TestScrollPane.go();   } }

More Related