After validating user name and password using TextField in an application (application extends frame), let us rewrite the same program using applets but with a different functionality. We take two numbers from the user and display the sum of two numbers.
The following program comes with 3 text fields. In two text fields, user enters two numbers. In the third text field, the programmer displays their sum.
Example on TextField Applet Adding Two Numbers
Applet Program. File Name: TwoNumbers.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TwoNumbers extends Applet implements ActionListener
{
TextField firstNum, secondNum, resultNum;
public TwoNumbers()
{
setLayout(new GridLayout(3, 2, 10, 15));
setBackground(Color.cyan);
firstNum = new TextField(15);
secondNum = new TextField(15);
resultNum = new TextField(15);
secondNum.addActionListener(this);
add(new Label("Enter First Number")); add(firstNum);
add(new Label("Enter Second Number")); add(secondNum);
add(new Label("S U M")); add(resultNum);
}
public void actionPerformed(ActionEvent e)
{
String str1 = firstNum.getText();
double fn = Double.parseDouble(str1);
double sn = Double.parseDouble(secondNum.getText());
resultNum.setText("Sum is " + (fn+sn));
}
}
HTMLfile to run the above applet. File Name: TN.html
Pages: 1 2
how to put only 10 textfield boxes with one result textfield and with a submit button which will show the average of the first 10 numbers entered
How to W.A.P in java containing three textfield, 1st textfield accepts lastname and 2nd textfield accepts firstname and onclick of button full name is displayed in 3rd textfield ?
What is ur problem? I mean u are not able to understand.
Can you help me
i want to create an applet prog. In which there are three texrfield for number1,number2,result, One button for display and one list in which all arithmatic operator. When i press only one time a display buttun then result will be show in result textfield.
I got a ready made code for application. Just change to an applet.
// a program that uses a combination of buttons and text fields.
// to do arithmetic operations using buttons and displaying the result in text field.
import java.awt.*;
import java.awt.event.*;
public class Arith extends Frame implements ActionListener
{
// reference variables
TextField tf1, tf2, tf3; // 3 text fields
Button pb, mb1, mb2, mb3, db, eb; // 6 buttons
public Arith( )
{
// setting the layout
setLayout(new GridLayout(6, 2, 5, 5));
setBackground(Color.cyan);
// creating objects
tf1 = new TextField(10);
tf2 = new TextField(10);
tf3 = new TextField(10);
pb = new Button(“+”);
mb1 = new Button(“-“);
mb2 = new Button(“*”);
mb3 = new Button(“%”);
db = new Button(“/”);
eb = new Button(“EXIT”);
// event handling
pb.addActionListener(this);
mb1.addActionListener(this);
mb2.addActionListener(this);
mb3.addActionListener(this);
db.addActionListener(this);
eb.addActionListener(this);
// beautification
eb.setForeground(Color.red);
tf3.setEditable(false);
tf3.setFont(new Font(“Serif”, Font.BOLD,20));
// adding components, order is important
add(new Label(“Enter 1st Number”));
add(tf1);
add(new Label(“Enter 2nd Number”));
add(tf2);
add(pb); // adding buttons one by one
add(mb1);
add(mb2);
add(mb3);
add(db);
add(eb);
add(new Label(“Result”)); // adding last row
add(tf3);
// create the frame
setTitle(“Arithmetic”);
setSize(400,450);
setVisible(true);
}
// override the abstract method of AL
public void actionPerformed(ActionEvent e)
{
String s1 = tf1.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(tf2.getText());
// Object obj = e.getSource();
// Button btn = (Button) obj;
Button btn = (Button) e.getSource();
// getSource( ) returns the object of the button (in Object form).
String s2 = ” “; // a temporary local variable
if(btn == pb)
s2 = “sum = ” + (d1+d2);
// assign a value to s2
else if(btn == mb1)
s2 = “Difference = ” + Math.abs(d1-d2);
else if(btn == mb2)
s2 = “Product = ” + (d1*d2);
else if(btn == mb3)
s2 = “Remainder = “+ (d1%d2);
else if(btn == db)
s2 = “Quotient = ” + Math.round((d1/d2));
else if(btn == eb)
{
System.exit(0); // closes the current application
}
tf3.setText(s2); // place the result s2 in tf3
}
public static void main(String[] args)
{
new Arith();
}
}
I want to display Selected items of one listbox in other listbox. what will be its code?
import java.awt.*;
import java.awt.event.*;
public class Demo extends Frame implements ActionListener
{
List selectList, displayList;
public Demo()
{
setLayout(new GridLayout(2, 2, 10, 15));
selectList = new List(5, true);
displayList = new List(5, true);
selectList.add(“S N Rao”);
selectList.add(“way2java.com”);
selectList.add(“Hyderabad”);
selectList.add(“Andhra Pradesh”);
selectList.add(“India”);
selectList.addActionListener(this);
add(new Label(“Enter Here”)); add(selectList);
add(new Label(“Display Here”)); add(displayList);
setSize(300, 350);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String names[] = selectList.getSelectedItems();
displayList.removeAll();
for(int i = 0; i < names.length; i++) { displayList.add(names[i]); } } public static void main(String args[]) { new Demo(); } }
i need press Button throws answer in TextBox
Be more clear about question.
The code should be below
import java.awt.*;
import java.awt.event.*;
public class SumDemo extends Frame implements ActionListener
{
Button sum,reset;
Label l1,l2,l3;
TextField t1,t2,t3;
public SumDemo()
{
setTitle(“Button Example”);
setSize(400,200);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setLayout(new GridLayout(4,2,20,20));
sum = new Button(“Add”);
reset = new Button(“Reset”);
l1 = new Label(“Enter the 1st number”);
l2 = new Label(“Enter the 2nd number”);
l3 = new Label(“Result”);
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
setBackground(Color.pink);
t1.setBackground(new Color(200,200,200));
t2.setBackground(new Color(200,200,200));
t3.setBackground(new Color(200,200,200));
t3.setEditable(false);
sum.addActionListener(this);
reset.addActionListener(this);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(sum);
add(reset);
}
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
if(str.equals(“Add”))
{
String s1 = t1.getText();
String s2 = t2.getText();
int n1 = Integer.parseInt(s1);
int n2 = Integer.parseInt(s2);
int n = n1 + n2;
String s = String.valueOf(n);
t3.setText(s);
}
else if(str.equals(“Reset”))
{
t1.setText(“”);
t2.setText(“”);
t3.setText(“”);
}
}
public static void main(String args[])
{
new SumDemo();
}
}
What shall I do?
Excellent code..
but still have a query???
Hoe to put validations on the text feild,suppose if i enter string in the textfeild it should give error.but its not.plz reply
Question must be more clear. I give some options.
a) Suppose your enter a wrong character, like a special character or a digit which the password does not accept, it must give error immediately (not waiting for whole word). The easiest way is to use a dialog box here from Swing.
b) Entering all the characters and then validate. etc, etc.
SN Rao