write simple java code
View Answers
November 12, 2009 at 10:57 AM
Hi Friend,
Try the following code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;
class Form extends JFrame {
JButton ADD,SUB,MUL,DIV;
JPanel panel1,panel2;
JLabel label1,label2,label3;
final JTextField text1,text2,text3;
int cal;
Form() {
label1 = new JLabel();
label1.setText("Enter First Number:");
text1 = new JTextField(20);
label2 = new JLabel();
label2.setText("Enter Second Number:");
text2 = new JTextField(20);
label3 = new JLabel("Result");
text3 = new JTextField(20);
text3.setText("");
ADD=new JButton("+");
SUB=new JButton("-");
MUL=new JButton("*");
DIV=new JButton("/");
panel1=new JPanel(new GridLayout(3,2));
panel2=new JPanel(new GridLayout(1,4));
panel1.add(label1);
panel1.add(text1);
panel1.add(label2);
panel1.add(text2);
panel1.add(label3);
panel1.add(text3);
panel2.add(ADD);
panel2.add(SUB);
panel2.add(MUL);
panel2.add(DIV);
add(panel1,BorderLayout.NORTH);
add(panel2,BorderLayout.CENTER);
setTitle("Calculate");
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String expression = "^[-+]?[0-9]*\\.?[0-9]+$";
CharSequence inputStr1 = value1;
CharSequence inputStr2 = value2;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr1);
Matcher matcher1 = pattern.matcher(inputStr2);
if((matcher.matches())&&(matcher1.matches())){
int n1=Integer.parseInt(value1);
int n2=Integer.parseInt(value2);
cal=n1+n2;
System.out.println(cal);
String res=Integer.toString(cal);
text3.setText(res);
}
else{
JOptionPane.showMessageDialog(null, "Please enter only numbers", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
November 12, 2009 at 10:57 AM
continue.........
SUB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String expression = "^[-+]?[0-9]*\\.?[0-9]+$";
CharSequence inputStr1 = value1;
CharSequence inputStr2 = value2;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr1);
Matcher matcher1 = pattern.matcher(inputStr2);
if((matcher.matches())&&(matcher1.matches())){
int n1=Integer.parseInt(value1);
int n2=Integer.parseInt(value2);
cal=n1-n2;
System.out.println(cal);
String res=Integer.toString(cal);
text3.setText(res);
}
else{
JOptionPane.showMessageDialog(null, "Please enter only numbers", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
MUL.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String expression = "^[-+]?[0-9]*\\.?[0-9]+$";
CharSequence inputStr1 = value1;
CharSequence inputStr2 = value2;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr1);
Matcher matcher1 = pattern.matcher(inputStr2);
if((matcher.matches())&&(matcher1.matches())){
int n1=Integer.parseInt(value1);
int n2=Integer.parseInt(value2);
cal=n1*n2;
System.out.println(cal);
String res=Integer.toString(cal);
text3.setText(res);
}
else{
JOptionPane.showMessageDialog(null, "Please enter only numbers", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
DIV.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String expression = "^[-+]?[0-9]*\\.?[0-9]+$";
CharSequence inputStr1 = value1;
CharSequence inputStr2 = value2;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr1);
Matcher matcher1 = pattern.matcher(inputStr2);
if((matcher.matches())&&(matcher1.matches())){
int n1=Integer.parseInt(value1);
int n2=Integer.parseInt(value2);
cal=n1/n2;
System.out.println(cal);
String res=Integer.toString(cal);
text3.setText(res);
}
else{
JOptionPane.showMessageDialog(null, "Please enter only numbers", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
text1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
text2.setText("");
text3.setText("");
}
});
}
}
class ArithmeticOperations
{
public static void main(String arg[])
{
try
{
Form frame=new Form();
frame.setSize(300,120);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}
Thanks
Related Tutorials/Questions & Answers:
write simple java code - Java Beginnerswrite simple java code
Write a program that creates a
simple calculator .The user enters two numbers in the text fields, Number 1 and Number 2... the following
code:
import java.awt.*;
import javax.swing.*;
import
help to write java codehelp to
write java code
write a full
code to produce a system will calculate all items to get total carry-marks which are 60 marks. and get sum of assignment 1,assignment 2, midterms-test and lave work to get total marks
Advertisements
help to write java codehelp to
write java code
write a full
code to produce a system will calculate all items to get total carry-marks which are 60 marks. and get sum of assignment 1,assignment 2, midterms-test and lave work to get total marks
How to write a simple java appletHow to
write a
simple java applet Hi, how can i
write a
simple java applet, displaying text in specific colors and font style.
For example... the following
code:
import java.awt.*;
import java.applet.*;
public class
Simple ATM Java Code...Simple ATM
Java Code... You are required to
write a Graphical User Interface that simulates an ATM by building on the program
you wrote.... It's a college assignment and needs to be written with the least amount of
code simple code - Java Beginnerssimple code to input a number and check wether it is prime or not and print its position in prime nuber series. Hi friend,
Code to help in solving the problem :
import java.io.*;
class PrimeNumber {
public
Help me to write this simple java frame program Help me to
write this
simple java frame program I want to
write a
Java program for....
To create a frame with 4 text fields;name,street,city and pin;with suitable labels.
Also add a button "OK". After enter values
How to write a session code - Java BeginnersHow to
write a session code
Once Again Thanks Deepak...Thanks for continuing responce
I want using session in my project plz help me how to
write a session
code
plz
write a session
code and post answer my personal id
How to write a error.jsp code - Java BeginnersHow to
write a error.jsp code Thanks once again
I have a session
code pls help me that how can i
write a error.jsp page.
why use error.jsp...let me know that hw can i
write the error.jsp
code.
plz send me error.jsp
code write java code discount bookstore - Java Beginnerswrite java code discount bookstore The question
Cik Mat operates... with special discounts, while others are sold at normal price.
Write a program which... if the book is sold at normal price).
Write appropriate constructors
WRITE a simple JSPWRITE a
simple JSP
Write a JSP that accepts a string parameter from the browser and simply displays it back in the HTML response
Hi Friend,
Try the following
code:ADS_TO_REPLACE_1
1)form.jsp:
<html>
<form
write code in c#write code in c# sir
I want to
write a save and save as
code in the c#(windosForm).Please help me
Write JQUERY CodeWrite JQUERY Code Hi,
Iam swathi.I created the table in the below format.can u please
write the jquery
code of this table..and my requirement... please send me the
code through jquery..?
Thank you
Swathi
Write JQUERY CodeWrite JQUERY Code Hi,
Iam swathi.I created the table in the below format.can u please
write the jquery
code of this table..and my requirement... please send me the
code through jquery..?
Thank you
Swathi
Can i write JavaScript code in AjaxResponse Code ?Can i
write JavaScript
code in AjaxResponse
Code ? Hai Every Dynamic's
We can't
write JavaScript
code in Ajax Response Code.Why because it takes only html,json,xml response.I tried a lot to create js form in ajax response.It
Run a simple EJB codeRun a
simple EJB code I found the
code this. However, as I have no idea with EJB, I can't understand how to run it. Can anybody help me by giving... is acceptable). Thanks.
Please visit the following link:
Simple EJB
Run a simple EJB codeRun a
simple EJB code I found the
code this. However, as I have no idea with EJB, I can't understand how to run it. Can anybody help me by giving... is acceptable). Thanks.
Please visit the following link:
Simple EJB
How to write the code for date in swings - StrutsHow to
write the
code for date in swings Hi Friends,
I want to
code for display the calendar.......technologies use only swing and core
java also how to display calendar like that popup window.....please
write and send me
simple code for XML database in JSsimple code for XML database in JS Sir ,
i want a
code in javascript for XML database to store details (username and password entered by user during registration process (login process)).
please send me a
code .
Thank you
how to write a programm in C for included code belowhow to
write a programm in C for included
code below Here is my question 'how to
write a program in C that runs your sql-xml application session. In the session, you can run SQL queries interactively. The query results
simple ajax Request and Response code...simple ajax Request and Response
code... var request=null;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
if(request
How to Write to a File in Java without overwritingLearn how to
Write to a File in
Java without overwriting in your
Java
program
This tutorial teaches you how to
write to a file in
Java without overwriting..._TO_REPLACE_6
In the above program you have learned how to
write code to to append
software or i have to write a code for this suffconvert windows picture or fax viewer file into ms word or pdf sir
i want to convert windows picture or fax viewer file into ms word or pdf so for this is i have to take help from the
java code or any software is present
Java write to fileJava write to file How to
write to a file in
Java?
Is there any good example
code here?
Thanks
Hi,
Java is one of the best... files.
You can easily use the the FileWriter and BufferedWriter to
write data
How to Write to a File in JavaIn this
Java tutorial, we will demonstrate how to
write to a file in
Java... to a file from
Java program.
To
write the
Java Write To File we will use two... and execute it, this will
write "Hello
Java" into out.txt file. While creating
How to write in File in JavaHow to
write in File in Java Hi,
How to
write in File in
Java. Please suggest and give example of this program.
thanks
write java prgramwrite java prgram
write java program to offer a menu which contains 3 choices to make calculation on 3 shapes.use method for each calculation