Sei sulla pagina 1di 12
UNIT4 [35 Graphical User nerface 6) 4.3 Java Layout Managers Objectives 1, Introduce layout manager. 2, Describe and differentiate PLowbayous, Bordextayour and Gridiayout layout manager. Introduction In the previous section, you learnt how to ereate GUIs. ‘Two common GUI ‘components — button (sutton) and label (suabe1) — were introduced as wel, If you resize che window, the embedded button or label will be resized to occupy the entire content However, a window with either a button ora label isnot that useful, and we usually reed several GUI components to be embedded in a single GUI. In the sample programs we discussed in the previous section, the method aa) of the content pane (of type container) is called to add a GUI component to the top-level container. ‘Would two or more GUI components be shown on the GUL if they are added to the content pane by calling the acid () method one by one? To answer the question, a class Pranetfithliul:isvetons? is written in Figure 4.37. import jave.aut.*; // Resolve class Container import javax.swing.*; // Resolve class JFrame and JButton J) datination of class FrameWithwultiButtons) public class FraneWithMultiButtons! extends JFrame | // aetribute private gButton buttonl; // The first button private gButton button2; // The second button 1) Constructor public FranelichMultiputtons!() [ // Call super class constructor with a title super (“Frane With Multiple Suttons”): (1 create a qButton buttonl © new JButton ("Button One"); button? = new JButton ("Button Two"); // Get the content pane Container contentPane = getContentPane (): // Add the JButton objects to the content pane contentPane. add (button) ; contentPane.add(button2) ; 11 Set. the application to exit when the close button is clicked setDefaultCloseoperation (EXIT_ON_CLOSE) + 36) WAWASAN OPEN UNIVERSITY TEC102 Computing I packi) // Reorganise the enbedded components Figure 4.37 FrameWithMultiButtons1 java ‘A driver program, the TestPraneWitntileiputtons! class, is written to test the FrameWithouLtiButtonsi class. (Refer to the CD-ROM for the definition of the class TestFrameWithtultiBuetons1.) Compile the classes and execute the TestFraneWichtwiesauttonst class. The following GUT is shown on the Figure 4,38 The Franelsi thmwultisuttons? object shown on the screen ‘The GUI shown in Figure 4.38 illustrates chat only the last gut con object added. co the content pane is shown on the screen, ‘The above illustrates two characteristics of a GUI developed with Java and Swing: 1. The position and size of each GUI component embedded in a GUI are decermined and updated automatically at runtime 2, Some rules govern the placing of GUT components on a GUL ‘These ewo characteristics enable 2 GUI buile with Java and Swing to be eross- platform. The reason is that each container object has a layout manager object to determine the positions and sizes ofall embedded GUI components. What are layout managers? You learnt that a GUI component has to be added (0 a top-level container so that i can be shown on the sereen. More exactly, it can bea content pane of a 3Frame object. As we have mentioned, each JFrane object has a content pane object ot Container object. To arrange the GUI components added t0 a Container object, cach container has a layout manager object for arranging the GUI components added to the Container object. The type of layout manager is java-awt Layouttianager (or simply Layouttanager). Whenever a Container object is created, such as the content pane of a Jezame, a default layout manager is created as well. UNIT4 [37 ‘Grapica sertnertae(U) The relationships among the Container, tayoutManager and GUI component objects are visualised in Figure 4.39, container ayoutManager 1GUE Component PGUI Component Figure 4.39 The relationships among Container, LayoutManager and GUI component objects In Figure 4.39, the GUI component objects can be a oButton object, a stabe object or even other container objects. The arrows in the figure indicate that the Container object maintains the references of all embedded GUI components. When a GUI component is added to the Container object, the Container will pass the reference of the GUI component to the tayouttianager object it possesses. Therefore, the scenario of the objects is more exactly like what Figure 4.40 shows. container TayoutManager Sy GUE Component (GUI Component Figure 4.40 The LayoutManager object of a container object has the references to the GUI component objects added to the container objects (The arrows in Figure 4.40 are set as dimmed and dashed to highlight the arrows pointing from the LayoutMianager object to the GUI component objects.) The tayouttanager object of the Container object determines the sizes and positions of the GUI components to be shown in the Container on the screen. ‘Therefore, itis usual practice to seta proper layout manager object to a Container object before adding any GUI component to it

Potrebbero piacerti anche

  • WUC103 Unit2-2
    WUC103 Unit2-2
    Documento10 pagine
    WUC103 Unit2-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-3n4
    TCC102 Unit5-3n4
    Documento14 pagine
    TCC102 Unit5-3n4
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-4
    WUC103 Unit2-4
    Documento10 pagine
    WUC103 Unit2-4
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-3
    WUC103 Unit2-3
    Documento16 pagine
    WUC103 Unit2-3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-1
    TCC102 Unit5-1
    Documento22 pagine
    TCC102 Unit5-1
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-2
    WLA105 Unit2-2
    Documento6 pagine
    WLA105 Unit2-2
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit5-3
    WLA105 Unit5-3
    Documento18 pagine
    WLA105 Unit5-3
    api-26781128
    Nessuna valutazione finora
  • WUC103 Unit2-1
    WUC103 Unit2-1
    Documento24 pagine
    WUC103 Unit2-1
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit5-1-2
    WLA105 Unit5-1-2
    Documento22 pagine
    WLA105 Unit5-1-2
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-1
    WLA105 Unit2-1
    Documento20 pagine
    WLA105 Unit2-1
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit4-3
    WLA105 Unit4-3
    Documento18 pagine
    WLA105 Unit4-3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-5
    TCC102 Unit4-5
    Documento22 pagine
    TCC102 Unit4-5
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit4-1-2
    WLA105 Unit4-1-2
    Documento24 pagine
    WLA105 Unit4-1-2
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-3
    WLA105 Unit2-3
    Documento10 pagine
    WLA105 Unit2-3
    api-26781128
    Nessuna valutazione finora
  • WLA105 Unit2-4
    WLA105 Unit2-4
    Documento24 pagine
    WLA105 Unit2-4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-5
    TCC102 Unit5-5
    Documento12 pagine
    TCC102 Unit5-5
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 Summary
    TCC102 Unit3 Summary
    Documento16 pagine
    TCC102 Unit3 Summary
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-4
    TCC102 Unit4-4
    Documento14 pagine
    TCC102 Unit4-4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-1
    TCC102 Unit4-1
    Documento14 pagine
    TCC102 Unit4-1
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 4
    TCC102 Unit3 4
    Documento14 pagine
    TCC102 Unit3 4
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit5-2
    TCC102 Unit5-2
    Documento20 pagine
    TCC102 Unit5-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit4-2
    TCC102 Unit4-2
    Documento26 pagine
    TCC102 Unit4-2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 3
    TCC102 Unit3 3
    Documento14 pagine
    TCC102 Unit3 3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 2
    TCC102 Unit3 2
    Documento20 pagine
    TCC102 Unit3 2
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit3 1
    TCC102 Unit3 1
    Documento14 pagine
    TCC102 Unit3 1
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 Summary
    TCC102 Unit2 Summary
    Documento25 pagine
    TCC102 Unit2 Summary
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 5
    TCC102 Unit2 5
    Documento13 pagine
    TCC102 Unit2 5
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 3
    TCC102 Unit2 3
    Documento12 pagine
    TCC102 Unit2 3
    api-26781128
    Nessuna valutazione finora
  • TCC102 Unit2 4
    TCC102 Unit2 4
    Documento24 pagine
    TCC102 Unit2 4
    api-26781128
    Nessuna valutazione finora