GUILayout.java
上传用户:ljt780218
上传日期:2022-07-30
资源大小:110k
文件大小:10k
源码类别:

金融证券系统

开发平台:

Java

  1. // <html><head><title>ATM Simulation Implementation - GUI Layout</title></head><body><h2>ATM Simulation Implementation - GUI Layout</h2><pre>
  2. /*
  3.  * Example ATM simulation - file GUILayout.java
  4.  *
  5.  * This defines the layout of the graphical user interface for the ATM simulation.  
  6.  *
  7.  * Copyright (c) 1997 - Russell C. Bjork
  8.  *
  9.  */
  10. package atm.atmparts;
  11. import java.awt.*;
  12. //</pre><hr><h3>Class GUILayout</h3><pre>
  13. public class GUILayout
  14.   {
  15.   
  16.     //</pre><hr><pre>
  17.     public static void doLayout(Container container,
  18.                                 Component cardReader,
  19.                                 Component display,
  20.                                 Component keyboard,
  21.                                 Component cashDispenser,
  22.                                 Component envelopeAcceptor, 
  23.                                 Component receiptPrinter,
  24.                                 Component operatorPanel)
  25.       {    
  26.         container.hide();
  27.         
  28.         GridBagLayout layout = new GridBagLayout();
  29.         container.setLayout(layout);
  30.         
  31.         // Put cardReader in a Panel with GridLayout to ensure it gets space
  32.         // even when invisible
  33.         
  34.         Panel cardReaderPanel = new Panel();
  35.         cardReaderPanel.setLayout(new GridLayout(1,1));
  36.         cardReaderPanel.add(cardReader);
  37.         container.add(cardReaderPanel);
  38.         layout.setConstraints(cardReaderPanel, 
  39.                 makeConstraints(READER_ROW, READER_COL,
  40.                                 READER_WIDTH, READER_HEIGHT, 
  41.                                 READER_FILL));
  42.         cardReader.hide();
  43.         
  44.         container.add(display);
  45.         layout.setConstraints(display, 
  46.                 makeConstraints(DISPLAY_ROW, DISPLAY_COL,
  47.                                 DISPLAY_WIDTH, DISPLAY_HEIGHT,
  48.                                 DISPLAY_FILL));
  49.                                 
  50.         container.add(keyboard);
  51.         layout.setConstraints(keyboard,
  52.                 makeConstraints(KEYBOARD_ROW, KEYBOARD_COL,
  53.                                 KEYBOARD_WIDTH, KEYBOARD_HEIGHT,
  54.                                 KEYBOARD_FILL));
  55.                                 
  56.         container.add(cashDispenser);
  57.         layout.setConstraints(cashDispenser,
  58.                 makeConstraints(DISPENSER_ROW, DISPENSER_COL,
  59.                                 DISPENSER_WIDTH, DISPENSER_HEIGHT,
  60.                                 DISPENSER_FILL));
  61.                                 
  62.                                 
  63.         // Put envelopeAcceptor in a Panel with GridLayout to ensure it gets space
  64.         // even when invisible
  65.         
  66.         Panel envelopeAcceptorPanel = new Panel();
  67.         envelopeAcceptorPanel.setLayout(new GridLayout(1,1));
  68.         envelopeAcceptorPanel.add(envelopeAcceptor);
  69.         container.add(envelopeAcceptorPanel);
  70.         layout.setConstraints(envelopeAcceptorPanel,
  71.                 makeConstraints(ENVELOPE_ROW, ENVELOPE_COL, 
  72.                                 ENVELOPE_WIDTH, ENVELOPE_HEIGHT,
  73.                                 ENVELOPE_FILL));
  74.         envelopeAcceptor.hide();
  75.         
  76.         container.add(receiptPrinter);
  77.         layout.setConstraints(receiptPrinter,
  78.                 makeConstraints(PRINTER_ROW, PRINTER_COL,
  79.                                 PRINTER_WIDTH, PRINTER_HEIGHT,
  80.                                 PRINTER_FILL));
  81.                                 
  82.         container.add(operatorPanel);
  83.         layout.setConstraints(operatorPanel,
  84.                 makeConstraints(OPERATOR_ROW, OPERATOR_COL,
  85.                                 OPERATOR_WIDTH, OPERATOR_HEIGHT,
  86.                                 OPERATOR_FILL));                                
  87.                                 
  88.         // When we create a dialog box later, we may need to pass a frame
  89.         // to its constructor. (Standards-conforming implementations do not
  90.         // need this, but defective implementations (e.g. Unix) do.)  Our
  91.         // container is either a Frame, or we should be able to get to one
  92.         // from it.  We can also use the location of this frame to position
  93.         // the dialog appropriately.
  94.         
  95.         Container c = container;
  96.         while (c != null && ! (c instanceof Frame))
  97.             c = c.getParent();
  98.         if (c instanceof Frame)
  99.             _containingFrame = (Frame) c;
  100.         else
  101.             _containingFrame = null;
  102.       }
  103.   
  104.     //</pre><hr><pre>
  105.     // The GUI representing the ATM is laid out using a GridBagLayout.  
  106.     // The following constants determine the positioning of the various
  107.     // components within the grid bag.  Each component has a row and column
  108.     // coordinate for its upper left hand corner, plus a height in rows and
  109.     // a width in columns.  The following is the arrangement:
  110.     
  111.     // ---------------------------------------------------------------------
  112.     // |       DISPLAY                                    |     RECEIPT    |
  113.     // |                                                  |     PRINTER    |
  114.     // |                                                  |                |
  115.     // |                                                  |                |
  116.     // |                                                  |                |
  117.     // |                                                  |                |
  118.     // |                                                  |                |
  119.     // |                                                  |                |
  120.     // |-------------------------------------------------------------------|
  121.     // |  ENVELOPE      |    CASH        |    CARD        |   KEYBOARD     |
  122.     // |  ACCEPTOR      |    DISPENSER   |    READER      |                |
  123.     // |                |                |                |                |
  124.     // |                |                |                |                |
  125.     // |                |                |                |                |
  126.     // |                |                |                |                |
  127.     // ---------------------------------------------------------------------
  128.     // |  OPERATOR PANEL                                                   |
  129.     // |                                                                   |
  130.     // ---------------------------------------------------------------------
  131.     
  132.     // The following constants govern the size of various components, and
  133.     // need to be used by the component
  134.     
  135.     public static final int DISPLAYABLE_LINES =     9;
  136.     public static final int PRINTABLE_LINES =       8;
  137.     public static final int PRINTABLE_CHARS =      30;
  138.     
  139.     // The following are used only for doing the layout
  140.     private static final int DISPLAY_ROW =          0;
  141.     private static final int DISPLAY_COL =          0;
  142.     private static final int DISPLAY_WIDTH =        3;
  143.     private static final int DISPLAY_HEIGHT =       1;
  144.     private static final int DISPLAY_FILL =         GridBagConstraints.BOTH;
  145.     
  146.     private static final int PRINTER_ROW =          0;
  147.     private static final int PRINTER_COL =          DISPLAY_COL + DISPLAY_WIDTH;
  148.     private static final int PRINTER_WIDTH =        1;
  149.     private static final int PRINTER_HEIGHT =       1;
  150.     private static final int PRINTER_FILL =         GridBagConstraints.NONE;
  151.     
  152.     private static final int ENVELOPE_ROW =         DISPLAY_ROW + DISPLAY_HEIGHT;
  153.     private static final int ENVELOPE_COL =         0;
  154.     private static final int ENVELOPE_WIDTH =       1;
  155.     private static final int ENVELOPE_HEIGHT =      1;
  156.     private static final int ENVELOPE_FILL =        GridBagConstraints.NONE;
  157.         
  158.     private static final int DISPENSER_ROW =        ENVELOPE_ROW;
  159.     private static final int DISPENSER_COL =        ENVELOPE_COL + ENVELOPE_WIDTH;
  160.     private static final int DISPENSER_WIDTH =      1;
  161.     private static final int DISPENSER_HEIGHT =     1;
  162.     private static final int DISPENSER_FILL =       GridBagConstraints.NONE;
  163.     private static final int READER_ROW =           ENVELOPE_ROW;
  164.     private static final int READER_COL =           DISPENSER_COL + DISPENSER_WIDTH;
  165.     private static final int READER_WIDTH =         1;
  166.     private static final int READER_HEIGHT =        1;
  167.     private static final int READER_FILL =          GridBagConstraints.NONE;
  168.     
  169.     private static final int KEYBOARD_ROW =         ENVELOPE_ROW;
  170.     private static final int KEYBOARD_COL =         READER_COL + READER_WIDTH;
  171.     private static final int KEYBOARD_WIDTH =       1;
  172.     private static final int KEYBOARD_HEIGHT =      1;
  173.     private static final int KEYBOARD_FILL =        GridBagConstraints.NONE;
  174.     
  175.     private static final int OPERATOR_ROW =         ENVELOPE_ROW + ENVELOPE_HEIGHT;
  176.     private static final int OPERATOR_COL =         0;
  177.     private static final int OPERATOR_WIDTH =       GridBagConstraints.REMAINDER;
  178.     private static final int OPERATOR_HEIGHT =      1;
  179.     private static final int OPERATOR_FILL =        GridBagConstraints.BOTH;
  180.     
  181.     private static final int TOTAL_ROWS = 3;
  182.     private static final int TOTAL_COLS = 3;
  183.     //</pre><hr><pre>
  184.     // This method creates a GridBagConstraints object with specified constraints,
  185.     // and others defaulted.
  186.     
  187.     private static GridBagConstraints makeConstraints(
  188.                     int row, int col, int width, int height, int fill)
  189.       { GridBagConstraints g = new GridBagConstraints();
  190.         g.gridy = row;
  191.         g.gridx = col;
  192.         g.gridheight = height;
  193.         g.gridwidth = width;
  194.         g.fill = fill;
  195.         g.insets = new Insets(2,2,2,2);
  196.         return g;
  197.       }
  198.     // On some (defective) implementations of Java, we need to pass a Frame to
  199.     // the constructor for Dialog - the documentation says null is allowed here,
  200.     // but Unix implementations throw a null pointer exception if one tries.
  201.     // Fortunately, the container we are given when we do the layout is either
  202.     // a Frame, or we should be able to get to one by following parents.  The
  203.     // doLayout method tries to fill this in; it if can't, it sets it null.
  204.     
  205.     private static Frame _containingFrame;
  206.     public static Frame getContainingFrame()
  207.       { return _containingFrame; }
  208.       
  209.   }
  210. //</pre></body></html>