Java GUI AWT to Swing AWT Abstract Windowing
Java GUI
AWT to Swing • AWT: Abstract Windowing Toolkit • import java. awt. * • Swing: new with Java 2 • • • import javax. swing. * Extends AWT Tons o’ new improved components Standard dialog boxes, tooltips, … Look-and-feel, skins Event listeners • API: • http: //java. sun. com/j 2 se/1. 3/docs/api/index. html
Swing Set Demo J 2 sdk/demo/jfc/Swing. Set 2 • Many predefined GUI components
GUI Component API • Java: GUI component = class • Properties • • Methods • • Events • JButton
Using a GUI Component 1. Create it • Instantiate object: b = new JButton(“press me”); 2. Configure it • • Properties: b. text = “press me”; Methods: b. set. Text(“press me”); [avoided in java] 3. Add it • panel. add(b); 4. Listen to it • Events: Listeners JButton
Anatomy of an Application GUI Internal structure JFrame JPanel containers JPanel JButton JLabel
Using a GUI Component 2 1. 2. 3. 4. 5. Create it Configure it Add children (if container) Add to parent (if not JFrame) Listen to it order important
Build from bottom up • Create: • • Frame Panel Components Listeners • Add: (bottom up) • listeners into components • components into panel • panel into frame Listener JLabel JButton JPanel JFrame
Code JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); p. add(b); f. set. Content. Pane(p); // add button to panel // add panel to frame f. show(); press me
Application Code import javax. swing. *; class hello { public static void main(String[] args){ JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p. add(b); f. set. Content. Pane(p); f. set. Visible(true); } } // add button to panel // add panel to frame press me
Layout Manager Heuristics null Flow. Layout none, programmer sets x, y, w, h Left to right, Top to bottom Border. Layout n w c s e Card. Layout One at a time Grid. Layout Grid. Bag. Layout JButton
Combinations JButton JText. Area
Combinations JButton JFrame n JPanel: Flow. Layout JPanel: Border. Layout c JText. Area
Code: null layout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); b. set. Bounds(new Rectangle(10, 100, 50)); p. set. Layout(null); // x, y layout p. add(b); f. set. Content. Pane(p); press me
Code: Flow. Layout JFrame f = JPanel p = Flow. Layout JButton b 1 JButton b 2 new JFrame(“title”); new JPanel( ); L = new Flow. Layout( ); = new JButton(“press me”); = new JButton(“then me”); p. set. Layout(L); p. add(b 1); p. add(b 2); f. set. Content. Pane(p); Set layout mgr before adding components press me then me
Applets • JApplet is like a JFrame • Already has a panel • Access panel with JApplet. get. Content. Pane( ) JApplet content. Pane import javax. swing. *; JButton class hello extends JApplet { public void init(){ JButton b = new JButton(“press me”); get. Content. Pane(). add(b); } }
Applet Methods • Called by browser: • • • init( ) - initialization start( ) - resume processing (e. g. animations) stop( ) - pause destroy( ) - cleanup paint( ) - redraw stuff (‘expose’ event)
Application + Applet import javax. swing. *; class hello. App { public static void main(String[] args){ // create Frame and put my main. Panel in it JFrame f = new JFrame(“title”); main. Panel p = new main. Panel(); f. set. Content. Pane(p); f. show(); } } class hello. Applet extends JApplet { public void init(){ // put my main. Panel in the Applet main. Panel p = new main. Panel(); get. Content. Pane(). add(p); } } // my main GUI is in here: class main. Panel extends JPanel { main. Panel(){ set. Layout(new Flow. Layout()); JButton b = new JButton(“press me”); add(b); } } Command line Browser JFrame JApplet or content. Pane JPanel JButton
Applet Security • • No read/write on client machine Can’t execute programs on client machine Communicate only with server “Java applet window” Warning
- Slides: 19