write simple java code

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 Beginners
write 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 code
help 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 code
help 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 applet
How 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 code to write an read and write the login detail to a xml file using javascript ( username and password )
simple code to write an read and write the login detail to a xml file using javascript ( username and password )  pls can nyone give me a code to write and read the login details (username and password )into a xml file using
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 Beginners
simple 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 Beginners
How 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 Beginners
How 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
Please write code for this program in java and Explain it ? pl'sssssssss
Please write code for this program in java and Explain it ? pl'sssssssss ... that were ever alive at one time. Write a method to compute this, given the above array of 2n integers. Write one of the following methods: C/C++: int
write java code discount bookstore - Java Beginners
write 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 JSP
WRITE 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
how to write code for this output?
how to write code for this output?   1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Write JQUERY Code
Write 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 Code
Write 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 code
Run 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 code
Run 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
Need someone to check and modify my simple code - Java Beginners
Need someone to check and modify my simple code   How to write a class that contains Variables that hold hourly rate of pay, number of hours...); } }   Hi Friend, You can use following code: import
Need simple java code to start and stop the remote windows service.
Need simple java code to start and stop the remote windows service.  Hi, I Need simple java code to start and stop the remote windows service. Example:There are two servers 1) Server A and 2) Server B. If a notepad.exe file
How to write jsp/servlet code to integrate LINKDIN?
How to write jsp/servlet code to integrate LINKDIN?  How integrate linkdin api's in java codding
How to write the code for date in swings - Struts
How 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
ModuleNotFoundError: No module named 'write-about-code'
ModuleNotFoundError: No module named 'write-about-code'  Hi, My... named 'write-about-code' How to remove the ModuleNotFoundError: No module named 'write-about-code' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'write-about-code'
ModuleNotFoundError: No module named 'write-about-code'  Hi, My... named 'write-about-code' How to remove the ModuleNotFoundError: No module named 'write-about-code' error? Thanks   Hi, In your
JAVA Write code which converts propositional expressions into clauses using the INOA rules
JAVA Write code which converts propositional expressions into clauses using the INOA rules  Write code which converts propositional expressions... are: or not p q or not p r Turn in: ΓΆβ?¬ΒΆ Printout of your code. ΓΆβ?¬ΒΆ Test
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
simple code for XML database in JS
simple 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 below
how 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 overwriting
Learn 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 suff
convert 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
Insert Image into Mysql Database through Simple Java Code
Insert Image into Mysql Database through Simple Java Code... simple java code that how save image into mysql database. Before running this java code you need to create data base and table to save image in same database
How to write the junit test code for the following controller code
How to write the junit test code for the following controller code  //Controller Code package com.payoda.springs; import java.util.ArrayList... "Volumereturn"; } } // Mock test code package com.payoda.test
write a java program
write a java program  write a program to print '*' in a circular form
pls provide steps to write ejb3.0 simple application in eclipse - EJB
pls provide steps to write ejb3.0 simple application in eclipse   hi, pls provide steps to create simple ejb3.0 application in eclipse .And also how to create entity bean ,session bean in ejb3.0
Java write to file
Java 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 Java
In 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 Java Program
How to write Java Program  how to write a program to find average of 5 student marks
How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java. Please suggest and give example of this program. thanks
how to write weighted moving average code using 2d-array
how to write weighted moving average code using 2d-array  how to write weighted moving average code using 2d-array
How to write Example code for CRUD application in hibernate annotation?
How to write Example code for CRUD application in hibernate annotation?  Learn Hibernate can be easy if anyone help me with the simple example code. How to write Example code for CRUD application in hibernate annotation? Share
write a programm using java
write a programm using java  print the following using java programming
code for a simple java program which performs basic arithmetic operation addition,subtraction,multiplication,division........
code for a simple java program which performs basic arithmetic operation addition,subtraction,multiplication,division........  code for a simple java program which performs basic arithmetic operation addition,subtraction
write java prgram
write java prgram  write java program to offer a menu which contains 3 choices to make calculation on 3 shapes.use method for each calculation
if my database(oracle) connection failure means wat is the code to write in exception ?
if my database(oracle) connection failure means wat is the code to write in exception ?  if my database(oracle) connection failure means wat is the code to write in exception
write the java program?
write the java program?  1 11 121 1331 14641 15101051
write the java program?
write the java program?  1 11 121 1331 14641 15101051

Ads