Write a Java application named MoveRight. The application presents a JFrame-base
ID: 3766749 • Letter: W
Question
Write a Java application named MoveRight. The application
presents a JFrame-based window that displays a text field, a button labeled "Right", and another text field. The user can type any text they wish into either text field, but whenever the "Right" button is pressed the text from the left text field is copied into the right text field. You will need to do a getText on the left text field and a setText on the right text field.Paste the code into the essay box, below. Don't worry if the indentation is lost when you post the code (although you need to make sure that it does not run together onto one long line).
Explanation / Answer
MoveRight.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MoveRight extends JFrame implements ActionListener {
private JTextField leftTextField, rightTextField;
private JButton button;
public MoveRight() {
initialize();
}
private void initialize() {
leftTextField = new JTextField(20);
rightTextField = new JTextField(20);
button = new JButton("Right");
button.addActionListener(this);
setLayout(new BorderLayout());
add(leftTextField, BorderLayout.WEST);
add(rightTextField, BorderLayout.EAST);
add(button, BorderLayout.NORTH);
}
@Override
public void actionPerformed(ActionEvent e) {
String text = leftTextField.getText();
rightTextField.setText(text);
}
public static void main(String[] args) {
MoveRight thisFrame = new MoveRight();
thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisFrame.setSize(600,600);
thisFrame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.