使用输入值执行计算 - Java 小程序

use input value to perform calculation - Java applet

我正在编写一个 Java 小程序,用于编程 class 来计算学费。我想(不,我知道)我试图让 ActionListener 获取输入值,然后使用该值执行计算,这超出了我的报道范围。代码如下。这是家庭作业,所以我不是在寻找一堆代码 - 我只是想指出正确的方向,了解如何获取用户输入到 "numCredits" 文本字段中的值并纳入学费计算公式。谢谢!

/**
 * Calculate tuition at NHCC based on 
 * number of credits and instruction type.
 * 
 * by Jodi Rehlander
 * version 1.0, 9/7/15 for CSci1130-51
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tuition4 extends JApplet implements ActionListener
{
Image nhccImg;
ImageIcon nhccIcon;
JLabel credits, cType, nhcc, title; 
JTextField numCredits;
JButton online, standard, nursing;
JTextArea tType;
String inputCredits;
int numCreditsInt = 0;
double totalOnline;
double totalStandard;
double totalNursing;


    //define buttons, add listeners, display the buttons in the applet
    public void init( )
    {
        getContentPane( ).setBackground( Color.WHITE );

        setLayout( new BorderLayout( ) );

        nhccImg = getImage( getCodeBase( ), "NHCCLOGO.png" );
        nhccIcon = new ImageIcon( nhccImg );
        nhcc = new JLabel (nhccIcon);
        credits = new JLabel( "How many credits?" );
        cType = new JLabel( "\n What kind of classes?" );
        online = new JButton( "Online" );
        standard = new JButton( "Standard" );
        nursing = new JButton( "Nursing" );
        numCredits = new JTextField( "",3 );
        inputCredits = numCredits.getText( );
        tType = new JTextArea( 12,14 );
        tType.setEditable(false);
        addListeners( );
        add( nhcc, BorderLayout.WEST );
        JPanel pane = new JPanel( new FlowLayout( ) );
        pane.add( credits );
        pane.add( numCredits );
        pane.add( cType );
        pane.add( online );    
        pane.add( standard );     
        pane.add( nursing );
        pane.add( tType );
        add( pane, BorderLayout.CENTER );
    }


     public void addListeners( )
    {
        numCredits.addActionListener( this );
        online.addActionListener( this );
        standard.addActionListener( this );
        nursing.addActionListener( this );
    }



    public void actionPerformed( ActionEvent ae)
    {
        Object obj = ae.getSource( );
            if( obj == online )
            {
                numCreditsInt = Integer.parseInt(numCredits.getText( ) );
                tType.setText( "\n \n Tuition type: Online \n \n Cost/Credit: 7.96 \n Fees: .35 \n Textbooks: 0.00 \n \n Total Cost: " + "$"+( totalOnline ) );
            }
            else if ( obj == standard )
            {
                tType.setText( "Tuition type: Standard \n Cost/Credit: 5.08 \n Fees: .35 \n Parking: .20 \n Textbooks: 0.00 \n Total Costs: $xxx.xx \n" );
            }
            else if ( obj == nursing )
            {
               tType.setText( "Tuition type: Standard \n Cost/Credit: 9.78 \n Fees: .35 \n Parking: .20 \n Textbooks: 0.00 \n Total Costs: $xxx.xx \n" );
            }
            revalidate( );
            repaint( );
    }

    public double calculateOnlineTuition(int numCredits, double rate, double tuition, double fees, 
       double textbooks)
    {
        //calculate online tuition
        //numCredits = Integer.parseInt(inputCredits);
        rate = 177.96;
        fees = 14.35;
        textbooks = 275.00;
        tuition = numCreditsInt*rate;
        totalOnline = tuition + fees + textbooks;
        return totalOnline;

        //calculate Standard and Nursing rates once Online works
    }


    public void paint(Graphics g)
    {
        super.paint (g);

        Font heading = new Font( "Monospaced", Font.BOLD, 14 );
        Font small = new Font( "Monospaced", Font.PLAIN, 12 );
        Font smallItalic = new Font( "Monospaced", Font.ITALIC, 12 );


        g.setFont( heading );
        g.drawString("FEE BREAKDOWN:", 272, 375);
        g.setFont( small );
        g.drawString("Technology Fee    .00", 272, 390);
        g.drawString("Student Life Fee  .00", 272, 405);
        g.drawString("MSCSA Fee         [=11=].35", 272, 420);
        g.drawString("Health Svcs Fee   .00", 272, 435);
        g.drawString("*Parking          .20", 272, 450);
        g.setFont( smallItalic );
        g.drawString("*Parking not included", 280,485 );
        g.drawString("in online rate", 300,501);

    }
}

首先,检查numCredits字段是否有值,没有输出就没有任何计算意义。

接下来根据所选操作收集 calculateOnlineTuition 方法的要求参数(顺便说一下,这有点像专用的 class)。

numCredits不需要ActionListener,因为只有按钮才有意义。此外,将 tuition 传递给 calculateOnlineTuition 毫无意义,因为这是您要计算的值,最好让该方法简单地 return 您想要的值

举个例子:

public void actionPerformed(ActionEvent ae) {
    Object obj = ae.getSource();

    if (!numCredits.getText().trim().isEmpty()) {
        numCreditsInt = Integer.parseInt(numCredits.getText());
        double rate = 0;
        double fees = 0;
        double books = 0;
        if (obj == online) {
            rate = 177.96;
            fees = 14.35;
            books = 250.0;
            tType.setText("\n \n Tuition type: Online \n \n Cost/Credit: 7.96 \n Fees: .35 \n Textbooks: 0.00 \n \n Total Cost: ");
        } else if (obj == standard) {
            rate = 165.08 + 3.20; //?
            fees = 14.35;
            books = 350.0;
            tType.setText("Tuition type: Standard \n Cost/Credit: 5.08 \n Fees: .35 \n Parking: .20 \n Textbooks: 0.00 \n Total Costs: ");
        } else if (obj == nursing) {
            rate = 189.78 + 3.20; //?
            fees = 14.35;
            books = 600.0;
            tType.setText("Tuition type: Standard \n Cost/Credit: 9.78 \n Fees: .35 \n Parking: .20 \n Textbooks: 0.00 \n Total Costs: ");
        }

        double total = calculateOnlineTuition(numCreditsInt, rate, fees, books);
        tType.append(NumberFormat.getCurrencyInstance().format(total) + "\n");

    }

    revalidate();
    repaint();
}

public double calculateOnlineTuition(int numCredits, double rate, double fees,
                double textbooks) {
    //calculate online tuition
    //numCredits = Integer.parseInt(inputCredits);
    rate = 177.96;
    fees = 14.35;
    textbooks = 275.00;
    double tuition = numCreditsInt * rate;
    totalOnline = tuition + fees + textbooks;
    return totalOnline;

    //calculate Standard and Nursing rates once Online works
}