Mr.Vuttichai Vicheanchai (Onizuka)
[email protected]
java.awt • java.awt package (Abstract Window Toolkit) supports GUI operations. • Contains all of the classes for creating user interfaces and for painting graphics and images. • AWT Component • AWT Event We leave it for next lesson
AWT Components hierarchy
Frame • ต้อง import java.awt.*; • Constructor Frame() , Frame(String title) • Method setTitle(String title) setResizable(boolean resizable) setVisible(boolean resizable) setSize(int width,int height)
ถ้าอยากจะปิด Frame ต้องสร้าง method ต่อไปนี้ public boolean handleEvent(Event e){ if(e.id == Event.WINDOW_DESTROY){ System.exit(0); } return super.handleEvent(e); }
JFrame • ต้อง import java.awt.*; • Constructor JFrame() , JFrame(String title) • Method setTitle(String title) setResizable(boolean resizable) setVisible(boolean resizable) setSize(int width,int height) setDefaultCloseOperation(EXIT_ON_CLOSE)
Component • • • • •
Frame Button TextField TextArea Label
Layout • • • •
BorderLayout GridLayout FlowLayout null
Etc
• Color • Font • Cursor
Frame
Button
javax.swing Component - JFrame - JButton - JTextArea - JScrollPane - JCheckBox - JLabel
BorderLayout
GridLayout
FlowLayout
null Layout
JOptionPane JOptionPane.showMessageDialog(null,”Message”,”title message”,JOptionPane.??? , icon) int x = JOptionPane.show?Dialog(null,”Message”,”title message”,JOptionPane.??,JOptionPane.???, icon , “botton1”,”button2”, “default button”) ? = Input or Confirm or Option ?? = YES_NO_OPTION or OK_CANCEL_OPTION or CANCEL_OPTION or NO_OPTION or CLOSED_OPTION or YES_OPTION ??? = WARNING_MESSAGE or INFORMATION_MESSAGE or ERROR_MESSAGE or PLAIN_MESSAGE
Event Listener and Event Class •
KeyListener and KeyEvent (All Component) public void keyPressed(KeyEvent e) = กด public void keyReleased(KeyEvent e) = ปล่อย public void keyTyped(KeyEvent e) = ทั้งกดและปล่อย
1.1 KeyEvent Class => important constant and method KEY_FIRST
KEY_LAST
VK_BACK_SPACE VK_TAB
VK_CANCEL
VK_SHIFT
VK_CONTROL
VK_ENTER
VK_ALT
VK_PAUSE
VK_CAPS_LOCK
VK_ESCAPE
VK_PAGE_UP
VK_PAGE_DOWN VK_END
VK_HOME
VK_LEFT
VK_RIGHT
VK_DOWN
VK_0,VK_1,…,VK_9
VK_F1,…,VK_F24
getKeyCode()
getKeyChar()
getKeyText(int keyCode)
isActionKey()
isAltDown()
isControlDown()
isShiftDown()
int primitive return int
return boolean
return char
return String
•
MouseListener , MouseMotionListener and MouseEvent (All Component) 2.1 MouseListener public void mouseClicked(MouseEvent e) = คลิก public void mouseEntered(MouseEvent e) = นำาเม้าส์เข้าไป public void mouseExited(MouseEvent e) = นำาเม้าส์ออก public void mousePressed(MouseEvent e) = กด public void mouseReleased(MouseEvent e) = ปล่อยเม้าส์ขวา
2.2 MouseMotionListener public void mouseDragged(MouseEvent e) = คลิกลาก public void mouseMoved(MouseEvent e) = เคลื่อนที่
2.3 MouseEvent Class => important constant and method public static final int NOBUTTON public static final int BUTTON1 public static final int BUTTON2 public static final int BUTTON3 public int getX() public int getY() public Point getPoint() public int getClickCount() public int getButton() public boolean isPopupTrigger() public void translatePoint(int x,int y) public static String getMouseModifiersText(int modifiers)
3. ActionListener and ActionEvent (Use to Button component) JButton
JToggleButton JTextComponent JMenuItem JCheckBoxMenuItem JRadioButton JRadioButtonMenuItem JComboBox JFileChooser JCheckBox JScrollbar
3.1 ActionListener public void actionPerformed(ActionEvent e) = คลิก หรือ enter
3.2 ActionEvent Class => important constant and method public static final SHIFT_MASK public static final int CTRL_MASK public static final int META_MASK public static final int ALT_MASK public String getActionCommand() public int getModifiers() public Point getWhen()