// Exercise 28.16: CarPayment.java
// Calculate different billing plans for a car loan.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.util.*;
import javax.speech.*;
import javax.speech.synthesis.*;
public class CarPayment extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 4067748494903359401L;
// JLabel and JTextfield for price
private JLabel priceJLabel;
private JTextField priceJTextField;
// JLabel and JTextfield for down payment
private JLabel downPaymentJLabel;
private JTextField downPaymentJTextField;
// JLabel and JTextfield for interest
private JLabel interestJLabel;
private JTextField interestJTextField;
// JButton for instructions
private JButton instructionsJButton;
// JButton to initiate calculation
private JButton calculateJButton;
// JTextArea to display results
private JTextArea paymentsJTextArea;
//创建一个Synthesizer对象
private Synthesizer speechSynthesizer;
// no-argument constructor
public CarPayment()
{
try
{
//为FreeTTS合成器创建一个SynthesizerModeDesc
SynthesizerModeDesc descriptor=new SynthesizerModeDesc(
"Unlimited domain FreeTTS Speech Synthesizer from Sun Labs",null,Locale.US,Boolean.FALSE,null);
//创建一个Synthesizer对象
speechSynthesizer =Central.createSynthesizer(descriptor);
if(speechSynthesizer!=null)
{
//准备一个作为朗读的合成器
speechSynthesizer.allocate();
speechSynthesizer.resume();
//取得合成器的属性
SynthesizerProperties properties=
speechSynthesizer.getSynthesizerProperties();
//设置语音合成器的朗读语速
properties.setSpeakingRate(100.0f);
}//end if
else
{
System.err.println("Synthesizer creation failed.");
System.exit(1);
}
}//end try
catch(Exception myException)
{
myException.printStackTrace();
}
createUserInterface(); // set up GUI
}
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up priceJLabel
priceJLabel = new JLabel();
priceJLabel.setBounds( 40, 24, 80, 21 );
priceJLabel.setText( "Price:" );
contentPane.add( priceJLabel );
// set up priceJTextField
priceJTextField = new JTextField();
priceJTextField.setBounds( 184, 24, 56, 21 );
priceJTextField.setHorizontalAlignment( JTextField.RIGHT );
contentPane.add( priceJTextField );
// set up downPaymentJLabel
downPaymentJLabel = new JLabel();
downPaymentJLabel.setBounds( 40, 56, 96, 21 );
downPaymentJLabel.setText( "Down payment:" );
contentPane.add( downPaymentJLabel );
// set up downPaymentJTextField
downPaymentJTextField = new JTextField();
downPaymentJTextField.setBounds( 184, 56, 56, 21 );
downPaymentJTextField.setHorizontalAlignment(
JTextField.RIGHT );
contentPane.add( downPaymentJTextField );
// set up interestJLabel
interestJLabel = new JLabel();
interestJLabel.setBounds( 40, 88, 120, 21 );
interestJLabel.setText( "Annual interest rate:" );
contentPane.add( interestJLabel );
// set up interestJTextField
interestJTextField = new JTextField();
interestJTextField.setBounds( 184, 88, 56, 21 );
interestJTextField.setHorizontalAlignment( JTextField.RIGHT );
contentPane.add( interestJTextField );
// set up instructionsJButton and register its event handler
instructionsJButton = new JButton();
instructionsJButton.setBounds( 40, 128, 110, 24 );
instructionsJButton.setText( "Instructions" );
contentPane.add( instructionsJButton );
instructionsJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when instructionsJButton
// is clicked
public void actionPerformed( ActionEvent event )
{
instructionsJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up calculateJButton and register its event handler
calculateJButton = new JButton();
calculateJButton.setBounds( 155, 128, 90, 24 );
calculateJButton.setText( "Calculate" );
contentPane.add( calculateJButton );
calculateJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is clicked
public void actionPerformed( ActionEvent event )
{
calculateJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up paymentsJTextArea
paymentsJTextArea = new JTextArea();
paymentsJTextArea.setBounds( 28, 168, 232, 90 );
paymentsJTextArea.setEditable( false );
contentPane.add( paymentsJTextArea );
// set properties of application's window
setTitle( "Car Payment Calculator" ); // set title bar string
setSize( 288, 302 ); // set window's size
setVisible( true ); // display window
// ensure synthesizer is cleaned up
// when user closes application
addWindowListener(
new WindowAdapter() // anonymous inner class
{
public void windowClosing( WindowEvent event )
{
frameWindowClosing( event );
}
} // end anonymous inner class
); // end addWindowListener
} // end method createUserInterface
// method called when user clicks instructionsJButton
private void instructionsJButtonActionPerformed(
ActionEvent event )
{
String Elucidation="Please enter price ,down payment and annual interest rate";
//说明文本
speechSynthesizer.speakPlainText(Elucidation, null);
//朗读说明
} // end method instructionsJButtonActionPerformed
// method called when user clicks calculateJButton
private void calculateJButtonActionPerformed( ActionEvent event )
{
int years = 2; // repetition counter
int months; // payment period
double monthlyPayment; // monthly payment
String textToSpeek="You Will Hava to Pay:\n";//显示输出结果时使用
// clear JTextArea
paymentsJTextArea.setText("");
// add header JTextArea
paymentsJTextArea.append( "Months\tMonthly Payments" );
// retrieve user input
int price = Integer.parseInt( priceJTextField.getText() );
int downPayment =
Integer.parseInt( downPaymentJTextField.getText() );
double interest =
Double.parseDouble( interestJTextField.getText() );
// calculate loan amount and monthly interest
int loanAmount = price - downPayment;
double monthlyInterest = interest / 1200;
// format to display monthlyPayment in currency format
DecimalFormat dollars = new DecimalFormat( "$0.00" );
// while years is less than or equal to five years
while ( years <= 5 )
{
// calculate payment period
months = 12 * years;
// get monthlyPayment