Thang Nguyen
Posts: 18
Nickname: thang
Registered: Apr, 2002
|
|
Re: TextField on an Applet
|
Posted: Apr 30, 2002 10:18 AM
|
|
Sorry for being to short. Here is my code
/* APPLET: This exercise has 2 text fields which take two integers from the user.The first number will divide the second one and the applet display the result. If zero is divided, a message is displayed, "Result: cannot divide zero!" . There is a button to perform the division. */ import java.applet.*; import java.awt.*; import java.awt.event.*;
public class DivideTwo extends Applet implements ActionListener { Label lblPromt = new Label("Enter values"); Label lblResult = new Label(); TextField tfNum1 = new TextField(5); TextField tfNum2 = new TextField(5); Button btnCalc = new Button("Divide"); public void init() { add(lblPromt); add(tfNum1); tfNum1.requestFocus(); //this seems to work fine from jview /a ..., // but not often when test on Debug /Start(F5) add(tfNum2); add(btnCalc); btnCalc.addActionListener(this); add(lblResult); } public void actionPerformed(ActionEvent e) { int iNum1, iNum2, iResult; iNum1 = Integer.parseInt(tfNum1.getText()); iNum2 = Integer.parseInt(tfNum2.getText()); iResult = iNum1 / iNum2; if (iNum2 == 0) { lblResult.setText("Result: Cannot divide zero!"); //lblResult.setLocation(30, 50); } else { lblResult.setText("Result: " + iResult); //lblResult.setLocation(30, 50); } } }
/* This applet runs fine but two problems I am facing to:
1) When iNum2 = 0, the message "Result: cannot divide zero!" does not appear!!! - It appear nothing, instead.
2) When iNum2 != 0, the message "Result: ..." is truncated!! ???
Thanks for our your help! -- Thang */
|
|