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



Home Answers Viewqa Java-Beginners write simple java code

Have Programming Question? Ask it here!
 
 
 


chean
write simple java code
2 Answer(s)      4 years and 9 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
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
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
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 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 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
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
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
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
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
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
Simple java applet
Simple java applet  Create a Java applet to display your address on the screen. Use different colors for background and text   Please visit the following link: Applet Examples
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
simple source code for tower of hanoi in android
simple source code for tower of hanoi in android  may I get a source code for tower of hanoi game in android pls.If possible help me as much as possible
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
query in simple code..i had described all...........
query in simple code..i had described all...........  SAME HERE IF YOU GET THIS PROBLEM SOLVED THEN PLEASE REPLY......... MY ONE JAVA FILE IS: abstract class A { abstract void callmetoo() { System.out.println("anoop
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... In the above program you have learned how to write code to to append the content
write a java program
write a java program  write a program to print '*' in a circular form
simple java search engine
simple java search engine  i have already downloaded the project simple java search engine.but i am not able to run it.can anyone help me.i have.... ABSTRACT Title : Simple Search Engine System Specification: The system on which
java spring simple application
java spring simple application  hai I have design a simple application in this I always found class not found exception. I am sendig code as follows please resolve this and send me.my directory structure is as follows Project
Simple Calculator Application In Java Script
Simple Calculator Application In Java Script  ... the basics of JavaScript and create your first JavaScript program. What is simple Calculator The objective of this project is  learn how to write a simple
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 Java Program
How to write Java Program  how to write a program to find average of 5 student marks
write a programm using java
write a programm using java  print the following using java programming
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
A simple question - Java Beginners
A simple question   Design a program to calculate either the square of a number or the square root of a number and display the results. output..., Code to help in solving the problem : import java.io.*; class Square
Simple IO Application - Java Beginners
Simple IO Application  Hi, please help me Write a simple Java application that prompts the user for their first name and then their last name. The application should then respond with 'Hello first & last name, what
Advertisements
 

 

 

DMCA.com