To match this behavior let\'s add a \"Double\" button to the WordPlay code. We\'
ID: 3573151 • Letter: T
Question
To match this behavior let's add a "Double" button to the WordPlay code. We've started the modification for you: WordPlay now contains a JButton component called doubleButton. When the Double button is clicked, the displayed string should be doubled, or repeated, in the program.
The implementation of the "Double" button is incomplete. In the boxes below, provide the missing statements so that the doubling feature will work properly.
*************WordPlay code***********************
**********************************************************
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 doubleButton = new JButton("Double");
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
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 doubleButton = new JButton("Double");
JLabel display = new JLabel();
public WordPlay() {
this.add(inst);
this.add(textField);
this.add(submit);
submit.addActionListener(this);
//********Code here********
doubleButton.addActionListener(this);
this.add(display);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == submit){
display.setText(textField.getText());
}
//********Code here********
else if(source==doubleButton){
display.setText(textField.getText()+textField.getText());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.