Download presentation
Presentation is loading. Please wait.
Published byMarlene Ford Modified over 9 years ago
1
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes this and super r Layout r Reading: m Horstmann, Chapter 10 m Core Java (Volume 1), Chapter 9
2
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 2 Inner classes r Java supports inner classes, i.e., classes defined inside other classes. r Inner classes are often used to define listeners for events. public class ActionPanel3 extends JPanel { int darkness = 128; private class DarkenListener implements ActionListener { public void actionPerformed(ActionEvent e) { darkness = darkness-10; repaint(); } }.... }
3
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 3 Inner classes (cont.) The inner class can access the instance variables of the outer class (e.g., darkness ) as well as the methods (e.g., repaint ). r An instance of the inner class can be supplied as the listener for events: public class ActionPanel3 extends JPanel {.... JButton darken = new JButton(“Darken”); public ActionPanel3() { add(darken); darken.addActionListener( new DarkenListener()); }
4
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 4 Why use inner classes? r We can handle each event separately in a self-contained method, e.g., m DarkenListener.actionPerformed m LightenListener.actionPerformed r Leads to better structure when a class has to implement listeners for a large number of events.
5
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 5 Keyword: this Any object can refer to itself using the keyword this. this.x this.setX(2.0) button.addActionListener(this) r Note that a superclass can call a subclass method (due to overriding).
6
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 6 Keyword: super The keyword super refers to the superclass instance implicit inside the current object. public class MyPanel extends JPanel() { public paintComponent(Graphics g) { super.paintComponent(g); g.fillRect(x1, y1, xSize, ySize); }..... }
7
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 7 Subclass constructors r A constructor of a subclass always has an implicit call to the superclass’s constructor at the beginning: public class MyPanel extends JPanel { public MyPanel() { // implicitly super(); setPreferredSize(300, 400); } r However, if there are arguments to the superclass constructor, need to call it explicitly.
8
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 8 Layout r Java GUI library is designed to be flexible. r Create interfaces for multiple platforms, screen sizes, window sizes, font sizes, international languages etc. r This requires a “layout manager” for each GUI panel. r Comes into picture whenever the panel needs to be laid out. r See Java tutorial for quick intro http://java.sun.com/docs/books/tutorial/uiswing/mini/layout.html http://java.sun.com/docs/books/tutorial/uiswing/mini/layout.html
9
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 9 FlowLayout r The default layout manager for a panel. r Places components in top-to-bottom, left-to- right order, like a word processor. r Components are kept at their preferred sizes as far as possible. (No stretching or shrinking). r Often components are centered horizontally. r See ActionPanel2 in code examples.
10
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 10 BorderLayout r Default layout for JFrames. For other containers, set it: container.setLayout(new BorderLayout()); r A BorderLayout container contains exactly 5 components: north, south, east, west and center. container.add( component, BorderLayout.NORTH); r Of course, any of these components can be a panel that groups together smaller components. r The 5 components stretch to fill all available space.
11
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 11 BoxLayout r A box is an array of components laid out horizontally or vertically. r Use it via the Box container: Box b = Box.createVerticalBox(); r Components in a box can be aligned: left, right or center (center by default). (Similarly: top, bottom or center for horizontal boxes.) r See ActionPanelCanvas3 in code examples.
12
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 12 GridBagLayout r The most flexible layout manager in Swing. r Imagine the panel arranged as a grid of invisible “cells.” r A component can occupy any rectangular area of cells. r Within its rectangular area, a component can either stretch or not, aligned or not, padded or not. (Lots of options!)
13
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 13 GridBagConstraints To specify all these options, Swing provides another class called GridBagConstraints. panel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; // position c.gridwidth = 1; c.gridheight = 3; // size c.weightx = 100; c.weighty = 100; // stretch JList style = new JList(); panel.add(style, c);
14
2004-02-03 MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 14 Programming GridBags r Don’t use the GridBagLayout class directly. r Define subclasses of JPanel with appropriate layout built-in: public class GridBagPanel extends Panel { GridBagConstraints gbc = new GridBagConstraints(); public GridBagPanel() { setLayout(new GridBagLayout());.... set default constraints } public add(Component c, int x1, int y1, int x2, int y2) {.... }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.