fuzzydiceorderform.java
来自「32个java程序源代码」· Java 代码 · 共 477 行 · 第 1/2 页
JAVA
477 行
JTextField.RIGHT );
contentPane.add( redTotalsJTextField );
// set up subtotalJLabel
subtotalJLabel = new JLabel();
subtotalJLabel.setBounds( 196, 293, 72, 16 );
subtotalJLabel.setText( "Subtotal:" );
contentPane.add( subtotalJLabel );
// set up subtotalJTextField
subtotalJTextField = new JTextField();
subtotalJTextField.setBounds( 267, 293, 87, 16 );
subtotalJTextField.setText( "$0.00" );
subtotalJTextField.setEditable( false );
subtotalJTextField.setHorizontalAlignment(
JTextField.RIGHT );
contentPane.add( subtotalJTextField );
// set up taxJLabel
taxJLabel = new JLabel();
taxJLabel.setBounds( 196, 317, 72, 16 );
taxJLabel.setText( "Tax:" );
contentPane.add( taxJLabel );
// set up taxJTextField
taxJTextField = new JTextField();
taxJTextField.setBounds( 267, 317, 87, 16 );
taxJTextField.setText( "$0.00" );
taxJTextField.setEditable( false );
taxJTextField.setHorizontalAlignment(
JTextField.RIGHT );
contentPane.add( taxJTextField );
// set up discountJLabel
discountJLabel = new JLabel();
discountJLabel.setBounds( 196, 341, 72, 16 );
discountJLabel.setText( "Discount:" );
contentPane.add( discountJLabel );
// set up discountJTextField
discountJTextField = new JTextField();
discountJTextField.setBounds( 267, 341, 87, 16 );
discountJTextField.setText( "$0.00" );
discountJTextField.setEditable( false );
discountJTextField.setHorizontalAlignment(
JTextField.RIGHT );
contentPane.add( discountJTextField );
// set up totalJLabel
totalJLabel = new JLabel();
totalJLabel.setBounds( 196, 365, 72, 16 );
totalJLabel.setText( "Total:" );
contentPane.add( totalJLabel );
// set up totalJTextField
totalJTextField = new JTextField();
totalJTextField.setBounds( 267, 365, 87, 16 );
totalJTextField.setText( "$0.00" );
totalJTextField.setEditable( false );
totalJTextField.setHorizontalAlignment(
JTextField.RIGHT );
contentPane.add( totalJTextField );
// set up calculateJButton and register its event handler
calculateJButton = new JButton();
calculateJButton.setBounds( 267, 397, 87, 23 );
calculateJButton.setText( "Calculate" );
contentPane.add( calculateJButton );
calculateJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// method called when user clicks calculateJButton
public void actionPerformed( ActionEvent event )
{
calculateJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set properties of application's window
setTitle( "Fuzzy Dice Order Form" ); // set title bar string
setSize( 380, 466 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
// calculate the cost of the order
private void calculateJButtonActionPerformed( ActionEvent event )
{
// get user inputs
String orderNumber = orderNumberJTextField.getText();
String name = nameJTextField.getText();
String address1 = addressJTextField1.getText();
String address2 = addressJTextField2.getText();
// total of white/black dice ordered
double whiteTotals = 0.0;
// total of red/black dice ordered
double redTotals = 0.0;
// calculate price if white/black JCheckBox is selected
if ( whiteTypeJCheckBox.isSelected() )
{
whiteTotals = Integer.parseInt(
whiteQuantityJTextField.getText() ) * 6.25;
}
// calculate price if red/black JCheckBox is selected
if ( redTypeJCheckBox.isSelected() )
{
redTotals = Integer.parseInt(
redQuantityJTextField.getText() ) * 5.00;
}
// display message if order number, name or address is empty
if ( orderNumber.equals( "" ) || name.equals( "" ) ||
address1.equals( "" ) || address2.equals( "" ) ||
orderNumber.equals( "0" ) ||
name.equals( "Enter name here" ) ||
address1.equals( "Address Line 1" ) ||
address2.equals( "City, State, Zip" ) )
{
// display message
JOptionPane.showMessageDialog( null,
"Please fill out all information fields.",
"Empty Fields", JOptionPane.WARNING_MESSAGE );
}
else if ( !whiteTypeJCheckBox.isSelected() &&
!redTypeJCheckBox.isSelected() )
{
// display message if no JCheckBox is
// selected and its quantity is 0
JOptionPane.showMessageDialog( null,
"Please select an item and enter a quantity.",
"No Item selected", JOptionPane.WARNING_MESSAGE );
}
else // otherwise, calculate totals
{
// set display format
DecimalFormat dollars = new DecimalFormat( "$0.00" );
// display totals of dice ordered
whiteTotalsJTextField.setText( dollars.format(
whiteTotals ) );
redTotalsJTextField.setText( dollars.format( redTotals ) );
// calculate and display subtotal
double subtotal = whiteTotals + redTotals;
subtotalJTextField.setText( dollars.format( subtotal ) );
// if subtotal is greater than $50
// display message and give 7% discount
if ( subtotal > 50 )
{
JOptionPane.showMessageDialog( null,
"7% discount will be applied.", "Discount Offer",
JOptionPane.INFORMATION_MESSAGE );
// calculate and display discount
double discount = subtotal * 0.07;
discountJTextField.setText(
dollars.format( -discount ) );
subtotal -= discount; // calculate new subtotal
}
else // no discount if subtotal is not greater than 50
{
discountJTextField.setText( "$0.00" );
}
// calculate and display tax
double tax = subtotal * 0.05;
taxJTextField.setText( dollars.format( tax ) );
// calculate and display total
totalJTextField.setText(
dollars.format( subtotal + tax ) );
} // end else
} // end method calculateJButtonActionPerformed
// modifying application when whiteTypeJCheckBox
// is selected or deselected
private void whiteTypeJCheckBoxActionPerformed(
ActionEvent event )
{
} // end method whiteTypeJCheckBoxActionPerformed
// modifying application when redTypeJCheckBox
// is selected or deselected
private void redTypeJCheckBoxActionPerformed( ActionEvent event )
{
} // end method redTypeJCheckBoxActionPerformed
// reset components
private void clearResults()
{
whiteTotalsJTextField.setText( "$0.00" );
redTotalsJTextField.setText( "$0.00" );
subtotalJTextField.setText( "$0.00" );
taxJTextField.setText( "$0.00" );
discountJTextField.setText( "$0.00" );
totalJTextField.setText( "$0.00" );
} // end method clearResults
// main method
public static void main( String[] args )
{
FuzzyDiceOrderForm application = new FuzzyDiceOrderForm();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class FuzzyDiceOrderForm
/**************************************************************************
* (C) Copyright 1992-2004 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?