fuzzydiceorderform.java

来自「32个java程序源代码」· Java 代码 · 共 477 行 · 第 1/2 页

JAVA
477
字号
// Exercise 7.17: FuzzyDiceOrderForm.java
// This application calculates the total cost of a
// purchase order for different colored fuzzy dice.
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;

public class FuzzyDiceOrderForm extends JFrame
{
   // JLabel that displays header on application window
   private JLabel fuzzyDiceJLabel;

   // JLabel and JTextField for order number
   private JLabel orderNumberJLabel;
   private JTextField orderNumberJTextField;

   // JLabel and JTextField for user's name
   private JLabel nameJLabel;
   private JTextField nameJTextField;

   // JLabel and JTextFields for user's address
   private JLabel addressJLabel;
   private JTextField addressJTextField1;
   private JTextField addressJTextField2;

   // JLabel and JCheckBoxes for die types
   private JLabel typeJLabel;
   private JCheckBox whiteTypeJCheckBox;
   private JCheckBox redTypeJCheckBox;

   // JLabel and JTextFields for die quantities
   private JLabel quantityJLabel;
   private JTextField whiteQuantityJTextField;
   private JTextField redQuantityJTextField;

   // JLabels for die prices
   private JLabel priceJLabel;
   private JLabel whitePriceJLabel;
   private JLabel redPriceJLabel;

   // JLabel and JTextFields for die subtotals
   private JLabel totalsJLabel;
   private JTextField whiteTotalsJTextField;
   private JTextField redTotalsJTextField;

   // JLabel and JTextField for total before tax
   private JLabel subtotalJLabel;
   private JTextField subtotalJTextField;
   
   // JLabel and JTextField for tax
   private JLabel taxJLabel;
   private JTextField taxJTextField;

   // JLabel and JTextField for discount
   private JLabel discountJLabel;
   private JTextField discountJTextField;

   // JLabel and JTextField for final total
   private JLabel totalJLabel;
   private JTextField totalJTextField;

   // JButton to initiate calculate of user's bill
   private JButton calculateJButton;
   
   // no-argument constructor
   public FuzzyDiceOrderForm()
   {
      createUserInterface();
   }
   
   // 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 fuzzyDiceJLabel
      fuzzyDiceJLabel = new JLabel();
      fuzzyDiceJLabel.setBounds( 137, 16, 235, 24 );
      fuzzyDiceJLabel.setText( "Fuzzy Dice" );
      fuzzyDiceJLabel.setFont( 
         new Font( "Default", Font.PLAIN, 22 ) );
      contentPane.add( fuzzyDiceJLabel );

      // set up orderNumberJLabel
      orderNumberJLabel = new JLabel();
      orderNumberJLabel.setBounds( 15, 65, 100, 16 );
      orderNumberJLabel.setText( "Order Number:" );
      contentPane.add( orderNumberJLabel );

      // set up orderNumberJTextField
      orderNumberJTextField = new JTextField();
      orderNumberJTextField.setBounds( 111, 65, 48, 21 );
      orderNumberJTextField.setText( "0" );
      orderNumberJTextField.setHorizontalAlignment( 
         JTextField.RIGHT );
      contentPane.add( orderNumberJTextField );

      // set up nameJLabel
      nameJLabel = new JLabel();
      nameJLabel.setBounds( 15, 105, 40, 16 );
      nameJLabel.setText( "Name:" );
      contentPane.add( nameJLabel );

      // set up nameJTextField
      nameJTextField = new JTextField();
      nameJTextField.setBounds( 111, 105, 245, 21 );
      nameJTextField.setText( "Enter name here" );
      contentPane.add( nameJTextField );

      // set up addressJLabel
      addressJLabel = new JLabel();
      addressJLabel.setBounds( 15, 129, 56, 16 );
      addressJLabel.setText( "Address:" );
      contentPane.add( addressJLabel );

      // set up addressJTextField1
      addressJTextField1 = new JTextField();
      addressJTextField1.setBounds( 111, 129, 245, 21 );
      addressJTextField1.setText( "Address Line 1" );
      contentPane.add( addressJTextField1 );

      // set up addressJTextField2
      addressJTextField2 = new JTextField();
      addressJTextField2.setBounds( 111, 153, 245, 21 );
      addressJTextField2.setText( "City, State, Zip" );
      contentPane.add( addressJTextField2 );

      // set up typeJLabel
      typeJLabel = new JLabel();
      typeJLabel.setBounds( 15, 204, 40, 16 );
      typeJLabel.setText( "Type:" );
      contentPane.add( typeJLabel );

      // set up whiteTypeJCheckBox
      whiteTypeJCheckBox = new JCheckBox();
      whiteTypeJCheckBox.setBounds( 10, 227, 93, 21 );
      whiteTypeJCheckBox.setText( "White/Black" );
      contentPane.add( whiteTypeJCheckBox );
      whiteTypeJCheckBox.addActionListener( 
      
         new ActionListener() // anonymous inner class
         {
            // event handler called when user selects
            // or deselects whiteTypeJCheckBox
            public void actionPerformed( ActionEvent event )
            {
               whiteTypeJCheckBoxActionPerformed( event );
            }
            
         } // end anonymous inner class
         
      ); // end call to addActionListener
      
      // set up redTypeJCheckBox
      redTypeJCheckBox = new JCheckBox(); 
      redTypeJCheckBox.setBounds( 10, 252, 88, 21 );
      redTypeJCheckBox.setText( "Red/Black" ); 
      contentPane.add( redTypeJCheckBox );
      redTypeJCheckBox.addActionListener( 
      
         new ActionListener() // anonymous inner class
         {
            // event handler called when user selects
            // or deselects redTypeJCheckBox
            public void actionPerformed( ActionEvent event )
            {
               redTypeJCheckBoxActionPerformed( event );
            }
            
         } // end anonymous inner class
         
      ); // end call to addActionListener

      // set up quantityJLabel
      quantityJLabel = new JLabel();
      quantityJLabel.setBounds( 111, 204, 72, 16 );
      quantityJLabel.setText( "Quantity:" );
      contentPane.add( quantityJLabel );

      // set up whiteQuantityJTextField
      whiteQuantityJTextField = new JTextField();
      whiteQuantityJTextField.setBounds( 111, 228, 64, 21 );
      whiteQuantityJTextField.setText( "0" );
      whiteQuantityJTextField.setHorizontalAlignment( 
         JTextField.RIGHT );
      contentPane.add( whiteQuantityJTextField );

      // set up redQuantityJTextField
      redQuantityJTextField = new JTextField();
      redQuantityJTextField.setBounds( 111, 252, 64, 21 );
      redQuantityJTextField.setText( "0" );
      redQuantityJTextField.setHorizontalAlignment(
         JTextField.RIGHT );
      contentPane.add( redQuantityJTextField );

      // set up priceJLabel
      priceJLabel = new JLabel();
      priceJLabel.setBounds( 196, 204, 72, 16 );
      priceJLabel.setText( "Price:" );
      contentPane.add( priceJLabel );

      // set up whitePriceJLabel
      whitePriceJLabel = new JLabel();
      whitePriceJLabel.setBounds( 196, 228, 80, 21 );
      whitePriceJLabel.setText( "$6.25" );
      contentPane.add( whitePriceJLabel );

      // set up redPriceJLabel
      redPriceJLabel = new JLabel();
      redPriceJLabel.setBounds( 196, 252, 80, 21 );
      redPriceJLabel.setText( "$5.00" );
      contentPane.add( redPriceJLabel );

      // set up totalsJLabel
      totalsJLabel = new JLabel();
      totalsJLabel.setBounds( 267, 204, 72, 16 );
      totalsJLabel.setText( "Totals:" );
      contentPane.add( totalsJLabel );

      // set up whiteTotalsJTextField
      whiteTotalsJTextField = new JTextField();
      whiteTotalsJTextField.setBounds( 267, 228, 87, 16 );
      whiteTotalsJTextField.setText( "$0.00" );
      whiteTotalsJTextField.setEditable( false );
      whiteTotalsJTextField.setHorizontalAlignment( 
         JTextField.RIGHT );
      contentPane.add( whiteTotalsJTextField );

      // set up redTotalsJTextField
      redTotalsJTextField = new JTextField();
      redTotalsJTextField.setBounds( 267, 252, 87, 16 );
      redTotalsJTextField.setText( "$0.00" );
      redTotalsJTextField.setEditable( false );
      redTotalsJTextField.setHorizontalAlignment( 

⌨️ 快捷键说明

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