Abdullah Al- Mamun
Event Handling
1 Answer(s)      3 years and 3 months ago
Posted in : Java Beginners

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;




public class Calculator extends JFrame
{
JLabel lblNumber=new JLabel("Number");
JLabel lblSquareRoot=new JLabel("Square Root");
JLabel lblCubicRoot=new JLabel("Cuboc Root");
JLabel lblRound=new JLabel("Round");

JTextField txtNumber=new JTextField();
JTextField txtSquareRoot=new JTextField();
JTextField txtCubicRoot=new JTextField();
JTextField txtRound=new JTextField();

JButton btnClose=new JButton("Close");
JButton btnClear=new JButton("Clear");


public Calculator()
{
Container c=getContentPane();
c.setLayout(new GridLayout(0,2));
c.add(lblNumber);
c.add(txtNumber);
c.add(lblSquareRoot);
c.add(txtSquareRoot);
c.add(lblCubicRoot);
c.add(txtCubicRoot);
c.add(lblRound);
c.add(txtRound);
c.add(btnClose);
c.add(btnClear);

pack();
setVisible(true);


txtNumber.addKeyListener(new KeyListener()
{
public void keyReleased(KeyEvent ke);
{
double number=Double.parseDouble(txtNumber.getText());
txtSquareRoot.setText(""+Math.sqrt(number));
txtCubicRoot.setText(""+Math.cbrt(number));
txtRound.setText(""+Math.round(number));
}


public void keyPressed(KeyEvent ke);
{

}


public void keyTyped(KeyEvent ke);
{

}
});




btnClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae);
{
System.exit(0);
}
});







btnClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae);
{
txtNumber.setText("");
txtSquareRoot.setText("");
txtCubicRoot.setText("");
txtRound.setText("");
}
});


txtSquareRoot.setEditable(false);
txtCubicRoot.setEditable(false);
txtRound.setEditable(false);
}



public static void main(String args[])
{
Calculator calculator=new Calculator();
}
}
View Answers

March 17, 2010 at 10:32 AM


Hi Friend,

Try the following code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;

public class Calculator extends JFrame{
JLabel lblNumber=new JLabel("Number");
JLabel lblSquareRoot=new JLabel("Square Root");
JLabel lblCubicRoot=new JLabel("Cuboc Root");
JLabel lblRound=new JLabel("Round");

JTextField txtNumber=new JTextField();
JTextField txtSquareRoot=new JTextField();
JTextField txtCubicRoot=new JTextField();
JTextField txtRound=new JTextField();

JButton btnClose=new JButton("Close");
JButton btnClear=new JButton("Clear");

public Calculator(){
Container c=getContentPane();
c.setLayout(new GridLayout(0,2));
c.add(lblNumber);
c.add(txtNumber);
c.add(lblSquareRoot);
c.add(txtSquareRoot);
c.add(lblCubicRoot);
c.add(txtCubicRoot);
c.add(lblRound);
c.add(txtRound);
c.add(btnClose);
c.add(btnClear);

pack();
setVisible(true);

txtNumber.addKeyListener(new KeyListener(){
public void keyReleased(KeyEvent ke){
double number=Double.parseDouble(txtNumber.getText());
txtSquareRoot.setText(""+Math.sqrt(number));
txtCubicRoot.setText(""+Math.cbrt(number));
txtRound.setText(""+Math.round(number));
}
public void keyPressed(KeyEvent ke){
}
public void keyTyped(KeyEvent ke){
String input=txtNumber.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Please enter only numbers");
}
}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
});
btnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
txtNumber.setText("");
txtSquareRoot.setText("");
txtCubicRoot.setText("");
txtRound.setText("");
}
});
txtSquareRoot.setEditable(false);
txtCubicRoot.setEditable(false);
txtRound.setEditable(false);
}
public static void main(String args[]){
Calculator calculator=new Calculator();
}
}

Thanks









Related Pages:
event handling
event handling  diff btwn event handling in ASP.net and in HTML
handling Button Event in iphone
handling Button Event in iphone  handling Button Event in iphone
Event handling on an image
Event handling on an image  I want to divide an image into frames and want to do event handling on the frames
Java event handling
Java event handling  Which java.util classes and interfaces support event handling
Java event handling
Java event handling  What event results from the clicking of a button
javascript event handling examples
javascript event handling examples  javascript event handling examples   var handleClick = function(e) { // Older IEs set the `event... event listeners. document.attachEvent('onclick', handleClick); } else
Java event handling
Java event handling  What is the purpose of the enableEvents() method
event handling in jsp
event handling in jsp  i am developing a web application. i am getting a problem in event handing in jsp. in this web application there is a file... to generate event on each row of retrieved row from the database
KEY EVENT HANDLING
KEY EVENT HANDLING  I am trying to write a program that receives every key stroke even when the window is not active or it's minimized. Is there anyway to do
Event handling - Java Beginners
Event handling  Hi, This is a similar kind of question i had asked before, with a little difference. Two JCombobox and a JtextField. When I select an Item from the first Combo(i.e.,Select,First,Second or Third),the set
Event Handling - Java Beginners
Event Handling  import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame { JLabel lblNumber=new JLabel("Number"); JLabel lblSquareRoot=new JLabel("Square Root
JComboBox Event Handling - Java Beginners
JComboBox Event Handling  Hi, I have problem with event handling here.There are Two JComboBox and a JTextField.When I select an item in first combo(Select,First,Second in the below program) the items in the second combo change
Understanding the jQuery event handling mechanism
Understanding the jQuery event handling mechanism... handling mechanism First of all , you need to know -What is an event... these event. Event handling methods basically associates an event with 
What are events? Explain how Event handling in Java?
What are events? Explain how Event handling in Java?  What are events? Explain how Event handling in Java?   Hi, The Events..., and introduces us to AWT event handling. For Details about Event Handling in Java
how to add audio clip in swings with event handling
how to add audio clip in swings with event handling  hello dear, i wanna to play audio clip in java program in JFrame in swings by event handling ,it should be like when i click on a button , audio clip should play , plz tell me
J2ME Event Handling Example
J2ME Event Handling Example       In J2ME programming language, Event Handling are used to handle certain... event like selecting an item from a list And low level events like pressing a key
Handling Key Press Event in Java
Handling Key Press Event in Java   ... the handling key press event in java. Key Press is the event is generated when you press any key to the specific component. This event is performed by the KeyListener
delegation event model
delegation event model  What is delegation event model?   delegation event model In the event-delegation model, specific objects are designated as event handlers for GUI components. These objects implement event
Handling Mouse Clicks in Java
Handling Mouse Clicks in Java       Introduction In this section, you will learn about handling the mouse click event in the awt application. This program simply implements
Event handling in Java AWT
Event handling in Java AWT     ... related to the event handling through this example and used methods through which you can implement the event driven application. This example shows you how to handle
how to open a web page using proxy in java and handling button event in that web page..?
how to open a web page using proxy in java and handling button event in that web page..?  Sir, what i need is a code which uses proxy address and opens a web page i.e for example and then clicks a "yes" button in that page
Event on Slide bar In Java
Event on Slide bar In Java       Introduction In this section, you will learn about the event handling for a swing component named Slide bar. This section provides a complete
Exception Handling
Exception Handling      ... exceptions are used for handling errors in programs that occurs during the program... will see that how the exception handling can be done in java program. This example
JSF Application
;   Immediate Event Handling Example Event Handling... application, which explains how to implement ?immediate? event handling in JSF. Immediate event handling is useful in cases where you do not need to validate an entire
Java : Exception Handling
Java : Exception Handling In this section we will discuss about  JAVA Exception Handling with simple example. Exception Handling : An exception is an event that create problem at the time of program execution. It disturb your
Exception Handling in JSP
Exception Handling in JSP          An exception is an event that occurs during the execution of a program and it disrupts the normal working of the instructions
Java - Event Listeners Example in Java Applet
Java - Event Listeners Example in Java Applet       Introduction The event... for helping in implementing event listeners are present in the java.awt.event.
Flex event
Flex event  Hi.... please tell me about How does Flex event system works? Thanks  Ans: When event are triggered there are three...: In the capturing phase, Flex examines an event target?s ancestors in the display list to see
Event in flex
Event in flex  Hi..... can you tell me about that How do you make component participate in event mechanism? Thanks   Ans: Extend EventDispatcher or any subclass of it or implement IEventDispatcher
Event management
Event management   Hi, I want event management application like maintaining email notifications while task creation and update in broader way using spring java
Event in flex
Event in flex  Hi..... I want to know about What does calling preventDefault() on an event do? How is this enforced? please give me an example for that Thanks   Ans: The methods of the Event class can be used
Flex event
Flex event  Hi... please give me the answer with example What is event Bubbling? Thanks in advance  Event Bubbling: In the bubbling phase, Flex examines an event?s ancestors for event listeners. Flex starts
Flex event
Flex event  hi...... How to capture and event if the component is disabled? please give an example..... Thanks in advance
Flex event
Flex event  Hi.... Please tell me about How to create your own event in flex? Thanks
exception handling
exception handling  explain about exception handling
Event.mxml
Event.mxml         This tutorial will help you to learn event handling on mouse...("An event occurred.");   }   ]]></mx:Script>
Click event
Click event  hi............ how to put a click event on a particular image, so that it can allow to open another form or allow to display result in tabular form????????/ can u tell me how to do that????????/ using java swings
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  could you please tell me detail the concept of exception handling