Results 1 to 10 of 10
  1. #1
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Post GUI click exit problem

    hi..
    can any one heLp to this prob...

    here's the code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class JFrameWithPanel extends JFrame {

    JButton button = new JButton("Exit");

    public JFrameWithPanel() {
    super("JFrame with Panel and Button");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel Panel = new JPanel();
    Panel.add(button);
    setContentPane(Panel);
    }
    }
    and the other one:
    import javax.swing.*;

    public class JFrameWithPanelDemo {

    public static void main(String[] args) {
    JFrame aFrame = new JFrameWithPanel();
    aFrame.setSize(350, 90);
    aFrame.setVisible(true);
    }
    }
    the process is all complete
    but every time i click the exit in
    the frame.. it won't exit.. :(
    PLS help me to add some codes in exit...
    tnx

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You need to add an actionlistener to your button. At the moment it is doing nothing when you click the button because you haven't programmed it to do anything.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Because you didn't attach any event (listener) to the button click event.

    Java Code:
    button .addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });

  4. #4
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Default

    can u give me an example? :(

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ryamz View Post
    the process is all complete
    but every time i click the exit in
    the frame.. it won't exit.. :(
    PLS help me to add some codes in exit...
    tnx
    If you click the red cross button at the top right of the JFrame the application exits. If you press your JButton nothing happens, no matter if you label it "Exit" or "Quit" or whatever. Read about ActionListeners.

    kind regards,

    Jos

    edit: darn, much too slow again ;-)

  6. #6
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Default

    Because you didn't attach any event (listener) to the button click event.

    Code:

    button .addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    });
    but every time i put this it say's illegal start to type...

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by ryamz View Post
    can u give me an example? :(
    Amazingly enough the Tutorials can.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by ryamz View Post
    but every time i put this it say's illegal start to type...
    Then see the above reference tutorials (and actually read them don't just cut-n-paste code) so that you know how to use what it is you cut-n-pasted from that post, which you obviously don't know how, yet.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by ryamz View Post
    but every time i put this it say's illegal start to type...
    Just without copy and past what I've post, you must look at it first of all and find what it's really means and how it should used.

  10. #10
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. JButton click problem
    By mine0926 in forum NetBeans
    Replies: 7
    Last Post: 06-11-2010, 03:00 AM
  2. Problem with click to move ball algorithm
    By Laythe in forum Java Applets
    Replies: 1
    Last Post: 12-19-2009, 12:00 AM
  3. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 08:32 PM
  4. [SOLVED] On Exit JoptionPane problem
    By Mir in forum New To Java
    Replies: 24
    Last Post: 07-15-2008, 07:39 AM
  5. Problem in mouse click n repaint
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-04-2008, 11:16 AM

Posting Permissions

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