Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners write simple java code
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


chean
write simple java code
2 Answer(s)      5 years and 2 months ago
Posted in : Java Beginners



Advertisement
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
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... in green color Help me pls :(   Hi Friend, Try the following code
Advertisements
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
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
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
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
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: 1)form.jsp: <html> <form name="form
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
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
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
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
Simple FTP upload in Java
Simple FTP upload in Java  How to write a program for Simple FTP upload in Java? Any code example of Simple FTP upload in Java will be very helpful for me. Thanks
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
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
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
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
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
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
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
Simple Java Projects
Simple Java Projects  Hi, I am beginner in Java and trying to find Simple Java Projects to learn the different concepts on Java. Can anyone tell me where to get it? Thanks
Simple Java Calculator - Java Beginners
Simple Java Calculator  Write a Java program to create simple.... Write proper code for making this calculator operational. Students..., Please visit the following link: http://www.roseindia.net/java/example/java
simple calculator - Java Beginners
simple calculator  how can i create a simple calculator using java codes?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
How to Write to a File in Java
In this Java tutorial, we will demonstrate how to write to a file in Java with the help of a sample Java program. After going though this tutorial you... to a file from Java program. To write the Java Write To File we will use two
 

 

 

DMCA.com