⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weatherserviceclient.java

📁 java web services how to program
💻 JAVA
字号:
// WeatherServiceClient.java 
// WeatherServiceClient uses the Weather web service
// to retrieve weather information.
package com.deitel.jws1.jaxrpc.weather;

// Java core packages
import java.rmi.*;   
import java.awt.*;
import java.awt.event.*;

// Java extension packages
import javax.swing.*;
import javax.swing.border.*;

// Java XML RPC packages
import javax.xml.rpc.*;

// Client packages
import weather.*;

public class WeatherServiceClient extends JFrame
{
   private JPanel generalPanel;
   private JPanel dailyPanel;

   private JTextField zipcodeField, cityField, timeField,
      sunriseField, sunsetField;

   private JTextArea dailyArea;

   // WeatherServiceClient constructor
   public WeatherServiceClient() 
   {
      super( "Weather Forecast" );

      // create JButton for invoking Weather Web service
      JButton weatherButton = 
         new JButton( "Get Weather Service" );
      weatherButton.addActionListener(

         new ActionListener() {

            public void actionPerformed( ActionEvent event )
            {
               String zipcode = JOptionPane.showInputDialog( 
                  WeatherServiceClient.this, "Enter zip code");
      
               // if the user inputs zip code, get weather
               // forecast according to zip code
               if ( zipcode != null ) {

                  // get weather forecast
                  WeatherForecast weatherForecast = 
                     getWeatherForecast( zipcode );

                  updateGeneralContent( weatherForecast );
                  updateDailyContent( weatherForecast );
               }

            } // end method actionPerformed

         } // end ActionListener

      ); // end call to addActionListener

      // create JPanel for weatherButton
      JPanel buttonPanel = new JPanel();
      buttonPanel.add( weatherButton );

      // create JPanel for general weather forecast
      generalPanel = new JPanel();
      generalPanel.setBorder( new TitledBorder( "General" ) );
      createGeneralPanel();

      // create JPanel for daily weather forecast
      dailyPanel = new JPanel();
      dailyPanel.setBorder( 
         new TitledBorder( "Daily Forecast" ) );
      createDailyPanel();

      // lay out components
      Container contentPane = getContentPane();
      contentPane.add( buttonPanel, BorderLayout.NORTH );
      contentPane.add( generalPanel, BorderLayout.CENTER );
      contentPane.add( dailyPanel, BorderLayout.SOUTH );
	  
   } // end WeatherServiceClient constructor

   // get weather forecast from Unisys's WeatherService
   public WeatherForecast getWeatherForecast( String zipcode )
   {
      // connect to Web service and get weather information
      try {

         // get Web service
         WeatherServicesSoap weatherService = ( new 
            WeatherServices_Impl() ).getWeatherServicesSoap();

         // get weather information from server
		 WeatherForecast result = 
            weatherService.getWeather( zipcode );
         
         return result;

      } // end try
          
      // handle exceptions communicating with remote object
      catch ( Exception exception ) {
         exception.printStackTrace();

         return null;
      }
      
   } // end method getWeatherForecast

   // create general panel content
   public void createGeneralPanel()
   {
      generalPanel.setLayout( new GridLayout( 5, 2 ) );

      zipcodeField = new JTextField( 15 );
      cityField = new JTextField( 15 );
      timeField = new JTextField( 15 );
      sunriseField = new JTextField( 15 );
      sunsetField = new JTextField( 15 );

      zipcodeField.setEditable( false );
      cityField.setEditable( false );
      timeField.setEditable( false );
      sunriseField.setEditable( false );
      sunsetField.setEditable( false );

      generalPanel.add( new JLabel( "ZipCode: " ) );
      generalPanel.add( zipcodeField );
      generalPanel.add( new JLabel( "City: " ) );
      generalPanel.add( cityField );
      generalPanel.add( new JLabel( "Time: " ) );
      generalPanel.add( timeField );
      generalPanel.add( new JLabel( "Sunrise: " ) );
      generalPanel.add( sunriseField );
      generalPanel.add( new JLabel( "Sunset: " ) );
      generalPanel.add( sunsetField );

   } // end method createGeneralPanel

   // create daily panel content
   public void createDailyPanel()
   {
      dailyArea = new JTextArea( 20, 35 );
      
      dailyPanel.add( new JScrollPane( dailyArea ) );

   } // end method createDailyPanel

   // update general forecast
   public void updateGeneralContent( 
      WeatherForecast weatherForecast )
   {
      // parse WeatherForecast if not null
      if ( weatherForecast != null ) {
         zipcodeField.setText( weatherForecast.getZipCode() );
         cityField.setText( 
            weatherForecast.getCityShortName() );
         timeField.setText( weatherForecast.getTime() );
         sunriseField.setText( weatherForecast.getSunrise() );
         sunsetField.setText( weatherForecast.getSunset() );
      }

   } // end method updateGeneralContent

   // update daily content
   public void updateDailyContent(
      WeatherForecast weatherForecast )
   {
      // get daily forecast if weatherForecast is not null
      if ( weatherForecast != null ) {
         ArrayOfDailyForecast dayForecast = 
            weatherForecast.getDayForecast();

         // get DailyForecast array if dayForecast is not null
         if ( dayForecast != null ) {
            DailyForecast[] dailyForecast = 
               dayForecast.getDailyForecast();

            StringBuffer results = new StringBuffer();

            // store daily forecast to results StringBuffer
            for ( int i = 0; i < dailyForecast.length ; i++ ) {
               results.append( dailyForecast[ i ].getDay() );
               results.append( ": \n   " );
               results.append( dailyForecast[ i ].getForecast() );
               results.append( "\n" );
            }

            dailyArea.setText( results.toString() );

         } // end inner if
         
      } // end outter if

   } // end method updateDailyContent

   // execute WeatherServiceClient
   public static void main( String args[] )
   {
      WeatherServiceClient client = 
         new WeatherServiceClient();
      client.setDefaultCloseOperation( EXIT_ON_CLOSE );
      client.setSize( 425, 600 );
      client.setVisible( true );

   } // end method main

} // end class WeatherServiceClient

/**************************************************************************
 * (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall.     *
 * 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -