DON\'T KNOW WHAT IS THE ERROR OF THIS CODE.. JUST CAN\'T GET CORRECT IN THIS PRO
ID: 3627629 • Letter: D
Question
DON'T KNOW WHAT IS THE ERROR OF THIS CODE.. JUST CAN'T GET CORRECT IN THIS PROBLEM..
THE DOUBLE BUTTON WORKS LIKE THIS:
EX:
INPUT=11, OUTPUT=1111,(AND IF I CLICK DOUBLE AGAIN, THE OUTPUT IS 11111111 AND SO ON)
INPUT=SASA, OUTPUT=SASASASA,
When you finish this question, your application should work as follows:
Let's add another button to this version of the WordPlay program, a "Double" button. Notice that we've added new code, so that 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.
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);
**THIS IS MY CODE**
this.add(doubleButton);
doubleButton.addActionListener(this);
**END OF MY CODE**
this.add(display);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == submit){
display.setText(textField.getText());
}
**THIS IS MY CODE**
if(source == doubleButton){
display.setText(textField.getText()+textField.getText());
}
**END OF MY CODE**
}
}
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);
//**THIS IS MY CODE**
this.add(doubleButton);
doubleButton.addActionListener(this);
//**END OF MY CODE**
this.add(display);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == submit){
display.setText(textField.getText());
}
//**THIS IS MY CODE**
if(source==doubleButton){
if (display.getText().equals(""))
JOptionPane.showMessageDialog(this,"Enter text into the textbox, press submit button and then clik double button","Information", JOptionPane.INFORMATION_MESSAGE);
display.setText(display.getText()+" "+display.getText());
}
//**END OF MY CODE**
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.