⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch14.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<PRE><FONT COLOR="#0066FF"><TT> 1: // AccountUpdateListener.java</TT><TT> 2: </TT><TT> 3: import idlGlobal.Account;</TT><TT> 4: </TT><TT> 5: public interface AccountUpdateListener {</TT><TT> 6: </TT><TT> 7:     /**</TT><TT> 8:      * Update the given Account with the new balance.</TT><TT> 9:      */</TT><TT>10:     public void update(Account account, float balance);11: }</TT></FONT></PRE><H4><FONT COLOR="#000077">A Word About Java Development Tools</FONT></H4><P>You're almost ready to begin work on the <TT>BankApplet</TT> itself. If you alreadytook a peek at <TT>BankApplet.java</TT> (see Listing 14.3), you saw that the fileis quite sizable--much larger than anything that has appeared in this book. Don'tbe alarmed. Much of the code was not written by hand but was generated by a developmenttool (a development tool, or a portion thereof, for defining GUI interfaces is oftencalled a <I>GUI builder</I>). Symantec's Visual Caf&#233; 2.0 (available at <TT>http://cafe.symantec.com/</TT>)was used to develop this particular applet, but any GUI builder for Java--such asSun's Java Workshop (<TT>http://www.sun.com/</TT>), Microsoft's Visual J++ (<TT>http://www.microsoft.com/</TT>),Borland's JBuilder (<TT>http://www.borland.com/</TT>), IBM's Visual Age for Java(<TT>http://www.software.ibm.com/</TT>), or many others--can be used to perform theGUI design portion of this task. Each of these products generates different code,depending on the GUI components chosen, but produces a similar end result.</P><P>Although you can certainly write all the user interface code by hand, using aGUI builder makes your life a lot easier for all but the most trivial user interfaces.Describing even one of these tools is beyond the scope of this book, so it's hopedthat you already have some experience with these. Many GUI builders not only enableyou to place user interface components on forms (also called screens or dialogs)but also to define interactions between components without writing any code. Themore work the GUI builder does, the less code you have to write by hand, and youcan always take code the GUI builder generates and modify it by hand to get the exactresults you want.<H4><FONT COLOR="#000077">BankApplet</FONT></H4><P><TT>BankApplet.java</TT>, including code generated by Visual Caf&#233; and codewritten by hand, appears in Listing 14.3. Again, most of this code (almost the entire<TT>init()</TT> method, for instance) was generated by Visual Caf&#233;. In a fewmoments, you'll examine more closely the portions written by hand. You will see thatthe applet's behavior is similar to that of the client applications implemented inprevious chapters. The structure, however, differs significantly, as the structuresof GUI-based applications often differ from their console-based counterparts.</P><P>Before examining the code for the <TT>BankApplet</TT>, it's helpful to see whatit produces. Figure 14.1 illustrates the main window of the <TT>BankApplet</TT>,as designed using Visual Caf&#233;. Figure 14.2 shows the corresponding hierarchyof objects that make up the user interface. <BR><BR><A HREF="javascript:popUp('01.jpg')"><B>Figure 14.1</B></A>. <TT>BankApplet</TT><I>main window.</I> <BR><BR><A HREF="javascript:popUp('02.jpg')"><B>Figure 14.2.</B></A> <TT>BankApplet</TT><I>main window hierarchy.</I><H4><FONT COLOR="#000077">Listing 14.3. BankApplet.java.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>  1: // BankApplet.java</TT><TT>  2: </TT><TT>  3: import java.awt.*;</TT><TT>  4: import java.awt.event.ItemEvent;</TT><TT>  5: import java.applet.*;</TT><TT>  6: import java.util.Enumeration;</TT><TT>  7: import java.util.Hashtable;</TT><TT>  8: </TT><TT>  9: import org.omg.CORBA.BOA;</TT><TT> 10: import org.omg.CORBA.ORB;</TT><TT> 11: </TT><TT> 12: import idlGlobal.Account;</TT><TT> 13: import idlGlobal.ATM;</TT><TT> 14: import idlGlobal.Bank;</TT><TT> 15: import idlGlobal.BankServer;</TT><TT> 16: import idlGlobal.BankServerHelper;</TT><TT> 17: import idlGlobal.CheckingAccountHelper;</TT><TT> 18: import idlGlobal.Customer;</TT><TT> 19: </TT><TT> 20: import idlGlobal.InsufficientFundsException;</TT><TT> 21: import idlGlobal.InvalidAccountException;</TT><TT> 22: import idlGlobal.InvalidAmountException;</TT><TT> 23: </TT><TT> 24: public class BankApplet extends Applet implements</TT><TT> 25:         AccountUpdateListener {</TT><TT> 26: </TT><TT> 27:     public static ORB myORB;</TT><TT> 28:     public static BOA myBOA;</TT><TT> 29: </TT><TT> 30:     // This BankApplet's BankServer.</TT><TT> 31:     BankServer myBankServer;</TT><TT> 32: </TT><TT> 33:     // This BankApplet's currently selected Bank, ATM, Customer,</TT><TT> 34:     // and Account.</TT><TT> 35:     Bank mySelectedBank;</TT><TT> 36:     ATM mySelectedATM;</TT><TT> 37:     Customer mySelectedCustomer;</TT><TT> 38:     Account mySelectedAccount;</TT><TT> 39: </TT><TT> 40:     // This BankApplet's list of Banks, keyed on the Bank name.</TT><TT> 41:     Hashtable myBanks = new Hashtable();</TT><TT> 42: </TT><TT> 43:     // This BankApplet's list of ATMs, keyed on the ATM name.</TT><TT> 44:     Hashtable myATMs = new Hashtable();</TT><TT> 45: </TT><TT> 46:     // This BankApplet's list of Customers, keyed on the Customer</TT><TT> 47:     // name. Note that this means that Customer names must be</TT><TT> 48:     // unique in this applet!</TT><TT> 49:     Hashtable myCustomers = new Hashtable();</TT><TT> 50: </TT><TT> 51:     // This BankApplet's list of Accounts. This Hashtable is keyed</TT><TT> 52:     // on Customer objects, which in turn point to Hashtables which</TT><TT> 53:     // are keyed on the concatenation of Bank names and Account</TT><TT> 54:     // numbers and identify Accounts.</TT><TT> 55:     Hashtable myAccounts = new Hashtable();</TT><TT> 56: </TT><TT> 57:     // This BankApplet's map which links Accounts to Banks.</TT><TT> 58:     Hashtable myAccountBankMap = new Hashtable();</TT><TT> 59: </TT><TT> 60:     public void init() {</TT><TT> 61: </TT><TT> 62:         //{{INIT_CONTROLS</TT><TT> 63:         GridBagLayout gridBagLayout;</TT><TT> 64:         gridBagLayout = new GridBagLayout();</TT><TT> 65:         setLayout(gridBagLayout);</TT><TT> 66:         setSize(404, 327);</TT><TT> 67:         setBackground(new Color(-4144944));</TT><TT> 68:         bankChoice = new java.awt.Choice();</TT><TT> 69:         bankChoice.setBounds(112, 3, 322, 21);</TT><TT> 70:         GridBagConstraints gbc;</TT><TT> 71:         gbc = new GridBagConstraints();</TT><TT> 72:         gbc.gridx = 1;</TT><TT> 73:         gbc.gridy = 0;</TT><TT> 74:         gbc.weightx = 1.0;</TT><TT> 75:         gbc.fill = GridBagConstraints.HORIZONTAL;</TT><TT> 76:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT> 77:         gbc.ipadx = 100;</TT><TT> 78:         ((GridBagLayout)getLayout()).setConstraints(bankChoice,</TT><TT> 79:                 gbc);</TT><TT> 80:         add(bankChoice);</TT><TT> 81:         atmChoice = new java.awt.Choice();</TT><TT> 82:         atmChoice.setBounds(112, 30, 156, 21);</TT><TT> 83:         gbc = new GridBagConstraints();</TT><TT> 84:         gbc.gridx = 1;</TT><TT> 85:         gbc.gridy = 1;</TT><TT> 86:         gbc.weightx = 1.0;</TT><TT> 87:         gbc.fill = GridBagConstraints.HORIZONTAL;</TT><TT> 88:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT> 89:         ((GridBagLayout)getLayout()).setConstraints(atmChoice,</TT><TT> 90:                 gbc);</TT><TT> 91:         add(atmChoice);</TT><TT> 92:         customerChoice = new java.awt.Choice();</TT><TT> 93:         customerChoice.setBounds(112, 57, 156, 21);</TT><TT> 94:         gbc = new GridBagConstraints();</TT><TT> 95:         gbc.gridx = 1;</TT><TT> 96:         gbc.gridy = 2;</TT><TT> 97:         gbc.weightx = 1.0;</TT><TT> 98:         gbc.fill = GridBagConstraints.HORIZONTAL;</TT><TT> 99:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>100:         ((GridBagLayout)getLayout()).setConstraints(customerChoice,</TT><TT>101:                 gbc);</TT><TT>102:         add(customerChoice);</TT><TT>103:         newCustomerButton = new java.awt.Button();</TT><TT>104:         newCustomerButton.setActionCommand(&quot;button&quot;);</TT><TT>105:         newCustomerButton.setLabel(&quot;New...&quot;);</TT><TT>106:         newCustomerButton.setBounds(272, 56, 48, 23);</TT><TT>107:         gbc = new GridBagConstraints();</TT><TT>108:         gbc.gridx = 2;</TT><TT>109:         gbc.gridy = 2;</TT><TT>110:         gbc.fill = GridBagConstraints.NONE;</TT><TT>111:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>112:         ((GridBagLayout)getLayout()).</TT><TT>113:                 setConstraints(newCustomerButton, gbc);</TT><TT>114:         add(newCustomerButton);</TT><TT>115:         accountChoice = new java.awt.Choice();</TT><TT>116:         accountChoice.setBounds(112, 84, 156, 21);</TT><TT>117:         gbc = new GridBagConstraints();</TT><TT>118:         gbc.gridx = 1;</TT><TT>119:         gbc.gridy = 3;</TT><TT>120:         gbc.weightx = 1.0;</TT><TT>121:         gbc.fill = GridBagConstraints.HORIZONTAL;</TT><TT>122:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>123:         ((GridBagLayout)getLayout()).setConstraints(accountChoice,</TT><TT>124:                 gbc);</TT><TT>125:         add(accountChoice);</TT><TT>126:         accountChoice.setEnabled(false);</TT><TT>127:         newAccountButton = new java.awt.Button();</TT><TT>128:         newAccountButton.setActionCommand(&quot;button&quot;);</TT><TT>129:         newAccountButton.setLabel(&quot;New...&quot;);</TT><TT>130:         newAccountButton.setBounds(272, 83, 48, 23);</TT><TT>131:         gbc = new GridBagConstraints();</TT><TT>132:         gbc.gridx = 2;</TT><TT>133:         gbc.gridy = 3;</TT><TT>134:         gbc.fill = GridBagConstraints.NONE;</TT><TT>135:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>136:         ((GridBagLayout)getLayout()).</TT><TT>137:                 setConstraints(newAccountButton, gbc);</TT><TT>138:         add(newAccountButton);</TT><TT>139:         newAccountButton.setEnabled(false);</TT><TT>140:         autoUpdateButton = new java.awt.Button();</TT><TT>141:         autoUpdateButton.setActionCommand(&quot;button&quot;);</TT><TT>142:         autoUpdateButton.setLabel(&quot;AutoUpdate&quot;);</TT><TT>143:         autoUpdateButton.setBounds(324, 83, 78, 23);</TT><TT>144:         gbc = new GridBagConstraints();</TT><TT>145:         gbc.gridx = 3;</TT><TT>146:         gbc.gridy = 3;</TT><TT>147:         gbc.fill = GridBagConstraints.NONE;</TT><TT>148:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>149:         ((GridBagLayout)getLayout()).</TT><TT>150:                 setConstraints(autoUpdateButton, gbc);</TT><TT>151:         add(autoUpdateButton);</TT><TT>152:         autoUpdateButton.setEnabled(false);</TT><TT>153:         selectBankLabel = new java.awt.Label(&quot;Select Bank&quot;);</TT><TT>154:         selectBankLabel.setBounds(2, 2, 79, 23);</TT><TT>155:         gbc = new GridBagConstraints();</TT><TT>156:         gbc.gridx = 0;</TT><TT>157:         gbc.gridy = 0;</TT><TT>158:         gbc.anchor = GridBagConstraints.WEST;</TT><TT>159:         gbc.fill = GridBagConstraints.NONE;</TT><TT>160:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>161:         ((GridBagLayout)getLayout()).</TT><TT>162:                 setConstraints(selectBankLabel, gbc);</TT><TT>163:         add(selectBankLabel);</TT><TT>164:         selectATMLabel = new java.awt.Label(&quot;Select ATM&quot;);</TT><TT>165:         selectATMLabel.setBounds(2, 29, 74, 23);</TT><TT>166:         gbc = new GridBagConstraints();</TT><TT>167:         gbc.gridx = 0;</TT><TT>168:         gbc.gridy = 1;</TT><TT>169:         gbc.anchor = GridBagConstraints.WEST;</TT><TT>170:         gbc.fill = GridBagConstraints.NONE;</TT><TT>171:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>172:         ((GridBagLayout)getLayout()).</TT><TT>173:                 setConstraints(selectATMLabel, gbc);</TT><TT>174:         add(selectATMLabel);</TT><TT>175:         selectCustomerLabel = new java.awt.</TT><TT>176:                 Label(&quot;Select Customer&quot;);</TT><TT>177:         selectCustomerLabel.setBounds(2, 56, 106, 23);</TT><TT>178:         gbc = new GridBagConstraints();</TT><TT>179:         gbc.gridx = 0;</TT><TT>180:         gbc.gridy = 2;</TT><TT>181:         gbc.anchor = GridBagConstraints.WEST;</TT><TT>182:         gbc.fill = GridBagConstraints.NONE;</TT><TT>183:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>184:         ((GridBagLayout)getLayout()).</TT><TT>185:                 setConstraints(selectCustomerLabel, gbc);</TT><TT>186:         add(selectCustomerLabel);</TT><TT>187:         selectAccountLabel = new java.awt.Label(&quot;Select Account&quot;);</TT><TT>188:         selectAccountLabel.setBounds(2, 83, 94, 23);</TT><TT>189:         gbc = new GridBagConstraints();</TT><TT>190:         gbc.gridx = 0;</TT><TT>191:         gbc.gridy = 3;</TT><TT>192:         gbc.anchor = GridBagConstraints.WEST;</TT><TT>193:         gbc.fill = GridBagConstraints.NONE;</TT><TT>194:         gbc.insets = new Insets(2, 2, 2, 2);</TT><TT>195:         ((GridBagLayout)getLayout()).</TT><TT>196:                 setConstraints(selectAccountLabel, gbc);</TT><TT>197:         add(selectAccountLabel);</TT><TT>198:         accountActionPanel = new java.awt.Panel();</TT><TT>199:         gridBagLayout = new GridBagLayout();</TT><TT>200:         accountActionPanel.setLayout(gridBagLayout);</TT><TT>201:         accountActionPanel.setBounds(2, 110, 400, 188);</TT><TT>202:         gbc = new GridBagConstraints();</TT><TT>203:         gbc.gridx = 0;</TT>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -