Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why will this gui window not close

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why will this gui window not close

    package newestlatestgui;
     
    import java.awt.*;
    import java.awt.event.*;
     
     
     
        public class NewestLatestGui extends Frame implements WindowListener {
            public GUIFrame(String title) {
                super(title);
                setBackground(SystemColor.control);
     
                addWindowListener(this);
            }
     
            public void setVisible(boolean visible) {
                if (visible) {
                    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
                    setLocation((d.width-getWidth())/2, (d.height-getHeight())/2);
     
                }
     
                super.setVisible(visible);
            }
        }
     
        public void windowClosing(WindowEvent p1) {
            dispose();
            System.exit(0);
        }
     
        public void windowDeactivated(WindowEvent p1) {}
        public void windowClosed(WindowEvent p1) {}
        public void windowDeiconfied(WindowEvent p1) {}
        public void WindowOpened(WindowEvent p1) {}
        public void windowIconified(WindowEvent p1) {}
        public void windowActivated(WindowEvent p1) {}
     
     
     
    }

     
    package newestlatestgui;
     
     
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class SimpleFrameTest {
     
        public static void main(String argsp[]) {
            Frame frame = new Frame("Simple Frame Test");
            frame.setSize(400,300);
            frame.setVisible(true);
        }
     
    }

    Why won't the window close?

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Why will this gui window not close

    Why won't the window close?
    You need to add window listener

     public static void main(String argsp[]) {
            Frame frame = new Frame("Simple Frame Test");
            frame.setSize(400, 300);
            frame.setVisible(true);
     
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    // Dispose the window after the close button is clicked.
                    frame.dispose();
                }
            });
        }
    Whatever you are, be a good one

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,398
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: Why will this gui window not close

    Where is an instance of the NewestLatestGui class created or used?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Aug 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why will this gui window not close

    Thank you John Joe

  5. #5
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Why will this gui window not close

    Quote Originally Posted by rostaryms View Post
    Thank you John Joe
    Welcome
    Whatever you are, be a good one

Similar Threads

  1. Verification Window "Do you really want to close?"
    By Wolfone in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 29th, 2013, 06:08 AM
  2. Replies: 2
    Last Post: August 27th, 2012, 09:19 PM
  3. SWING GUI CONSOLE WINDOW DISPLAY
    By Khadafi in forum Java Theory & Questions
    Replies: 5
    Last Post: January 10th, 2012, 08:15 AM
  4. Adding an array of shapes to a GUI window
    By cslx99 in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2011, 05:03 AM
  5. Im trying to open a new GUI window by clicking on a button
    By amzwans in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 10th, 2011, 11:33 PM

Tags for this Thread