Results 1 to 5 of 5

Thread: frame closing

  1. #1
    manish007g is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Post frame closing

    import java.awt.*;
    import java.awt.event.*;
    class EventDemo implements ActionListener
    {
    Frame f;
    Button b, b1;
    TextField tf;

    EventDemo(String s)
    {
    f=new Frame(s);
    b=new Button("OK");
    b1=new Button("CANCEL");
    b.setBounds(20,100,50,50);
    b1.setBounds(20,180,50,50);
    b.addActionListener(this);
    b1.addActionListener(this);
    f.add(b);
    f.add(b1);
    tf=new TextField();
    tf.setBounds(20,40,100,40);
    tf.addKeyListener(new KeyEventListener());
    f.add(tf);
    f.addWindowListener(new WindowEventListener());
    f.addMouseListener(new MouseEventListener(this));
    f.setLayout(null);
    f.setSize(400,400);
    f.setVisible(true);
    }


    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==b)
    tf.setText("OK");
    if(e.getSource()==b1)
    tf.setText("CANCEL");
    }

    public static void main(String s[])
    {
    new EventDemo("event demo");
    }

    }


    class WindowEventListener extends WindowAdapter
    {

    public void WindowClosing(WindowEvent e1)
    {
    System.exit(0);
    }
    }

    class MouseEventListener extends MouseAdapter
    {
    EventDemo f;
    MouseEventListener(EventDemo f)
    {
    this.f=f;
    }
    public void mouseClicked(MouseEvent e1)
    {
    f.tf.setText("mouse event");
    f.f.setBackground(Color.RED);
    }
    }

    class KeyEventListener extends KeyAdapter
    {
    public void Keytyped(KeyEvent e)
    {
    System.out.println(e.getKeyChar());
    }
    }



    EVERYTHING CORRECT EXCEPT THE FRAME CLOSING ON CROSS BUTTON NOT WORKING ...... WHATS PROBLEMS . PLZ HELP

  2. #2
    gauravmanral's Avatar
    gauravmanral is offline Gaurav Manral
    Join Date
    Jul 2011
    Location
    Delhi
    Posts
    37
    Rep Power
    0

    Default Re: frame closing

    hmmm...
    use this...


    f.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
    System.exit(0);
    }
    });

  3. #3
    manish007g is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Re: frame closing

    yeah working .... thank u.

    but what was wrong in that

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: frame closing

    When AWT is initialized, it creates a non-daemon thread that keeps the JVM running and allowing the GUI to persist even if the main method ends. With AWT, you must explicitly tell the JVM to exit to close this thread, since the JVM will not exit as long as there's a non-daemon thread running.

    This is handled a little better with Swing where you can set your JFrame's default close behavior.

  5. #5
    gauravmanral's Avatar
    gauravmanral is offline Gaurav Manral
    Join Date
    Jul 2011
    Location
    Delhi
    Posts
    37
    Rep Power
    0

    Default Re: frame closing

    then u should use JFrame in place of Frame...

    and then use this -

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    but it is in swing.

Similar Threads

  1. Replies: 4
    Last Post: 01-18-2011, 08:22 AM
  2. how to open one frame to another frame
    By tukadiya in forum AWT / Swing
    Replies: 1
    Last Post: 12-20-2010, 08:27 PM
  3. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 10:40 AM
  4. Replies: 2
    Last Post: 05-28-2010, 02:48 PM
  5. Replies: 7
    Last Post: 11-02-2009, 06:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •