Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using java to create a program that displays a quiz as a GUI interface using the code below. However when I run the program I can not close the window using the usual close X button. How do I over come this?

What I have tried:

Java
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 

/**
 *
 * @author Joseph
 */
public class PartA extends Frame implements ActionListener, WindowListener
{
    private Button checkButton;//Button displaying "check"
    private Button resetButton;//Button displaying "reset"
    private Panel option1;
    private Panel option2;
    private Panel option3;
    public JCheckBox answer1;//first possible answer
    public JCheckBox answer2;//second possible answer
    public JCheckBox answer3;//third possible answer
    
    private String outputAns;//Displays whether answer is correct
    
    public PartA () {
        setLayout(new FlowLayout());
        
        Label header = new Label("123 Practice Assesment\n Question 1\n");
        add(header);//Declare and add title to interface
        
        Label question = new Label("In Disney's animated movie 'Frozen', what is the name of the snowman that comes to help");
        add(question);//Declare and add question to interface
        
        //Add text to possible answers
        answer1 = new JCheckBox("Olaf");
        answer2 = new JCheckBox("Snowlaf");
        answer3 = new JCheckBox("Yolaf");
        
        //add first answer to interface
        option1 = new Panel();
        option1.add(answer1);
        add(option1);
        
        //add second answer to interface
        option2 = new Panel();
        option2.add(answer2);
        add(option2);
        
        //add third answer to interface
        option3 = new Panel();
        option3.add(answer3);
        add(option3);
        
        //add check button to interface
        checkButton = new Button("Check");
        add(checkButton);
        checkButton.addActionListener(this);
        
        //add reset button to interface
        resetButton = new Button("Reset");
        add(resetButton);
        resetButton.addActionListener(this);
        
        setTitle("X123 Practice Assesment\n Question");//Set title of interface to "X123 Practice Assessment"
        setSize(1650, 1080);//Make interface full screen
        
        setVisible(true);//Open interface on run
    }
    
    
    
    @Override
    public void actionPerformed(ActionEvent evt) {
       if(evt.getSource() == checkButton){
           if((answer1.isSelected()) && (!answer2.isSelected()) && (!answer3.isSelected()))
           {
               outputAns = "That was the correct answer";
           }else
           {
               outputAns = "That was the incorrect answer";
           }
           
           JOptionPane.showMessageDialog(null, outputAns);
       }else
       {
           if(answer1.isSelected())
           {
               answer1.setSelected(false);
           }
           if(answer2.isSelected())
           {
               answer2.setSelected(false);
           }
           if(answer3.isSelected())
           {
               answer3.setSelected(false);
           }
       }
    }
    
    @Override
    public void windowDeactivated(WindowEvent we){
        System.exit(0);
    }

    @Override
    public void windowOpened(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowIconified(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowActivated(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

/**
 *
 * @author Joseph
 */
public class CWq5 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PartA partA = new PartA();
    }
    
}
Posted
Updated 14-Apr-16 0:11am
v2
Comments
Richard MacCutchan 14-Apr-16 3:33am    
What happens when you click the X? Have you checked with your debugger to see if your code terminates cleanly?

1 solution

Your application is extending Frame rather than JFrame. Also you do not need all the WindowListener actions as they will be handled automatically. Just extend JFrame, and add the following line in your constructor:
Java
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 
Share this answer
 

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