shippinghub.java

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

JAVA
564
字号
         
      ); // end call to addActionListener

      // set up updateJButton
      updateJButton = new JButton();
      updateJButton.setBounds( 379, 248, 85, 26 );
      updateJButton.setText( "Update" );
      updateJButton.setEnabled( false );
      contentPane.add( updateJButton );
      updateJButton.addActionListener( 
      
         new ActionListener() // anonymous inner class
         {
            // event handler called when updateJButton is pressed
            public void actionPerformed( ActionEvent event )
            {
               updateJButtonActionPerformed( event );
            }
            
         } // end anonymous inner class
         
      ); // end call to addActionListener

      // set up backJButton
      backJButton = new JButton();
      backJButton.setBounds( 469, 248, 85, 26 );
      backJButton.setText( "< Back" );
      backJButton.setEnabled( false );
      contentPane.add( backJButton );
      backJButton.addActionListener( 
      
         new ActionListener() // anonymous inner class
         {
            // event handler called when backJButton is pressed
            public void actionPerformed( ActionEvent event )
            {
               backJButtonActionPerformed( event );
            }
            
         } // end anonymous inner class
         
      ); // end call to addActionListener

      // set up nextJButton
      nextJButton = new JButton();
      nextJButton.setBounds( 559, 248, 85, 26 );
      nextJButton.setText( "Next >" );
      nextJButton.setEnabled( false );
      contentPane.add( nextJButton );
      nextJButton.addActionListener( 
      
         new ActionListener() // anonymous inner class
         {
            // event handler called when nextJButton is pressed
            public void actionPerformed( ActionEvent event )
            {
               nextJButtonActionPerformed( event );
            }
            
         } // end anonymous inner class
         
      ); // end call to addActionListener
      
      // set properties of application's window
      setTitle( "Shipping Hub" ); // set title bar string
      setSize( 663, 313 );        // set window size
      setVisible( true );         // display window
      
   } // end method createUserInterface
   
   // prepare to scan a new parcel
   private void scanNewJButtonActionPerformed( ActionEvent event )
   {
      // clear JTextFields
      clearComponents();

      // disable appropriate components
      setJButtons( false );

      // enable or make editable appropriate components
      addJButton.setEnabled( true );
      parcelInformationJPanelEditable( true );
      
      // grab focus
      nameJTextField.requestFocusInWindow();
      
   } // end method scanNewJButtonActionPerformed
   
   // add a new Parcel
   private void addJButtonActionPerformed( ActionEvent event )
   {
      // set information for new Parcel
      setParcelData();
      
      // disable or make uneditable appropriate components
      addJButton.setEnabled( false );
      parcelInformationJPanelEditable( false );

      // enable appropriate components
      setJButtons( true );
      
   } // end method addJButtonActionPerformed
   
   // remove a Parcel
   private void removeJButtonActionPerformed( ActionEvent event )
   {
      // retrieve the state of the current Parcel
      String stateSelected = newParcel.getState();
      
      // load next Parcel in list if there is one
      if ( parcelsArrayList.size() > 0 )
      {
         if ( position >= parcelsArrayList.size() )
         {
            position = 0; // go to beginning
         }
      }
      else
      {
         // if no other Parcels remain
         clearComponents();
      }

      setJButtons( true ); // enabled appropriate JButtons

      // set focus to scanNewJButton
      scanNewJButton.requestFocusInWindow();
      
   } // end method removeJButtonActionPerformed
   
   // allow user to edit Parcel information
   private void editJButtonActionPerformed( ActionEvent event )
   {
      // disable appropriate components
      setJButtons( false );

	  // make user able to update Parcel information
	  updateJButton.setEnabled( true );
	  parcelInformationJPanelEditable( true );
      
   } // end method editJButtonActionPerformed

   // move to next Parcel
   private void updateJButtonActionPerformed( ActionEvent event )
   {
      // enable or make editable appropriate components
      setJButtons( true );

      // disable or make uneditable appropriate components
      updateJButton.setEnabled( false );
      parcelInformationJPanelEditable( false );
      
   } // end method updateJButtonActionPerformed

   // move to previous Parcel
   private void backJButtonActionPerformed( ActionEvent event )
   {
      if ( position > 0 )
      {
         position--; // move position back by 1
      }
      else // go to last element in list
      {
         position = parcelsArrayList.size() - 1; 
      }

   } // end method backJButtonActionPerformed
   
   // move to next Parcel
   private void nextJButtonActionPerformed( ActionEvent event )
   {
      if ( position < parcelsArrayList.size() - 1 )
      {
         position++; // move position forward by 1
      }
      else
      {
         position = 0; // go to first element in list
      }
      
   } // end method nextJButtonActionPerformed

   // change the list of Parcels in the parcelStateJList
   private void parcelStateJComboBoxActionPerformed(
      ActionEvent event )
   {
      
   } // end method parcelStateJComboBoxActionPerformed
   
   // set all information about the Parcel
   private void setParcelData()
   {
      newParcel.setName( nameJTextField.getText() );
      newParcel.setAddress( addressJTextField.getText() );
      newParcel.setCity( cityJTextField.getText() );
      newParcel.setState( states[
         stateJComboBox.getSelectedIndex() ] );
      newParcel.setZip( Integer.parseInt(
         zipJTextField.getText() ) );

   } // end method setParcelData
   
   // display all information about the Parcel
   private void loadParcel()
   {

   } // end method loadParcel
   
   // clear all information about the Parcel
   private void clearComponents()
   {
      nameJTextField.setText( "" );
      addressJTextField.setText( "" );
      cityJTextField.setText( "" );
      zipJTextField.setText( "" );
      arrivedAtJTextField.setText( "" );
      parcelIDJTextField.setText( "" );

   } // end method clearComponents

   // enabled/disable JButtons
   private void setJButtons( boolean state )
   {
      backJButton.setEnabled( state );
	  scanNewJButton.setEnabled( state );
      removeJButton.setEnabled( state );
      editJButton.setEnabled( state );
      nextJButton.setEnabled( state );

      // disable navigation if not multiple packages
      if ( parcelsArrayList.size() < 2 )
      {
         nextJButton.setEnabled( false );
         backJButton.setEnabled( false );
      }

      // if no items, disable Remove, Edit and Update JButtons
      if ( parcelsArrayList.size() == 0 )
      {
         editJButton.setEnabled( false );
         updateJButton.setEnabled( false );
         removeJButton.setEnabled( false );
      }

   } // end method setJButtons

   // make editable or uneditable components
   // in parcelInformationJPanel
   private void parcelInformationJPanelEditable( boolean editable )
   {
      nameJTextField.setEditable( editable );
      addressJTextField.setEditable( editable );
      cityJTextField.setEditable( editable );
      stateJComboBox.setEnabled( editable );
      zipJTextField.setEditable( editable );

   } // end method parcelInformationJPanelEditable
   
   // main method
   public static void main( String[] args )
   {
      ShippingHub application = new ShippingHub();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } // end method main
   
} // end class ShippingHub

/**************************************************************************
 * (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 + -
显示快捷键?