Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This version of WordPlay contains a JButton component called quit. When you clic

ID: 3573145 • Letter: T

Question

This version of WordPlay contains a JButton component called quit. When you click on the Quit button, your application should terminate the current running program. To accomplish this, you should use the statement System.exit(0) , which will kill the application.

*********WordPlay code**************

**************************************************************************************

In the answer boxes below, provide appropriate code so that the Quit button will work correctly.


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

public class WordPlay extends JPanel implements ActionListener{

  JLabel inst = new JLabel("Enter your text:");
  JTextField textField = new JTextField(5);

  JButton submit = new JButton("Submit");
  JButton quit = new JButton("Quit");

  JLabel display = new JLabel();

  public WordPlay() {
    this.add(inst);
    this.add(textField);
    this.add(submit);
    submit.addActionListener(this);

*******Code here******

this.add(display);
  }

  public void actionPerformed(ActionEvent e){
    Object source = e.getSource();
    if(source == submit){
      display.setText(textField.getText());
    }

*******Code here******

}

}

Explanation / Answer

public WordPlay() {
    this.add(inst);
    this.add(textField);
    this.add(submit);
    submit.addActionListener(this);

quit.addActionListener(this);

this.add(display);
  }

  public void actionPerformed(ActionEvent e){
    Object source = e.getSource();
    if(source == submit){
      display.setText(textField.getText());
    }

if(source == quit) {

   System.exit(0);

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote