19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

157 NumberFormat.getCurrencyInstance(locale);<br />

158 NumberFormat numberForm = NumberFormat.getNumberInstance(locale);<br />

159 percentFormatter.setMinimumFractionDigits(2);<br />

160<br />

161 // Display formatted input<br />

162 jtfFormattedInterestRate.setText(<br />

163 percentFormatter.format(interestRate * 12));<br />

164 jtfFormattedNumberOfYears.setText<br />

165 (numberForm.format(numberOfYears));<br />

166 jtfFormattedLoanAmount.setText(currencyForm.format(loan));<br />

167<br />

168 // Display results in currency format<br />

169 jtfMonthlyPayment.setText(currencyForm.format(monthlyPayment));<br />

170 jtfTotalPayment.setText(currencyForm.format(<strong>to</strong>talPayment));<br />

171 }<br />

172<br />

173 /** Update resource strings */<br />

174 private void updateStrings() {<br />

175 res = ResourceBundle.getBundle("MyResource", locale);<br />

176 jlblInterestRate.setText(res.getString("Annual_Interest_Rate"));<br />

177 jlblNumberOfYears.setText(res.getString("Number_Of_Years"));<br />

178 jlblLoanAmount.setText(res.getString("Loan_Amount"));<br />

179 jlblTotalPayment.setText(res.getString("Total_Payment"));<br />

180 jlblMonthlyPayment.setText(res.getString("Monthly_Payment"));<br />

181 jbtCompute.setText(res.getString("Compute"));<br />

182 <strong>com</strong>boBoxTitle.setTitle(res.getString("Choose_a_Locale"));<br />

183 inputTitle.setTitle(res.getString("Enter_Interest_Rate"));<br />

184 paymentTitle.setTitle(res.getString("Payment"));<br />

185<br />

186 // Make sure the new labels are displayed<br />

187 repaint();<br />

188 }<br />

189<br />

190 /** Main method */<br />

191 public static void main(String[] args) {<br />

192 // Create an instance of the applet<br />

193 ResourceBundleDemo applet = new ResourceBundleDemo();<br />

194<br />

195 // Create a frame with a resource string<br />

196 JFrame frame = new JFrame(<br />

197 applet.res.getString("Number_Formatting"));<br />

198<br />

199 // Add the applet instance <strong>to</strong> the frame<br />

200 frame.add(applet, BorderLayout.CENTER);<br />

201<br />

202 // Invoke init() and start()<br />

203 applet.init();<br />

204 applet.start();<br />

205<br />

206 // Display the frame<br />

207 frame.setSize(400, 300);<br />

208 frame.setLocationRelativeTo(null);<br />

209 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />

210 frame.setVisible(true);<br />

211 }<br />

212 }<br />

30

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!