Question: Hello, In Java , I'm trying to develop a GUI for a user to log in. It should prompt the user to type in and
Hello, In Java, I'm trying to develop a GUI for a user to log in. It should prompt the user to type in and submit a username, and then ask them to submit their password which it validates. The program should be broken up into three classes: Model, View, and Controller. The Model should manage the data, the View faces user, and Controller contains logic. You can use any storage of data you like, and the UI should be straight forward, and hiding the characters/encrypting isn't a requirement.
From the code I have below (which I have made in Eclipse) are the Model, View, and Controller classes named ModelUseNamPass.java, ViewUseNamPass.java, and ControllerUseNamPass.java respectively including the class that interacts with them named MVCUseNamPass.java, which are fully set up to where none of them except ViewUseNamPass.java which has an error saying "The return type is incompatible with Component.getName()" to where it has one red swiggle line under int.
If you can find some way to resolve the problem and get the program to work base on the given instructions, then it would be very much appreciative. The code I have is shown below.
----------------------------------------------------------
ModelUseNamPass.java
public class ModelUseNamPass {
//Values that will display on the screen.
private int UserName, PassWord;
//Method for inputing user name.
public void EnterName(int Name){
UserName = Name;
}
//Gets the User Name value.
public int getUserName(){
return UserName;
}
//Method for inputing password
public void PassWord(int data){
PassWord = data;
}
//Gets the Password value.
public int getPassWord(){
return PassWord;
}
}
ViewUseNamPass.java
import java.awt.event.ActionListener; //Performs and catch events when user clicks on buttons
import javax.swing.*; //Creates the interface
//Components to make the interface.
public class ViewUseNamPass extends JFrame {
//Label that says "User Name" in the dialogue box.
private JLabel UserNameLabel = new JLabel("User Name");
//Allows user to enter his/her name.
private JTextField Name = new JTextField(100);
//Displays button for user to click after inputting his/her name.
private JButton UserNameButton = new JButton("Submit");
//Label that says "Password" in the dialogue box.
private JLabel PassWordLabel = new JLabel("Password");
//Allows user to enter his/her password.
private JTextField PassWord = new JTextField(100);
//Displays button for user to click after inputting his/her password.
private JButton PassWordButton = new JButton("Submit");
//Displays Label that welcomes the user after he/she finishes entering both username and password.
private JLabel SubmitResults = new JLabel("Welcome, " + Name + "!");
//Interface that will display on the screen.
ViewUseNamPass(){
//Sets up the interface that will display on the screen.
JPanel UseNamPassPanel = new JPanel();
//When user clicks the "x" on the top right of the dialogue box, the application will close.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Sets the size of the dialogue box 600 wide and 200 pixels tall.
this.setSize(600, 200);
//Components added to the panel for the User Name.
UseNamPassPanel.add(UserNameLabel); //Displays the label for user name.
UseNamPassPanel.add(Name); //Displays the box for user to input his/her name.
UseNamPassPanel.add(UserNameButton); //Displays button for user to click after he/she finishes inputting name.
//Components added to the panel for the Password.
UseNamPassPanel.add(PassWordLabel); //Displays the label for password.
UseNamPassPanel.add(PassWord); //Displays the box for user to input his/her password.
UseNamPassPanel.add(PassWordButton); //Displays button for user to click after he/she finishes inputting password.
//Components to show results after user is finished inputting both his/her username and password.
UseNamPassPanel.add(SubmitResults);
//Represents the JFrame that is extended by ViewUseNamePass as shown on the top.
this.add(UseNamPassPanel);
//End of setting up the components.
}
//Gets access to the user name. THIS IS WHERE THE ERROR IS LOCATED AT.
public int getName(){
//Returns a string
return Integer.parseInt(Name.getText());
}
//Gets access to the password.
public int getPassWord(){
//Returns a string
return Integer.parseInt(PassWord.getText());
}
//Gets the submission results
public int getSubmitResults(){
return Integer.parseInt(SubmitResults.getText());
}
//Sets the submission results
public void setSubmission(int Submission){
SubmitResults.setText(Integer.toString(Submission));
}
//If the button is clicked, then execute a method in the Controller named actionPerformed.
//When button for both the User Name and Password are clicked, the Controller will be alerted by it.
void addListener(ActionListener ListenForButton){
UserNameButton.addActionListener(ListenForButton);
PassWordButton.addActionListener(ListenForButton);
}
//This will open a pop up that contains the error message passed.
void displayErrorMessage(String errorMessage){
JOptionPane.showMessageDialog(this, errorMessage);
}
}
ControllerUseNamPass.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//Handles interactions between everything.
public class ControllerUseNamPass {
//Allows interaction with ViewUseNamPass
private ViewUseNamPass View;
//Allows interaction with ModelUseNamPass
private ModelUseNamPass Model;
//Represents the constructor
public ControllerUseNamPass(ViewUseNamPass View, ModelUseNamPass Model){
//Access the View.
this.View = View;
//Access the Model.
this.Model = Model;
//Tells View when the "submit" button is click, then it executes the ActionPerformed method.
this.View.addListener(new Listener());
}
//Listener as an inner class letting us listen what is going on with our View with our Controller.
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int Name = 0;
//Prevents any errors from being triggered.
try{
Name = View.getName();
Model.EnterName(Name);
View.setSubmission(Model.getUserName());
}
//Catches error that is triggered.
catch(NumberFormatException ex){
View.displayErrorMessage("INVALID INPUT!");
}
}
}
}
MVCUseNamPass.java
public class MVCUseNamPass {
public static void main(String[] args) {
// TODO Auto-generated method stub
//View exists.
ViewUseNamPass View = new ViewUseNamPass();
//Model exists.
ModelUseNamPass Model = new ModelUseNamPass();
//Controller unifies both View and Model so they can both interact with each other.
ControllerUseNamPass Controller = new ControllerUseNamPass(View, Model);
//Makes View visible on the screen.
View.setVisible(true);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
