157 // calculate and display sales and earnings
158 private void calculateEarnings()
159 {
160 // get user input
161 int items = Integer.parseInt( itemsSoldJTextField.getText() );
162 double price = Double.parseDouble( priceJTextField.getText() );
163 Integer integerObject =
164 ( Integer ) commissionJSpinner.getValue();
165 int commissionRate = integerObject.intValue();
166
167 // calculate total sales and earnings
168 double sales = items * price;
169 double earnings = ( sales * commissionRate ) / 100;
170
171 // display the results
172 DecimalFormat dollars = new DecimalFormat( "$0.00" );
173 grossSalesJTextField.setText( dollars.format( sales ) );
174 earningsJTextField.setText( dollars.format( earnings ) );
175
176 } // end method calculateEarnings
177
178 private void commissionJSpinnerStateChanged( ChangeEvent event )
179 {
180 calculateEarnings();
181 }
182
183 private void calculateJButtonActionPerformed( ActionEvent event )
184 {
185 calculateEarnings();
186 }
上面是我的代码可是我现在无法完整编译,但是如果我把158行的圆弧括号内写入ChangeEvent event
再把183行的方法去掉,那么178行的方法就可以正确调用158行的方法,如果把158行的圆弧括号内写入ActionEvent event,再把178行的方法去掉,那么183行的方法就可以正确调用了 我的目的是183行和178行的方法都可以调用158行的方法,可是由于event类型不统一,一个是ChangeEnvent ,一个是ActionEvent ,我该怎么改程序哦
高手表BS我,我是菜鸟哦
158 private void calculateEarnings()
159 {
160 // get user input
161 int items = Integer.parseInt( itemsSoldJTextField.getText() );
162 double price = Double.parseDouble( priceJTextField.getText() );
163 Integer integerObject =
164 ( Integer ) commissionJSpinner.getValue();
165 int commissionRate = integerObject.intValue();
166
167 // calculate total sales and earnings
168 double sales = items * price;
169 double earnings = ( sales * commissionRate ) / 100;
170
171 // display the results
172 DecimalFormat dollars = new DecimalFormat( "$0.00" );
173 grossSalesJTextField.setText( dollars.format( sales ) );
174 earningsJTextField.setText( dollars.format( earnings ) );
175
176 } // end method calculateEarnings
177
178 private void commissionJSpinnerStateChanged( ChangeEvent event )
179 {
180 calculateEarnings();
181 }
182
183 private void calculateJButtonActionPerformed( ActionEvent event )
184 {
185 calculateEarnings();
186 }
上面是我的代码可是我现在无法完整编译,但是如果我把158行的圆弧括号内写入ChangeEvent event
再把183行的方法去掉,那么178行的方法就可以正确调用158行的方法,如果把158行的圆弧括号内写入ActionEvent event,再把178行的方法去掉,那么183行的方法就可以正确调用了 我的目的是183行和178行的方法都可以调用158行的方法,可是由于event类型不统一,一个是ChangeEnvent ,一个是ActionEvent ,我该怎么改程序哦
高手表BS我,我是菜鸟哦
private void calculateEarnings(Event e)
用JSpinner输入销售员应得到的提成金的百分比用calculaterJButton计算销售得到的总资金,和销售员得到的提成金,要求点击calculatorJButton和改变JSpinner的时候,销售总资金和总提成金立刻发生改变下面为代码,重要的位置都留了空,请填补一下,没标记的地方没有问题// Exercise 13.13: SalesCommissionCalculator.java
// Application calculates a salesperson's commission from the number
// of units sold.
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.event.*;public class SalesCommissionCalculator extends JFrame
{
// JLabel and JTextField for items sold
private JLabel itemsSoldJLabel;
private JTextField itemsSoldJTextField; // JLabel and JTextField for price of items
private JLabel priceJLabel;
private JTextField priceJTextField; // JLabel and JTextField for gross sales
private JLabel grossSalesJLabel;
private JTextField grossSalesJTextField; // JLabel and JSpinner for commission
private JLabel commissionJLabel;
private JSpinner commissionJSpinner; // JLabel and JTextField for earnings
private JLabel earningsJLabel;
private JTextField earningsJTextField;
// JButton to calculate the earnings
private JButton calculateJButton; // no-argument constructor
public SalesCommissionCalculator()
{
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 itemsSoldJLabel
itemsSoldJLabel = new JLabel();
itemsSoldJLabel.setBounds( 20, 20, 150, 20 );
itemsSoldJLabel.setText( "Number of items sold:" );
contentPane.add( itemsSoldJLabel ); // set up itemsSoldJTextField
itemsSoldJTextField = new JTextField();
itemsSoldJTextField.setBounds( 170, 20, 90, 20 );
itemsSoldJTextField.setHorizontalAlignment( JTextField.RIGHT );
contentPane.add( itemsSoldJTextField ); // set up priceJLabel
priceJLabel = new JLabel();
priceJLabel.setBounds( 20, 55, 130, 20 );
priceJLabel.setText( "Price of items:" );
contentPane.add( priceJLabel ); // set up priceJTextField
priceJTextField = new JTextField();
priceJTextField.setBounds( 170, 55, 90, 20 );
priceJTextField.setHorizontalAlignment( JTextField.RIGHT );
contentPane.add( priceJTextField ); // set up grossSalesJLabel
grossSalesJLabel = new JLabel();
grossSalesJLabel.setBounds( 20, 90, 80, 20 );
grossSalesJLabel.setText( "Gross sales:" );
contentPane.add( grossSalesJLabel ); // set up grossSalesJTextField
grossSalesJTextField = new JTextField();
grossSalesJTextField.setBounds( 170, 90, 90, 20 );
grossSalesJTextField.setEditable( false );
grossSalesJTextField.setHorizontalAlignment(
JTextField.RIGHT );
contentPane.add( grossSalesJTextField ); // set up commissionJLabel
commissionJLabel = new JLabel();
commissionJLabel.setBounds( 20, 130, 110, 20 );
commissionJLabel.setText( "Commission (%):" );
contentPane.add( commissionJLabel ); // set up commissionJSpinner
commissionJSpinner = new JSpinner(
new SpinnerNumberModel( 1, 1, 10, 1 ) );
commissionJSpinner.setBounds( 170, 130, 90, 20 );
contentPane.add( commissionJSpinner );
commissionJSpinner.addChangeListener( new ChangeListener() // anonymous inner class
{
// event handler called when value in
// commissionJSpinner changes
public void stateChanged( ChangeEvent event )
{
commissionJSpinnerStateChanged( event );
} } // end anonymous inner class ); // end call to addChangeListener
// set up earningsJLabel
earningsJLabel = new JLabel();
earningsJLabel.setBounds( 20, 170, 90, 20 );
earningsJLabel.setText( "Earnings:" );
contentPane.add( earningsJLabel );
// set up earningsJTextField
earningsJTextField = new JTextField();
earningsJTextField.setBounds( 170, 170, 90, 20 );
earningsJTextField.setEditable( false );
earningsJTextField.setHorizontalAlignment( JTextField.RIGHT );
contentPane.add( earningsJTextField ); // set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds( 170, 205, 90, 25 );
calculateJButton.setText( "Calculate" );
contentPane.add( calculateJButton );
calculateJButton.addActionListener( new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed( ActionEvent event )
{
calculateJButtonActionPerformed( event );
} } // end anonymous inner class ); // end call to addActionListener // set properties of application's window
setTitle( "Sales Commission Calculator" ); // set window title
setSize( 285, 285 ); // set window size
setVisible( true ); // show window } // end method createUserInterface // calculate and display sales and earnings
private void calculateEarnings()
{
// get user input
int items = Integer.parseInt( itemsSoldJTextField.getText() );
double price = Double.parseDouble( priceJTextField.getText() );
Integer integerObject =
( Integer ) commissionJSpinner.getValue();
int commissionRate = integerObject.intValue();
// calculate total sales and earnings
double sales = items * price;
double earnings = ( sales * commissionRate ) / 100;
// display the results
DecimalFormat dollars = new DecimalFormat( "$0.00" );
grossSalesJTextField.setText( dollars.format( sales ) );
earningsJTextField.setText( dollars.format( earnings ) ); } // end method calculateEarnings
private void commissionJSpinnerStateChanged( ChangeEvent event )
{
calculateEarnings();
}
private void calculateJButtonActionPerformed( ActionEvent event )
{
calculateEarnings();
} // main method
public static void main( String[] args )
{
SalesCommissionCalculator application =
new SalesCommissionCalculator();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main} // end class SalesCommissionCalculator
我以前的版本是1.4.1_02,还是我教材光盘自带的安装版本,晕死