// IntCalc - Calculates the interest on a loan. // This program demonstrates different Swing Pluggable-Look-and-Feels, // or "PLAFs". This code was adapted from Art Gittleman's "Internet // Applications with the Java 2 Platform", (C) 2001 Scott/Jones Inc., // Example 5.3. // // Either pass in cmd-line args of 0, 1, or 2 (for the three standard // PLAFs, or use PARAM tags if run as an Applet. Note the Windows // PLAF and the Motif PLAF depend on platform-specific libraries and // may not work right (or at all) on different platforms. // // This program also uses JLabels, JButtons, JTextFields, JPanels, // JComponents, I18N (NumberFormat), and shows the use of HTML in // a JLabel, and the use of a Border. // // Written by Wayne Pollock, Tampa Florida USA, 2/2001. import java.awt.*; import java.awt.event.*; import java.text.NumberFormat; import java.util.Enumeration; import javax.swing.*; import javax.swing.border.*; public class IntCalc extends JApplet implements ActionListener { private String [] args = null; private JTextField rateTF; private JTextField balTF; private JTextField yearsTF; private JLabel msgLbl; private JButton btn; private String plafName; private NumberFormat nf; public static void main ( String [] args ) { JFrame frame = new JFrame( "Interest Calculator" ); frame.setSize( 480, 300 ); //Center the Frame: Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); int x = ( screenSize.width - frameSize.width ) / 2; int y = ( screenSize.height - frameSize.height ) / 2; frame.setLocation( x, y ); Container c = frame.getContentPane(); frame.addWindowListener( new WindowAdapter () // Window-closing code. { public void windowClosing ( WindowEvent e ) { System.exit( 0 ); } } ); IntCalc ic = new IntCalc(); ic.args = args; c.add( ic, "Center" ); frame.setVisible( true ); ic.init(); ic.start(); } public void init () { // Set PLAF and plafName from cmd-line args OR PARAM tags: String plafString; int plaf; if ( args == null ) // Then was run as an Applet { plafString = getParameter( "PLAF" ); if ( plafString == null ) plafString = "0"; // Use default ("0") PLAF, which is "Metal". } else { if ( args.length > 0 ) plafString = args[0]; else plafString = "0"; } try { plaf = Integer.parseInt( plafString ); } catch ( NumberFormatException nfe ) { plaf = 0; } // Create array of available PLAFs (note inner class!): UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels(); try { plafName = info[plaf].getClassName(); UIManager.setLookAndFeel( plafName ); plafName = info[plaf].getName(); // This gets a human-readable name. } catch ( Exception ignored ) {} // Initialiaze properties: rateTF = new JTextField( 6 ); balTF = new JTextField( 8 ); yearsTF = new JTextField( 6 ); msgLbl = new JLabel( "", JLabel.CENTER ); msgLbl.setText( "Enter amounts,
" + "then press button" ); btn = new JButton( "Compute" ); btn.setMnemonic( 'c' ); // Set keyboard shortcut key to Alt+C. btn.setToolTipText( "Click to calculate total amount" ); nf = NumberFormat.getCurrencyInstance(); // Create layout: JPanel c = new JPanel( new BorderLayout() ); setContentPane( c ); // The default contentPane can't have a border! // Set a fancy border on the applet: MatteBorder mb = new MatteBorder( 10, 10, 10, 10, Color.blue ); TitledBorder tb = new TitledBorder( mb, " Interest Calculator \u00A9" + "2001 by Wayne Pollock ", TitledBorder.LEFT, TitledBorder.BOTTOM, new Font( "SansSerif", Font.PLAIN, 10 ), Color.green.darker().darker() ); c.setBorder( tb ); JPanel top = new JPanel( new FlowLayout( FlowLayout.CENTER, 15, 5 ) ); JPanel p = new JPanel( new GridLayout( 2, 1 ) ); p.add( new JLabel( "Rate", JLabel.CENTER ) ); p.add( rateTF ); top.add( p, "West" ); p = new JPanel( new GridLayout( 2, 1 ) ); p.add( new JLabel( "Balance", JLabel.CENTER ) ); p.add( balTF ); top.add( p, "Center" ); p = new JPanel( new GridLayout( 2, 1 ) ); p.add( new JLabel( "# of Years", JLabel.CENTER ) ); p.add( yearsTF ); top.add( p, "East" ); JPanel bot = new JPanel( new GridLayout( 1, 3 ) ); p = new JPanel(); // Default is FlowLayout p.add( btn ); bot.add( new JLabel( " " ) ); // Just a spacer. bot.add( p ); bot.add( new JLabel( "PLAF: " + plafName + " ", JLabel.RIGHT ) ); c.add( top, "North" ); c.add( msgLbl, "Center" ); c.add( bot, "South" ); // Set up event handling: btn.addActionListener( this ); rateTF.requestFocus(); validate(); } public void actionPerformed( ActionEvent ae ) { msgLbl.setText( computeAmount() ); rateTF.requestFocus(); } private String computeAmount() { double rate, bal; int years; String s = ""; try { rate = Double.parseDouble( rateTF.getText() ); bal = Double.parseDouble( balTF.getText() ); years = Integer.parseInt( yearsTF.getText() ); } catch ( NumberFormatException nfe ) { s = "Error:
"; s += ""; s += "Rate and Balance must be doubles,
"; s += "Years must be an int"; return s; } rate = rate / 100.0; // a Rate of "10" percent is "0.10". for ( int i = 0; i < years; ++i ) bal += bal * rate; s = "The Total Amount is:" + " "; return s; } }
" + nf.format( bal ) + "
Send comments and mail to the WebMaster. |