Question
(JAVA) Modify class GUIApp (that we wrote in previous sections from this module) so that the window has two buttons, one marked Hi and one
(JAVA)
Modify class GUIApp (that we wrote in previous sections from this module) so that the window has two buttons, one marked "Hi" and one marked "Bye".
For the new version of this application, the user has two choices: If the user enters her name and then clicks the "Hi" button, the program displays the message "Well hello there
The user is able to enter different names and click different buttons to see one of the two messages as many times as she wants. When the user clicks the close button on the window, the application exits.
In order to prove that you have a completed, working application, take a screen shot of the final window that contains the goodbye message on the JLabel output. The following two paragraphs tell how to do this. Submit the screen shot together with your two source code files GUIApp.java and Main.java here in Canvas.
You will only be adding code to class GUIApp. Main class will remain the same.
GUIApp class
/*** One object of this class instantiates a JFrame, and a place for the user to* type her name, and a button. If the user types her name and clicks the "Hi"* button, a personalized greeting will appear on the JFrame.*/
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GUIApp implements ActionListener {
private JFrame myWindow;
private JTextField userInput;
private JButton hello;
private JLabel output;
/** * constructor sets up the JFrame and put all the components on it. */
public GUIApp() {
setUpWindow();
setUpEntry();
setUpHelloButton();
setUpOutput();
myWindow.validate();
}
/** * Instantiates the new JFrame and sets it up empty */
public void setUpWindow() {
myWindow = new JFrame();
myWindow.setLayout(new FlowLayout());
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setSize(300, 150);
myWindow.setVisible(true);
}
/** * Instantiates a text entry box for user input and adds it to the JFrame. */
public void setUpEntry() {
userInput = new JTextField("Type your name here");
myWindow.add(userInput);
}
/**
* * Instantiates the hello button, adds it to the JFrame, * and makes this
* class listen and react to a user event on that button.
*/
public void setUpHelloButton() {
hello = new JButton("Hi");
myWindow.add(hello);
hello.addActionListener(this);
}
/**
* * Instantiates a JLabel for displaying the output, adds it to the JFrame, *
* but leaves the JLabel empty for now.
*/
public void setUpOutput() {
output = new JLabel(" ");
myWindow.add(output);
}
/**
* * Java calls this method when the user clicks a button that has this class *
* as its ActionListener. This method then puts a string message onto the *
* JLabel output.
*/
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand() == "Hi") {
String usersName = userInput.getText();
output.setText("Well hello there " + usersName + "!");
}
}
}
Main Class
/**
* Instantiates a GUI application that puts up a JFrame and waits for
* user events.
*/
public class Main {
public static void main(String[] args) {
GUIApp myApp;
myApp = new GUIApp();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started