📄 swingclient.java
字号:
// SwingClient.java
// SwingClient provides a graphical user interface for interacting
// with the PriceFinder Web service.
package jws1casestudy.clients.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import jws1casestudy.pricefinder.common.*;
public class SwingClient extends JFrame {
private JButton orderButton;
private JTextArea bookDetailsArea;
private ServiceAccess service;
private PriceQuote quote;
private Customer customer;
private BookDetails bookDetails;
private NumberFormat moneyFormat =
NumberFormat.getCurrencyInstance( Locale.US );
public SwingClient()
{
service = new ServiceAccess();
bookDetailsArea = new JTextArea( 10, 25 );
bookDetailsArea.setEditable( false );
Container container = getContentPane();
container.add( bookDetailsArea,
BorderLayout.CENTER );
orderButton = new JButton( "Order Book" );
orderButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
customer = new Customer();
customer.setFirstName( getField( "First Name" ) );
customer.setLastName ( getField( "Last Name" ) );
customer.setStreetAddress( getField( "Street Address" ) );
customer.setCity( getField( "City" ) );
customer.setState( getField( "State" ) );
customer.setZipCode( getField( "Zip Code" ) );
customer.setCountry( getField( "Country" ) );
customer.setEmailAddress( getField( "Email Address" ) );
customer.setCreditCardNumber( getField( "Credit Card Number" ) );
try {
service.orderBook( quote, customer );
}
catch ( Exception exception ) {
exception.printStackTrace();
}
}
}
);
container.add( orderButton, BorderLayout.SOUTH );
String ISBN = JOptionPane.showInputDialog( this,
"Enter the ISBN of the book you would like to purchase" );
try {
bookDetails = service.getBookDetails( ISBN );
quote = service.getBestPrice( ISBN );
bookDetailsArea.append( "\nTitle: " + bookDetails.getTitle() );
bookDetailsArea.append( "\nAuthors: " + bookDetails.getAuthors() );
bookDetailsArea.append( "\nDescription: " + bookDetails.getDescription() );
bookDetailsArea.append( "\nPrice: " + quote.getPrice() + ", from " + quote.getStoreDescription() );
}
catch ( Exception exception ) {
exception.printStackTrace();
}
setDefaultCloseOperation( EXIT_ON_CLOSE );
pack();
}
private String getField( String fieldName )
{
return JOptionPane.showInputDialog( this, "Enter " + fieldName );
}
public static void main( String args[] )
{
new SwingClient().show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -