Home Answers Viewqa Java-Beginners write simple java code

 
 


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

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 Pages:
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
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
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
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
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 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 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
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 Java class program - Java Beginners
Simple Java class program  Hello, Please help me, Write a simple... your code Thanx  Hi import java.util.*; import.... http://www.roseindia.net/java/example/java/util
write spreadsheet
write spreadsheet  what is the java code to write a spreadsheet to database
write spreadsheet
write spreadsheet  what is the java code to write a spreadsheet to database
Simple Java class program - Java Beginners
Simple Java class program  Hi sir, Please help me Write a simple... your code and handle any errors that may arise. Thank you Alexander ...(); } } -------------------------------------------- Read for more information. http://www.roseindia.net/java
Java Write To File By Line
Java Write To File By Line In this tutorial you will learn how to write to file by line Write to a file by line using java you can use the newLine() method... this example : After doing the basic process to execute a java program write
Java Write To File CSV
Java Write To File CSV In this tutorial you will learn how to write to file... that is in a tabular form. In java to create a csv file is simple, you would require... a simple example which will demonstrate you how to write to csv file. To achieve
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
code
code    how to write this in java
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
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
Simple java program
Simple java program Hi this is the thing we got to do and i am totally confused on how to write this program it would be a great help i could be helped... marks up the prices of its items by 25%. Write a Java program that declares
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
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
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 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
Read Write
Read Write  Hi; How can I read certain line of say 10 text files and write to one text file   Java Read Multiple Files and store the data into another text file The given code reads all the text files of the directory
code
code  < comp xlink:type="new" xlink:actuate="onload"/> how to write this in java
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
simple team leader
simple team leader  Could you please help me to write a program for a simple team leader?   Please clarify your problem. Which type of program you want to create in java? Please specify
Simple Date Format Exception
Simple Date Format Exception       Simple Date Format Exception inherits from a package name... and normalization of date. Simple Date-format provides you to work on choosing any
code for java pattern
code for java pattern   * * * * * * * * please write the program of above pattern and do not use string tokennizer string buffer i mean to say that please use very simple method Thanks
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
Java Write To File From String
Java Write To File From String In this tutorial you will learn how to write to file from string. Write to file from string using java at first we will have... such as BufferedWriter, PrintWriter to write efficiently. Here I am going to give a simple example
Simple Java Question - Java Beginners
Simple Java Question  [color=#0040BF] Dear All, I have a huge text file with name animal.txt, I have the following sample data: >id1 lion... [/color]  Hi friend, Plz give full source code where you having
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
write a program - Java Beginners
write a program  1. write a program that reads a string composed of 6 words then generate and print a word composed of the first letters of the 6 words.  Hi friend, Code to solve the problem : class FirstLetter
java code - Java Beginners
java code  Write a program that performs flashcard testing of simple mathematical problems. Allow the user to pick the category. Repetitively display a problem and get the user?s answer. Indicate whether the user?s answer
Very simple `Hello world' java program that prints HelloWorld
Hello World Java Simple Java Program for beginners... step. This short example shows how to write first java application and compile... code in the Java interpreter with this command:  java HelloWorld
write program - Java Beginners
write program  write a java program that will read the values of matrix A and B give its multiplication in matrix C  Hi friend, Code...]); } System.out.println(); } } } For more information on java visit to : http
code for java pattern
code for java pattern  1 23 345 5678 891011 please write the program of above pattern and do not use string tokennizer string buffer i mean to say that please use very simple method Thanks
code for java pattern
code for java pattern  1 121 2342 45674 6789106 please write the program of above pattern and do not use string tokennizer string buffer i mean to say that please use very simple method Thanks
Simple Form in Java
Simple Form in Java       This is a simple program of java awt package which constructs a look like a form by using various java component. In this section, you will learn how
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
Applet Write Files Example
that you write it or not. Description of program: The following program or code...;TITLE> Write file example </TITLE> <applet code="... Applet Write Files Example     
Java Write To File Dom Document
Java Write To File Dom Document In this tutorial you will learn how to write to file dom document. Write to xml file you would be required to use the package.... Here I am giving a simple example which will demonstrate you how to write to xml