Results 1 to 6 of 6
  1. #1
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default Can't close JFrame

    I need a way to close my JFrame from my Mouse Adapter. System.exit(0); doesn't work in any method.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.imageio.*;
    import java.awt.Graphics.*;
    import java.io.*;
    import java.awt.image.*;
    import java.awt.Component.*;
    import java.*;
    import javax.*;
    import java.awt.event.*;
    public class StatxMenu extends JPanel{
        BufferedImage Back = ImageIO.read(new File ("StatMenu.png"));
        BufferedImage Skills = ImageIO.read(new File ("Skills.png"));
        BufferedImage System = ImageIO.read(new File ("System.png"));
        Dimension dim = new Dimension(Back.getWidth(), Back.getHeight());
        boolean bSkills = false, bSystem = false;
        public StatxMenu() throws IOException{
            setPreferredSize(dim);
            setFocusable(true);
            addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent me) {
                    if (me.getX()>390 && me.getY()<10){
                    }
                    repaint();
                }
            });
        }
        public void paint(Graphics g){
            g.drawImage(Back, 0, 0, this);
            g.drawImage(Skills, 20, 20, this);
            g.drawImage(System, 20, 515, this);
        }
        public static void main(String args[])throws IOException{
            JFrame frame = new JFrame();
            frame.getContentPane().add(new StatxMenu());
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    Thanks

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Custom painting is done by overriding the paintComponent() method NOT the paint() method of the JPanel.

    I don't see any code in your MouseAdapter where you attempt to close the frame so I don't know what your problem is. Update the posting to show the code you tested.

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    My most recent application distinguishes between
    • close the window
    • exit the application

    In principle, the application could launch multiple copies of the main window.
    So the application would exit only after all windows are closed.
    Each of the following code segments incorporates a wrinkle I had to ferret out to implement this approach.

    The window code is ready for a user click
    on the X icon on the title bar to close the window.
    In this case, the window is Disposed,
    and a "SHUTDOWN" event is sent (through a "comm" object).
    Java Code:
    	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    	addWindowListener(new WindowAdapter() {
    		@Override
    		public void windowClosing(WindowEvent we) {
    			comm.say(TaggerComm.ID.SHUTDOWN, null, "window close");
    		}
    	});

    There is also a CLOSE button within the main window.
    When the user clicks it, this code simulates a click on the X button.
    In consequence, this code will result in a call on the code just above.
    Java Code:
    	getToolkit().getSystemEventQueue()
            	.postEvent(new WindowEvent(TaggerFrame.this,
                    	WindowEvent.WINDOW_CLOSING));

    The main application waits for SHUTDOWN events.
    After all windows are closed, the application exits thusly:
    Java Code:
    	// arrange to exit after all listeners process the SHUTDOWN
    	java.awt.EventQueue.invokeLater(
    		new Runnable() { public void run() { System.exit(0); }}  );

  4. #4
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    In the
    Java Code:
     
    if (me.getX()>390 && me.getY()<10){
    }
    part, I am looking to put in a System.exit(0);, or a dispose(); if possible, but System."" commands aren't working in any of my methods, even including main. I can even use System.out.println(); in my main method, let alone my other ones. Sorry I wasn't clear on that.

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    I can even use System.out.println(); in my main method
    From the context it looks like you mean you CANNOT even use System.xxx.

    This could happen if somewhere on the path you have some class of your own called System.

    It may possibly help to remove the two erroneous lines
    Java Code:
    import java.*;
    import javax.*;
    It might also help to define a package for your code.
    By adding a "package xxx.xxx.xxx;" at the start of the code.
    (Choose values to replace xxx; perhaps local.dunafrothint.statinx)

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    part, I am looking to put in a System.exit(0);, or a dispose(); if possible, but System."" commands aren't working in any of my methods, even including main.
    Again the code you posted does not match the description of your problem.

    If you need more help post your SSCCE.

Similar Threads

  1. close to zero
    By nokicky in forum New To Java
    Replies: 6
    Last Post: 10-26-2009, 04:30 PM
  2. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  3. JFrame Conditional Close
    By hemanthjava in forum AWT / Swing
    Replies: 10
    Last Post: 06-29-2008, 07:49 AM
  4. How to close an open JFrame window from a jsp page?
    By kasisaiganesh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-27-2008, 06:29 PM
  5. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:33 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
  •