Click here to Skip to main content
15,888,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;
public class Frame3 extends JFrame implements ActionListener
{
  JLabel answer = new JLabel("");
  JPanel pane = new JPanel(); // create pane object
  JButton pressme = new JButton("Press Me");
  Frame3()   // the constructor
  {
    super("Event Handler Demo"); setBounds(100,100,300,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane(); // inherit main frame
    con.add(pane); pressme.setMnemonic('P'); // associate hotkey
    pressme.addActionListener(this);   // register button listener
    pane.add(answer); pane.add(pressme); pressme.requestFocus();
    setVisible(true); // make frame visible
  }
  // here is the basic event handler
  public void actionPerformed(ActionEvent event)
  {
    Object source = event.getSource();
    if (source == pressme)
    {
      answer.setText("Button pressed!");
      JOptionPane.showMessageDialog(null,"I hear you!","Message Dialog",
      JOptionPane.PLAIN_MESSAGE); setVisible(true);  // show something
    }
  }
  public static void main(String args[]) {new Frame3();}
}
Posted
Updated 13-Dec-14 4:23am
v2
Comments
Afzaal Ahmad Zeeshan 13-Dec-14 10:25am    
Isn't the question title and the source code assosiated with it a little irrelevant?
Richard MacCutchan 13-Dec-14 10:43am    
The formula for the area of a circle is pi * r * r. Where do you want to display the answer?

1 solution

Member 11308177 wrote:

Java
JOptionPane.showMessageDialog(null,"I hear you!","Message Dialog",
JOptionPane.PLAIN_MESSAGE); setVisible(true);  // show something
It reminds me the anecdote
One psychoanalyst asks another one:
— "Could you tell me how to calculate the area of a circle?"
— "Want to talk about it?"
Let me remind you: this is forum for software developers, engineers or students. Asking questions also assumes some prerequisites: an inquirer is supposed to be educated enough to be able to read texts on at least elementary mathematics and computing, perform Web search and make elementary conclusions from the material read, and to be able to logically bind the question with code sample posted. Please, grow to appropriate level (this is not so hard to do) and welcome back. Please, no offense; we would always be glad to help and wish you only the best.

Good luck. Hope to hear from you soon.
—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900