200 likes | 356 Views
GUI programming in java. Overview. Window System Visualization Containers and Components Layout Manager User Interface Event Model. Basic of Windows Programming. Message Queue A loop Fetch message from the queue Action Paint Deliver message to proper object.
E N D
Overview • Window System • Visualization • Containers and Components • Layout Manager • User Interface • Event Model
Basic of Windows Programming • Message Queue • A loop • Fetch message from the queue • Action • Paint • Deliver message to proper object
Basic of Windows Programming User’s Action key event mouse event … while(1) { message = fetch(queue)) switch(message) { case KEY_PRESSED: … case KEY_RELEASED: … case PAINT: … } }
Containers and Components JFrame jf = new JFrame(“Programming Methodology”); JTabbedPane jt = new JTabbedPane(); jt.addTab(“1”, new Label(“Introduction to Compiler”)); jt.addTab(“2”, new Label(“Programming Methodology”)); jt.addTab(“3”, new Label(“Data Structure and Algorithm”)); jt.addTab(“4”, new Label(“Introduction to Compiler”)); jf.getContentPane().add(jt); jf.show(); JFrame ContentPane JTabbedPane Label
Containers and Components JFrame JFrame jf = new JFrame(“Programming Methodology”); JPanel jp = new JPanel(); jp.add(new Button(“Introduction to Compiler”)); jp.add(new Button(“Programming Methodology”)); jp.add(new Button(“Data Structure and Algorithm”)); jp.add(new Button(“Introduction to Compiler”)); jf.getContentPane().add(jp); jf.show(); ContentPane JPanel Button
Layout Manager • Determine position and size of components in a container. • GridLayout, GridBagLayout, FlowLayout, BorderLayout, BoxLayout…
Layout Manager Border layout Border layout + Box layout
User Interface • Interaction with user • Low level input • keyboard input • mouse input • High level input • click a button component • select between a radio buttons • select a menu item
User Interface • Event Source • Every Components you can see • Canvas, TextField, Button, Menu, ScrollBar… • Event Listener • Each event source has its listener • You register your own listener to event creator you have interest in.
invoke registered event listener class A implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(“action”); } } User Interface ActionEvent is created Component action
Look and Feel in Swing • AWT vs. Swing • Platform widget vs. Component UI • MVC • Model is logical representation • View is visual representation • Controller handles user input
Look and Feel in Swing • Pluggable look and feel try{ UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel"); } catch (java.lang.ClassNotFoundException e) { // Can't change look and feel }
References • http://developer.java.sun.com/developer/onlineTraining/new2java/divelog/part1/ • http://developer.java.sun.com/developer/onlineTraining/new2java/divelog/part2/ • http://developer.java.sun.com/developer/onlineTraining/new2java/divelog/part3/ • http://developer.java.sun.com/developer/onlineTraining/new2java/divelog/part4/ • http://developer.java.sun.com/developer/onlineTraining/GUI/Swing1/shortcourse.html • http://developer.java.sun.com/developer/onlineTraining/GUI/Swing2/shortcourse.html