Java Software Solutions - Chapter 7: Object-Oriented Design

Now we can extend our discussion of the design of classes and objects

Chapter 7 focuses on:

software development activities

determining the classes and objects that are needed for a program

the relationships that can exist among classes

the static modifier

writing interfaces

the design of enumerated type classes

method design and method overloading

GUI design and layout managers

 

pptx158 trang | Chuyên mục: Java | Chia sẻ: dkS00TYs | Ngày: 08/06/2015 | Lượt xem: 1567 | Lượt tải: 0download
Tóm tắt nội dung Java Software Solutions - Chapter 7: Object-Oriented Design, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
blic class BorderPanel extends JPanel { //----------------------------------------------------------------- // Sets up this panel with a button in each area of a border // layout to show how it affects their position, shape, and size. //----------------------------------------------------------------- public BorderPanel() { setLayout (new BorderLayout()); setBackground (Color.green); continue Copyright © 2012 Pearson Education, Inc. continue JButton b1 = new JButton ("BUTTON 1"); JButton b2 = new JButton ("BUTTON 2"); JButton b3 = new JButton ("BUTTON 3"); JButton b4 = new JButton ("BUTTON 4"); JButton b5 = new JButton ("BUTTON 5"); add (b1, BorderLayout.CENTER); add (b2, BorderLayout.NORTH); add (b3, BorderLayout.SOUTH); add (b4, BorderLayout.EAST); add (b5, BorderLayout.WEST); } } Copyright © 2012 Pearson Education, Inc. continue JButton b1 = new JButton ("BUTTON 1"); JButton b2 = new JButton ("BUTTON 2"); JButton b3 = new JButton ("BUTTON 3"); JButton b4 = new JButton ("BUTTON 4"); JButton b5 = new JButton ("BUTTON 5"); add (b1, BorderLayout.CENTER); add (b2, BorderLayout.NORTH); add (b3, BorderLayout.SOUTH); add (b4, BorderLayout.EAST); add (b5, BorderLayout.WEST); } } Grid Layout A grid layout presents a container’s components in a rectangular grid of rows and columns One component is placed in each cell of the grid, and all cells have the same size Components fill the grid from left-to-right and top-to-bottom (by default) The size of each cell is determined by the overall size of the container See GridPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // GridPanel.java Authors: Lewis/Loftus // // Represents the panel in the LayoutDemo program that demonstrates // the grid layout manager. //******************************************************************** import java.awt.*; import javax.swing.*; public class GridPanel extends JPanel { //----------------------------------------------------------------- // Sets up this panel with some buttons to show how grid // layout affects their position, shape, and size. //----------------------------------------------------------------- public GridPanel() { setLayout (new GridLayout (2, 3)); setBackground (Color.green); continue Copyright © 2012 Pearson Education, Inc. continue JButton b1 = new JButton ("BUTTON 1"); JButton b2 = new JButton ("BUTTON 2"); JButton b3 = new JButton ("BUTTON 3"); JButton b4 = new JButton ("BUTTON 4"); JButton b5 = new JButton ("BUTTON 5"); add (b1); add (b2); add (b3); add (b4); add (b5); } } Copyright © 2012 Pearson Education, Inc. continue JButton b1 = new JButton ("BUTTON 1"); JButton b2 = new JButton ("BUTTON 2"); JButton b3 = new JButton ("BUTTON 3"); JButton b4 = new JButton ("BUTTON 4"); JButton b5 = new JButton ("BUTTON 5"); add (b1); add (b2); add (b3); add (b4); add (b5); } } Box Layout A box layout organizes components horizontally (in one row) or vertically (in one column) Components are placed top-to-bottom or left-to-right in the order in which they are added to the container By combining multiple containers using box layout, many different configurations can be created Multiple containers with box layouts are often preferred to one container that uses the more complicated gridbag layout manager Copyright © 2012 Pearson Education, Inc. Box Layout Invisible components can be added to a box layout container to take up space between components Rigid areas have a fixed size Glue specifies where excess space should go Invisible components are created using these methods of the Box class: 	createRigidArea(Dimension d) 	createHorizontalGlue() 	createVerticalGlue() See BoxPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // BoxPanel.java Authors: Lewis/Loftus // // Represents the panel in the LayoutDemo program that demonstrates // the box layout manager. //******************************************************************** import java.awt.*; import javax.swing.*; public class BoxPanel extends JPanel { //----------------------------------------------------------------- // Sets up this panel with some buttons to show how a vertical // box layout (and invisible components) affects their position. //----------------------------------------------------------------- public BoxPanel() { setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); setBackground (Color.green); continue Copyright © 2012 Pearson Education, Inc. continue JButton b1 = new JButton ("BUTTON 1"); JButton b2 = new JButton ("BUTTON 2"); JButton b3 = new JButton ("BUTTON 3"); JButton b4 = new JButton ("BUTTON 4"); JButton b5 = new JButton ("BUTTON 5"); add (b1); add (Box.createRigidArea (new Dimension (0, 10))); add (b2); add (Box.createVerticalGlue()); add (b3); add (b4); add (Box.createRigidArea (new Dimension (0, 20))); add (b5); } } Copyright © 2012 Pearson Education, Inc. continue JButton b1 = new JButton ("BUTTON 1"); JButton b2 = new JButton ("BUTTON 2"); JButton b3 = new JButton ("BUTTON 3"); JButton b4 = new JButton ("BUTTON 4"); JButton b5 = new JButton ("BUTTON 5"); add (b1); add (Box.createRigidArea (new Dimension (0, 10))); add (b2); add (Box.createVerticalGlue()); add (b3); add (b4); add (Box.createRigidArea (new Dimension (0, 20))); add (b5); } } Borders A border can be put around any Swing component to define how the edges of the component should be drawn Borders can be used effectively to group components visually The BorderFactory class contains several static methods for creating border objects A border is applied to a component using the setBorder method Copyright © 2012 Pearson Education, Inc. Borders An empty border buffers the space around the edge of a component otherwise has no visual effect A line border surrounds the component with a simple line the line's color and thickness can be specified An etched border creates the effect of an etched groove around a component uses colors for the highlight and shadow Copyright © 2012 Pearson Education, Inc. Borders A bevel border can be raised or lowered uses colors for the outer and inner highlights and shadows A titled border places a title on or around the border the title can be oriented in many ways A matte border specifies the sizes of the top, left, bottom, and right edges of the border separately uses either a solid color or an image Copyright © 2012 Pearson Education, Inc. Borders A compound border is a combination of two borders one or both of the borders can be a compound border See BorderDemo.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // BorderDemo.java Authors: Lewis/Loftus // // Demonstrates the use of various types of borders. //******************************************************************** import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class BorderDemo { //----------------------------------------------------------------- // Creates several bordered panels and displays them. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Border Demo"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout (new GridLayout (0, 2, 5, 10)); panel.setBorder (BorderFactory.createEmptyBorder (8, 8, 8, 8)); JPanel p1 = new JPanel(); p1.setBorder (BorderFactory.createLineBorder (Color.red, 3)); p1.add (new JLabel ("Line Border")); panel.add (p1); continue Copyright © 2012 Pearson Education, Inc. continue JPanel p2 = new JPanel(); p2.setBorder (BorderFactory.createEtchedBorder ()); p2.add (new JLabel ("Etched Border")); panel.add (p2); JPanel p3 = new JPanel(); p3.setBorder (BorderFactory.createRaisedBevelBorder ()); p3.add (new JLabel ("Raised Bevel Border")); panel.add (p3); JPanel p4 = new JPanel(); p4.setBorder (BorderFactory.createLoweredBevelBorder ()); p4.add (new JLabel ("Lowered Bevel Border")); panel.add (p4); JPanel p5 = new JPanel(); p5.setBorder (BorderFactory.createTitledBorder ("Title")); p5.add (new JLabel ("Titled Border")); panel.add (p5); JPanel p6 = new JPanel(); TitledBorder tb = BorderFactory.createTitledBorder ("Title"); tb.setTitleJustification (TitledBorder.RIGHT); p6.setBorder (tb); p6.add (new JLabel ("Titled Border (right)")); panel.add (p6); continue Copyright © 2012 Pearson Education, Inc. continue JPanel p7 = new JPanel(); Border b1 = BorderFactory.createLineBorder (Color.blue, 2); Border b2 = BorderFactory.createEtchedBorder (); p7.setBorder (BorderFactory.createCompoundBorder (b1, b2)); p7.add (new JLabel ("Compound Border")); panel.add (p7); JPanel p8 = new JPanel(); Border mb = BorderFactory.createMatteBorder (1, 5, 1, 1, Color.red); p8.setBorder (mb); p8.add (new JLabel ("Matte Border")); panel.add (p8); frame.getContentPane().add (panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. continue JPanel p7 = new JPanel(); Border b1 = BorderFactory.createLineBorder (Color.blue, 2); Border b2 = BorderFactory.createEtchedBorder (); p7.setBorder (BorderFactory.createCompoundBorder (b1, b2)); p7.add (new JLabel ("Compound Border")); panel.add (p7); JPanel p8 = new JPanel(); Border mb = BorderFactory.createMatteBorder (1, 5, 1, 1, Color.red); p8.setBorder (mb); p8.add (new JLabel ("Matte Border")); panel.add (p8); frame.getContentPane().add (panel); frame.pack(); frame.setVisible(true); } } Summary Chapter 7 has focused on: software development activities determining the classes and objects that are needed for a program the relationships that can exist among classes the static modifier writing interfaces the design of enumerated type classes method design and method overloading GUI design and layout managers Copyright © 2012 Pearson Education, Inc. 

File đính kèm:

  • pptxLESSON_07_Object-Oriented Design.pptx
Tài liệu liên quan