Here is the WordPlay code for this question:/JavaCS1/src/simpieguiapplication/Wo
ID: 3690392 • Letter: H
Question
Here is the WordPlay code for this question:/JavaCS1/src/simpieguiapplication/WordPlay.java When you finish this question, your application should work as follows: To match this behavior lets add a "Double" button to the WordPlay code. We've started the modification for you: WordPlay now contains a 3Button 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. Stuck? 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() { thisadd(inst); this.add(textField); thisadd(submit): submit. addActionListener(this); this add(display); public void actionPerformed(ActionEvent e){ Object source = e.getSource(); if(source == submit) { display.setText(textField.getText()); }Explanation / Answer
WordPlay.java:
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();
//boolean showing if there is new text to double
boolean newText = true;
public WordPlay() {
this.add(inst);
this.add(textField);
this.add(submit);
submit.addActionListener( this );
// my code:
this.add(doubleButton);
doubleButton.addActionListener( this );
// code provided:
this.add(display);
}
public void actionPerformed( ActionEvent e) {
Object source = e.getSource();
if (source.equals(submit)) {
display.setText( textField.getText() );
newText = true;
}
// my code:
if (source.equals( doubleButton )) {
String str = null;
if(newText){
str = textField.getText();
newText = false;
}else{
str = display.getText();
}
str+=str;
display.setText(str);
}
}
public static void main (String[] args){
JFrame frame = new JFrame( "WordPlay" );
frame.add(new WordPlay());
frame.setMinimumSize(new Dimension( 650, 75 ));
frame.setLocation(250, 250);
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.