我正在为编程课程编写一个Java applet来计算学费。我想(不,我知道)我试图让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: $177.96 \n Fees: $14.35 \n Textbooks: $250.00 \n \n Total Cost: " + "$"+( totalOnline ) );
}
else if ( obj == standard )
{
tType.setText( "Tuition type: Standard \n Cost/Credit: $165.08 \n Fees: $14.35 \n Parking: $3.20 \n Textbooks: $350.00 \n Total Costs: $xxx.xx \n" );
}
else if ( obj == nursing )
{
tType.setText( "Tuition type: Standard \n Cost/Credit: $189.78 \n Fees: $14.35 \n Parking: $3.20 \n Textbooks: $600.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 $8.00", 272, 390);
g.drawString("Student Life Fee $5.00", 272, 405);
g.drawString("MSCSA Fee $0.35", 272, 420);
g.drawString("Health Svcs Fee $1.00", 272, 435);
g.drawString("*Parking $3.20", 272, 450);
g.setFont( smallItalic );
g.drawString("*Parking not included", 280,485 );
g.drawString("in online rate", 300,501);
}
}
答案 0 :(得分:0)
首先,检查numCredits
字段是否有值,没有任何计算没有任何意义。
接下来根据所选行动收集calculateOnlineTuition
方法的必需参数(仅供参考,这是一个专门的课程)。
numCredits
不需要ActionListener
,因为只有按钮才有意义。此外,它是无意义的将tuition
传递给calculateOnlineTuition
,因为这是您尝试计算的值,最好让方法只返回您之后的值
举个例子:
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: $177.96 \n Fees: $14.35 \n Textbooks: $250.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: $165.08 \n Fees: $14.35 \n Parking: $3.20 \n Textbooks: $350.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: $189.78 \n Fees: $14.35 \n Parking: $3.20 \n Textbooks: $600.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
}