6
Chapter 15 JComponent and Event

Introduction to OOPtulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch16.pdf · 2012. 11. 9. · Introduction to OOP Author: Gim Created Date: 9/18/2011 9:43:58 PM

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to OOPtulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch16.pdf · 2012. 11. 9. · Introduction to OOP Author: Gim Created Date: 9/18/2011 9:43:58 PM

Chapter 15

JComponent and Event

Page 2: Introduction to OOPtulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch16.pdf · 2012. 11. 9. · Introduction to OOP Author: Gim Created Date: 9/18/2011 9:43:58 PM

Event

• Event ได้แบ่งเป็น 2 ชนิดคือ

• Low-Level Events ส าหรับ Windows, Mouse และ Keyboard

• Semantic Events ส าหรับ Component อื่นๆ ใน java.awt และ javax.swing

Page 3: Introduction to OOPtulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch16.pdf · 2012. 11. 9. · Introduction to OOP Author: Gim Created Date: 9/18/2011 9:43:58 PM

1. import java.awt.*;

2. import javax.swing.*;

3. import java.awt.event.*;

4. class MyFrame extends JFrame implements WindowListener{

5. public MyFrame(int width, int height) {

6. //can call getToolkit() methods directly from JFrame

7. Toolkit tk = getToolkit();

8. Dimension screen = tk.getScreenSize();

9. setBounds( (screen.width - width)/2, (screen.height - height)/2, width, height);

10. addWindowListener(this);

11. }

12. public void windowClosing(WindowEvent e) {

13. JOptionPane.showMessageDialog(null, "This windown is about to close");

14. dispose();

15. System.exit(0);

16. }

17. public void windowClosed(WindowEvent e) {}

18. public void windowOpened(WindowEvent e) {}

19. public void windowIconified(WindowEvent e) {}

20. public void windowDeiconified(WindowEvent e) {}

21. public void windowActivated(WindowEvent e) {}

22. public void windowDeactivated(WindowEvent e) {}

23. }

24. public class WinEvent {

25. public static void main(String[] args) {

26. MyFrame frame = new MyFrame(400, 200);

27. frame.setVisible(true);

28. }

29. }

Try WinEvent.java

Page 4: Introduction to OOPtulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch16.pdf · 2012. 11. 9. · Introduction to OOP Author: Gim Created Date: 9/18/2011 9:43:58 PM

Low-Level Events: Mouse Listener (MouseMove.java)

• MouseListener ใช้ส าหรับดกัจับ Event ในกรณีที่มีการสั่งงานโดยใช้เมาส์– public void mouseClicked(MouseEvent e)– public void mousePressed(MouseEvent e)– public void mouseReleased(MouseEvent e)– public void mouseEntered(MouseEvent e)– public void mouseExited(MouseEvent e)

• MouseMotionListener ใชส้ าหรับดักจับ Event ในกรณีที่มีการเคลื่อนหรือ Drag and Drop เมาส์– public void mouseMoved(MouseEvent e)– public void mouseDragged(MouseEvent e)

• MouseWheelListener ใชส้ าหรับดักจับ Event ในกรณีที่มีการใช้ Wheel ของเมาส์– mouseWheelMoved(MouseWheelEvent e)

Try TestDrag.java

Page 5: Introduction to OOPtulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch16.pdf · 2012. 11. 9. · Introduction to OOP Author: Gim Created Date: 9/18/2011 9:43:58 PM

Semantic Events

ชนิดของ Event ใช้กับ JComponent Method ที่ต้องสรา้งเมื่อมีการ Implement Interface

ActionEventเมื่อมีการคลิก

JButton, JToggleButton, JCheckBox,JMenuItem, JMenu,

JCheckBoxMenuItem,JRadioButtonMenuItem, JTextField, JPasswordField

void actionPerformed(ActionEvent e)

ItemEventเมื่อมกีารเลอืก

JButton, JToggleButton, JCheckBoxJMenuItem, JMenu,

JCheckBoxMenuItem,JRadioButtonMenuItem

void itemStateChanged(ItemEvent e)

AdjustmentEventเมื่อมกีารปรับค่า

JScrollbar void adjstmentValueChyanged( AdjustmentEvent e)

Page 6: Introduction to OOPtulip.bu.ac.th/~thirapon.w/gim/OOP_files/ch16.pdf · 2012. 11. 9. · Introduction to OOP Author: Gim Created Date: 9/18/2011 9:43:58 PM

Example of JComponent

• JButton• JTextArea• JInternalFrame and JFrileChooser

Try SetJButton.java

Try ShowJTextArea.java

Try ShowJFileChooser1.java

Try Calculator.java