1 / 21

AWT & Event

AWT & Event. 김영원 ywkim@cs.chonbuk.ac.kr. AWT 시작하기. import java.awt.*; public class TestButton extends Frame{ /*Button b; TestButton(){ b= new Button("OK"); add(b); }*/ public static void main(String[] args){ Frame f = new TestButton(); //f.pack();

remedy
Download Presentation

AWT & Event

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. AWT & Event 김영원ywkim@cs.chonbuk.ac.kr

  2. AWT 시작하기 import java.awt.*; public class TestButton extends Frame{ /*Button b; TestButton(){ b= new Button("OK"); add(b); }*/ public static void main(String[] args){ Frame f = new TestButton(); //f.pack(); //f.setSize(500,500); f.setVisible(true); } } 전북대학교 컴퓨터 비젼실

  3. 컴포넌트간의 위치조정 • java.awt.BorderLayout • java.awt.FlowLayout • java.awt.GridLayout • java.awt.CardLayout • java.awt.GridBagLayout • LayoutManager 없이 배치하기 전북대학교 컴퓨터 비젼실

  4. BorderLayout import java.awt.*; public class BorderLayoutTest extends Frame{ public BorderLayoutTest() { add(new Button("Center"), BorderLayout.CENTER); add(new Button("East"), BorderLayout.EAST); add(new Button("South"), "South"); add(new Button("West"), "West"); add(new Button("north"), "North"); } public static void main(String[] args){ Frame f = new BorderLayoutTest(); f.pack(); f.setVisible(true); } } 전북대학교 컴퓨터 비젼실

  5. FlowLayout import java.awt.*; public class FlowLayoutTest extends Frame{ public FlowLayoutTest() { FlowLayout flow = new FlowLayout(); setLayout(flow); add(new Button(“b1")); add(new Button(“b2")); add(new Button(“b3")); } public static void main(String[] args){ Frame f = new FlowLayoutTest(); f.pack(); f.setVisible(true); } } // GridLayout grid = new GridLayout(3, 2); 전북대학교 컴퓨터 비젼실

  6. import java.awt.*; publicclass MyLabel extends Frame{ public MyLabel(){ setLayout(new GridLayout(3,1)); Label left = new Label("왼쪽 정렬"); Label center = new Label("가운데 정렬",Label.CENTER); Label right = new Label("오른쪽 정렬",Label.RIGHT); add(left); add(center); add(right); } publicstaticvoid main(String[] args){ MyLabel ML = new MyLabel(); ML.setSize(500,500); ML.setVisible(true); } } 전북대학교 컴퓨터 비젼실

  7. LayoutManager 없이 배치하기 import java.awt.*; public class NoLayoutTest extends Frame{ public NoLayoutTest() { setLayout(null); TextField tf = new TextField(); tf.setBounds(10, 60, 80, 30); add(tf); Button b = new Button(“Button”)); b.setBounds(50, 25, 40, 40); } public static void main(String[] args){ Frame f = new NOLayoutTest(); f.setBounds(100, 50, 100, 100); f.setVisible(true); } } 전북대학교 컴퓨터 비젼실

  8. import java.awt.*; publicclass TestScrollPane extends Frame{ public TestScrollPane(){ setLayout(new BorderLayout()); ScrollPane pane = new ScrollPane(); add("Center",pane); MyCanvas can = new MyCanvas(); can.setSize(500,500); pane.add(can); } publicstaticvoid main(String[] args){TestScrollPane sp = new TestScrollPane(); sp.setSize(200,200); sp.setVisible(true); } } class MyCanvas extends Canvas{ publicvoid paint(Graphics g){ g.setColor(Color.red); g.fillOval(30,30,100,100); } } 전북대학교 컴퓨터 비젼실

  9. // 연습해보기 1 public TestCheckbox(){ Panel p1 = new Panel(); p1.setLayout(new GridLayout(3,1)); p1.add(new Checkbox("first")); p1.add(new Checkbox("second")); p1.add(new Checkbox("third")); add(p1); Panel p2 = new Panel(); pe.setLayout(new GridLayout(3,1)); CheckboxGroup cbg = new CheckboxGroup(); p2.add(new Checkbox("one",cbg, true)); p2.add(new Checkbox("two",cbg, false)); p2.add(new Checkbox("three",cbg, false)); add(p2); } 전북대학교 컴퓨터 비젼실

  10. // 연습해보기 2 public TestScrollbar(){ Scrollbar sb1,sb2; setLayout(new BorderLayout()); sb1 = new Scrollbar(Scrollbar.VERTICAL,0,30,0,60); sb2 = new Scrollbar(Scroolbar.HORIZONTAL); add(sb1,"East"); add(sb2."South"); } 전북대학교 컴퓨터 비젼실

  11. Event 란? • Event • GUI에 대해서 발생하는 Key 입력 • Mouse Click과 같은 동작 전북대학교 컴퓨터 비젼실

  12. 리스너 인터페이스 전북대학교 컴퓨터 비젼실

  13. 전북대학교 컴퓨터 비젼실

  14. Event Handler class를 따로 구현하는 방법 import java.awt.*; import java.awt.event.*; publicclass EventOther extends Frame{ public EventOther(){ Button b = new Button("Test"); add("Center", b); b.addActionListener(new MyHandler()); } publicstaticvoid main(String[] args) { EventOther eo = new EventOther (); eo.setSize(100, 100); eo.setVisible(true); } } class MyHandler implements ActionListener { publicvoid actionPerformed(ActionEvent ev) { System.out.println(ev.getActionCommand()); } } 전북대학교 컴퓨터 비젼실

  15. 자기 자신 Event Handler가 되는 경우 import java.awt.*; import java.awt.event.*; publicclass EventMe extends Frame implements ActionListener{ Button b; EventMe(){ Button b = new Button("EventMeButton"); add("Center", b); b.addActionListener(this); } publicstaticvoid main(String[] args) { EventMe em = new EventMe(); em.setSize(200, 100); em.setVisible(true); } publicvoid actionPerformed(ActionEvent ev) { System.out.println(ev.getActionCommand()); } } 전북대학교 컴퓨터 비젼실

  16. EventHandler Class를 inner class로 작성하는 경우 import java.awt.*; import java.awt.event.*; publicclass EventInner extends Frame{ EventInner(){ Button b = new Button("EventInnerButton"); add("Center", b); b.addActionListener(new EventInnerListener()); } publicstaticvoid main(String[] args) { EventInner ea = new EventInner(); ea.setSize(200, 100); ea.setVisible(true); } class EventInnerListener implements ActionListener{ publicvoid actionPerformed(ActionEvent ev) { System.out.println(ev.getActionCommand()); } } } 전북대학교 컴퓨터 비젼실

  17. EventHandler Class를 Anonymous class로 작성하는 경우 import java.awt.*; import java.awt.event.*; publicclass EventAnony extends Frame { public EventAnony(){ Button b = new Button("Test"); add("Center", b); b.addActionListener(new ActionListener() { publicvoid actionPerformed(ActionEvent ev){ System.out.println(ev.getActionCommand()); } } ); } publicstaticvoid main(String[] args) { EventAnony ea = new EventAnony(); ea.setSize(100, 100); ea.setVisible(true); } } 전북대학교 컴퓨터 비젼실

  18. import java.awt.*; import java.awt.event.*; publicclass TwoListen extends Frame implements MouseMotionListener,MouseListener{ private TextField tf; publicstaticvoid main(String args[]) { TwoListen two = new TwoListen(); two.setSize(300, 200); two.setVisible(true); } public TwoListen() { add(new Label("Click and drag the mouse"), "North"); tf = new TextField(30); add(tf, "South"); addMouseMotionListener(this); addMouseListener(this); } publicvoid mouseDragged(MouseEvent e) { String s = "Mouse dragging: X = " + e.getX() +"Y = " + e.getY(); tf.setText(s); } publicvoid mouseMoved(MouseEvent e){} publicvoid mouseClicked(MouseEvent e){} publicvoid mouseEntered(MouseEvent e){ String s = "The mouse entered"; tf.setText(s); } publicvoid mouseExited(MouseEvent e) { String s = "The mouse has left the building"; tf.setText(s); } publicvoid mousePressed(MouseEvent e){} publicvoid mouseReleased(MouseEvent e){} } 전북대학교 컴퓨터 비젼실

  19. Adapters로 구현하는 방법 publicclass EventListenrTest extends Frame { EventListenrTest (){ b = new Button("Test"); b.addMouseListener(new MyHandler()); } publicstaticvoid main(String[] args) { EventListenerTest elt = new EventListenerTest(); } } class MyHandler implements MouseListener { publicvoid mouseClicked(MouseEvent ev) { System.out.println("TestButton is pressed "); } publicvoid mouseEntered(MouseEvent ev) { } publicvoid mouseExited(MouseEvent ev) { } publicvoid mousePressed(MouseEvent ev) { } publicvoid mouseReleased(MouseEvent ev) { } } 전북대학교 컴퓨터 비젼실

  20. 리스너 & 어댑터 클래스 전북대학교 컴퓨터 비젼실

  21. Adapter class로 Handler class를 만들어 사용하는 방법 import java.awt.*; import java.awt.event.*; publicclass AdapterOther extends Frame { public AdapterOther(){ Button b = new Button("Test"); add(b, "South"); b.addMouseListener(new MyHandler()); } publicstaticvoid main(String[] args) { AdapterOther ao = new AdapterOther(); ao.setSize(100, 100); ao.setVisible(true); } } class MyHandler extends MouseAdapter{ publicvoid mouseClicked(MouseEvent ev) { System.out.println("TestButton is pressed"); } } 전북대학교 컴퓨터 비젼실

More Related