Md10 building java gu is

299 views
242 views

Published on

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
299
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Md10 building java gu is

  1. 1. Building Java GUI
  2. 2. Objectives * Describe the AWT package and its components. * Define the terms containers , components, and layout managers , and how they work together to build a graphical user interface (GUI) * Use layout managers * Use the FlowLayout, BorderLayout, GridLayout, and CardLayout managers to achieve a desired dynamic layout. * Add components to a container * Use the Frame and Panel containers appropriately
  3. 3. * Describe how complex layouts with nested containers work. * In a Java program, identify the following: Containers The associated layout managers The layout hierarchy of all components
  4. 4. Abstract Window Toolkit (AWT) Provides graphical user interface (GUI) components that are used in all Java applets and application. Contains classes that can be extended and their properties inherited; classes can also be abstract. Ensures that every GUI component that is displayed on the screen is a subclass of the abstract class Component or MenuComponent Has Container, which is an abstract subclass of Component and includes two subclasses: * Panel * Window
  5. 6. Containers * The two main types of containers are Window and Panel * A Window is a free floating window on the display * A Panel is a container of GUI components that must exist in the context of some other container, such as a window or applet
  6. 8. Building Graphical User Interfaces The position and size of a component in a container is determined by a layout manager. You can control the size or position of components by disabling the layout manager. You must then use setLocation(), setSize(),or setBounds() on components to locate them in the container.
  7. 9. Frames * Are a subclass of Window * Have title and resizing corners * Are initially invisible, use setVisible(true) to expose the frame * Have BorderLayout as the default layout manager * Use the setLayout method to change the default layout manager
  8. 10. <ul><li>FrameExample.java </li></ul><ul><ul><ul><ul><li>import java.awt.*; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>public class FrameExample { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>private Frame f; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public FrameExample(){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f = new Frame(&quot;Hello Out There!&quot;); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public void launchFrame() { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f.setSize(170,170); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f.setBackground(Color.blue); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f.setVisible(true); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public static void main(String args[]){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>FrameExample guiWindow = new FrameExample(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>guiWindow.launchFrame(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul>
  9. 11. FrameExample.java
  10. 12. <ul><ul><ul><ul><ul><li>Panels </li></ul></ul></ul></ul></ul><ul><li>* Provide a space for components. </li></ul><ul><li>* Allow subpanels to have their own layout manager. </li></ul>
  11. 13. <ul><li>FrameWithPanel.java </li></ul><ul><ul><ul><ul><ul><li>import java.awt.*; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public class FrameWithPanel { private Frame f; Panel pan; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public FrameWithPanel(String title){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f = new Frame(title); pan = new Panel(); } </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public void launchFrame(){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f.setSize(200,200); f.setBackground(Color.blue); f.setLayout(null); // Override default layout mgr pan.setSize(100,100); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>pan.setBackground(Color.yellow); f.add(pan); f.setVisible(true); } </li></ul></ul></ul></ul></ul>
  12. 14. <ul><ul><li>public static void main(String args[]){ </li></ul></ul><ul><ul><li>FrameWithPanel guiWindow = new FrameWithPanel(&quot;Frame with Panel&quot;); guiWindow.launchFrame(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  13. 15. FrameWithPanel.java
  14. 16. <ul><li>Container Layouts </li></ul><ul><ul><ul><ul><ul><li>• FlowLayout </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>• BorderLayout </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>• GridLayout </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>• CardLayout </li></ul></ul></ul></ul></ul>
  15. 17. Default Layout Manager
  16. 18. <ul><ul><ul><ul><ul><li>A Simple FlowLayout Example </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>import java.awt.*; public class LayoutExample { private Frame f; private Button b1; private Button b2; public LayoutExample() </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> f = new Frame(&quot;GUI example&quot;); b1 = new Button(&quot;Press Me&quot;); b2 = new Button(&quot;Don’t press Me&quot;); } public void launchFrame() { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> f.setLayout(new FlowLayout()); f.add(b1); f.add(b2); f.pack(); f.setVisible(true); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul>
  17. 19. public static void main(String args[]) { LayoutExample guiWindow = new LayoutExample(); guiWindow.launchFrame(); } }
  18. 20. FlowLayoutManager • Default layout for the Panel class • Components added from left to right • Default alignment is centered • Uses components’ preferred sizes • Uses the constructor to tune behavior
  19. 21. <ul><li>FlowExample.java </li></ul><ul><ul><ul><ul><ul><li>import java.awt.*; public class FlowExample { private Frame f; private Button button1; private Button button2; private Button button3; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public FlowExample(){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> f = new Frame(&quot;Flow Layout&quot;); button1 = new Button(&quot;Ok&quot;); button2 = new Button(&quot;Open&quot;); button3 = new Button(&quot;Close&quot;); } public void launchFrame(){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> f.setLayout(new FlowLayout()); f.add(button1); f.add(button2); </li></ul></ul></ul></ul></ul>
  20. 22. <ul><ul><ul><ul><ul><li>f.add(button3); f.setSize(100,100); f.setVisible(true); } </li></ul></ul></ul></ul></ul><ul><ul><li>public static void main(String args[]){ </li></ul></ul><ul><ul><li> FlowExample guiWindow = new FlowExample(); guiWindow.launchFrame(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  21. 23. FlowExample.java
  22. 24. BorderLayoutManager Default layout for the Frame class Components added to specific regions The resizing behavior: North, South, and Center regions adjust horizontally East, West, and Center regions adjust vertically
  23. 25. <ul><li>Example </li></ul><ul><li>BorderExample.java </li></ul><ul><ul><ul><ul><ul><li>import java.awt.*; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public class BorderExample { private Frame f; private Button bn, bs, bw, be, bc; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public BorderExample(){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> f = new Frame(&quot;Border Layout&quot;); bn = new Button(&quot;B1&quot;); bs = new Button(&quot;B2&quot;); bw = new Button(&quot;B3&quot;); be = new Button(&quot;B4&quot;); bc = new Button(&quot;B5&quot;); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} public void launchFrame(){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f.add(bn, BorderLayout.NORTH); f.add(bs, BorderLayout.SOUTH); f.add(bw, BorderLayout.WEST); f.add(be, BorderLayout.EAST); f.add(bc, BorderLayout.CENTER); </li></ul></ul></ul></ul></ul>
  24. 26. <ul><ul><ul><ul><ul><li>f.setSize(200,200); f.setVisible(true); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public static void main(String args[]){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>BorderExample guiWindow2 = new BorderExample(); guiWindow2.launchFrame(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul>
  25. 27. <ul><ul><ul><ul><ul><li>BorderExample.java </li></ul></ul></ul></ul></ul>
  26. 28. GridLayoutManager • Components are added left to right, top to bottom • All regions are equally sized • The constructor specifies the rows and columns
  27. 29. <ul><li>Example GridExample.java </li></ul><ul><ul><ul><ul><ul><li>import java.awt.*; public class GridExample { private Frame f; private Button b1, b2, b3, b4, b5, b6; public GridExample() { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f = new Frame(&quot;Grid Example&quot;); b1 = new Button(&quot;1&quot;); b2 = new Button(&quot;2&quot;); b3 = new Button(&quot;3&quot;); b4 = new Button(&quot;4&quot;); b5 = new Button(&quot;5&quot;); b6 = new Button(&quot;6&quot;); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul>
  28. 30. <ul><ul><ul><ul><ul><li>public void launchFrame(){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f.setLayout (new GridLayout(3,2)); f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); f.add(b6); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f.pack(); f.setVisible(true); } </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public static void main(String args[]){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>GridExample grid = new GridExample(); grid.launchFrame(); } </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul>
  29. 31. <ul><ul><ul><ul><ul><li>GridEx.java </li></ul></ul></ul></ul></ul>
  30. 32. Card Layout The CardLayout class is unique among the other layout managers in that it stores several different layouts. This can be useful for user interfaces with optional components that can be dynamically enabled and disabled upon user input. The cards are typically held in an object of type Panel . This panel must have CardLayout selected as its layout manager.
  31. 33. <ul><ul><ul><ul><ul><li>CardLayoutManager </li></ul></ul></ul></ul></ul>
  32. 34. <ul><li>CardExample.java </li></ul><ul><ul><ul><ul><ul><li>import java.awt.*; import java.awt.event.*; public class CardExample implements MouseListener { private Panel p1, p2, p3, p4, p5; private Label lb1, lb2, lb3, lb4, lb5; // Declare a CardLayout object to call its methods. private CardLayout myCard; private Frame f; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public CardExample(){ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>f = new Frame (&quot;Card Test&quot;); myCard = new CardLayout(); p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); p4 = new Panel(); p5 = new Panel(); </li></ul></ul></ul></ul></ul>
  33. 35. lb1 = new Label(&quot;This is the first Panel&quot;); lb2 = new Label(&quot;This is the second Panel&quot;); lb3 = new Label(&quot;This is the third Panel&quot;); lb4 = new Label(&quot;This is the fourth Panel&quot;); lb5 = new Label(&quot;This is the fifth Panel&quot;); } public void launchFrame() { f.setLayout(myCard); // change the color of each panel, so they are // easily distinguishable p1.setBackground(Color.yellow); p1.add(lb1); p2.setBackground(Color.green); p2.add(lb2); p3.setBackground(Color.magenta); p3.add(lb3); p4.setBackground(Color.white); p4.add(lb4); p5.setBackground(Color.cyan); p5.add(lb5);
  34. 36. // Set up the event handling here. p1.addMouseListener(this); p2.addMouseListener(this); p3.addMouseListener(this); p4.addMouseListener(this); p5.addMouseListener(this); // Add each panel to my CardLayout f.add(p1, &quot;First&quot;); f.add(p2, &quot;Second&quot;); f.add(p3, &quot;Third&quot;); f.add(p4, &quot;Fourth&quot;); f.add(p5, &quot;Fifth&quot;); // Display the first panel. myCard.show(f, &quot;First&quot;); f.setSize(200,200); f.setVisible(true) }
  35. 37. public void mousePressed(MouseEvent e) { myCard.next(f); } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public static void main (String args[]) { CardExample ct = new CardExample(); ct.launchFrame(); } }

×