Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Base on the code I have below using Eclipse, it can run the UI to where it has the label "User Name" a box where the user can input his/her name, and the red submit button, but when I try to input my name in the box and click submit, I get a result of messages in red as shown in the bottom half of this image, which also seen in the screenshot below. Also, below this image is also the code I am currently using to run this UI. I pretty much have it set up, but doesn't seem to run like it's suppose to.

---------------------------------------------------------------------

image text in transcribed

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Label;

import java.awt.List;

import java.awt.Rectangle;

import java.awt.TextField;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement; //extra

import java.sql.ResultSet; //extra

import java.util.Properties;

import java.awt.TextArea;

import java.lang.*;

import java.awt.Container;

import java.awt.TextComponent;

import javax.swing.JPanel;

import java.util.*;

//import com.mysql.jdbc.ResultSet;

//import com.mysql.jdbc.Statement;

// We use Java awt (window toolkit) and Swing

public class Button1 extends WindowAdapter implements ActionListener {// main class

Frame frame, frame1;

JPanel buttonPanel, buttonPanel1;

Button blueButton, redButton;

TextField tf,tf1;

List list;

Label loginLabel,loginLabel1;

String password;

public Button1(String title) {

frame = prepareFrame(title); // get a frame

tf= new TextField("", 20); // get a Text Field

loginLabel = new Label("User Name", Label.RIGHT);

loginLabel.setForeground(Color.BLUE);

buttonPanel = prepareContainer(); // get a panel

redButton = prepareButton("Submit", "Input Username"); // get a button

buttonPanel.add(loginLabel); // add label to panel

buttonPanel.add(tf); // add text field to panel

buttonPanel.add(redButton);//add button

frame.add("Center", buttonPanel); // add the panel to the frame

frame.setVisible(true); // make the frame visible

}

//} This curly bracelet was removed due to outputting an error

/* (non-Javadoc)

* @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)

*/

@Override

public void windowClosing(WindowEvent e) {

super.windowClosing(e);

//frame.setVisible(true);

}

public Frame prepareFrame(String title){

Rectangle r = new Rectangle(10,10,500,500);

frame = new Frame(title);

frame.setSize(950, 500);

frame.setLayout(new BorderLayout());

frame.setMaximizedBounds(r);

frame.setExtendedState(Frame.MAXIMIZED_BOTH);

frame.addWindowListener(this); // add a handler for events on this frame

return frame;

}

public Button prepareButton(String title, String actionCommand){

Button redButton = new Button(title);

redButton.setBackground(Color.RED);

redButton.setActionCommand(actionCommand);

redButton.addActionListener(this);

return redButton;

}

public JPanel prepareContainer(){

JPanel buttonPanel = new JPanel(new FlowLayout());

// Dimension d = new Dimension(2000,2000);

buttonPanel.setBounds(10,10,200,200);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

int x = Math.max(( screenSize.width / 2 - frame.getSize().width / 2),0);

int y = Math.max((screenSize.height / 2 - frame.getSize().height / 2),0);

buttonPanel.setLocation(x,y);

return buttonPanel;

}

public void createNew(String firstName, String lastName){ // prepare a new screen for password

String title = "Welcome " + firstName + " " + lastName;

frame1=prepareFrame(title);

tf1= new TextField("", 20);

loginLabel1 = new Label("Please Enter your Password", Label.RIGHT);

loginLabel1.setForeground(Color.BLUE);

buttonPanel1 = prepareContainer();

buttonPanel1.add(loginLabel1);

buttonPanel1.add(tf1);

blueButton = prepareButton("Submit","Input Password");

buttonPanel1.add(blueButton);

frame1.add("Center", buttonPanel1);

frame1.setVisible(true);

}

//public

public String databaseHandling(){

Properties settings = new Properties();

FileInputStream fis;

try{

fis = new FileInputStream("agent.properties"); // open properties file

settings.load(fis);

fis.close();

}

catch(Exception e1){

e1.printStackTrace();

}

String databaseURL = settings.getProperty("DatabaseURL"); // get the DatabaseURL property

return databaseURL;

}

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

if (cmd.equals("Input Username")){ // if username has been submitted

String enter = tf.getText(); // get the user name

try{

String databaseURL = databaseHandling(); // get the URL of the database

Class.forName("com.mysql.jdbc.Driver");

Connection con =

DriverManager.getConnection(

databaseURL,"root", "dijkstra"); // connect to database

Statement stmt = (Statement) con.createStatement(); // prepare a query statement

ResultSet rs = (ResultSet) stmt.executeQuery("SELECT * from user"); // query the database

while(rs.next()){

String user_Name = rs.getString("user_name");

if(user_Name.equals(enter)) // username matches an entry in the database

{

String firstName = rs.getString("firstname"); // get first name

String lastName = rs.getString("lastname"); // get last name

password = rs.getString("password"); // get password. will be used later

frame.setVisible(false); // make this screen invisible to start the password screen

createNew(firstName,lastName); // start the password screen

return;

}

}

tf.setText(""); // the username is not found in the database

frame.setTitle("The User Name entered is incorrect. Please enter your user name");

frame.setVisible(false);

frame.setVisible(true);

}

//}

catch (Exception e4){

e4.printStackTrace();

}

}

else if(cmd.equals("Input Password")){ // password submitted

if(tf1.getText().equals(password)){ // password matches

// do something here

}

else{

tf1.setText(""); // password does not match

frame1.setTitle("Wrong Password");

frame1.setVisible(false);

frame1.setVisible(true);

}

}

}

public static void main(String args[]) {

new Button1("Log in Screen");

}

}

Use Name amahing else 1fCMS.enals(.Input Passwo do sowethinsh ttl.setText pss treeel.setiier Wronz P trenel setvisitlefalse treeel.setvisitle(true) 215 public static void mainString ars at lavs.ls.g.CLass, tort.a#2e :Nativ Method) Source) at lavs.bg,CLass, tort.92(Unnon. at 1ava.ant. Button.processActionE ent(Unkoronn Sourcej at 1ava.ant. Button.processeventUknom Scurce 1av2.avit . Corponenc.dis2:c.Evetlial:u,known scurce: av2.avit . Corponent-dis2 tc-Event : U.known Scurce at at at at at av2.ait. EventQueus, accessese9(unkron so.rce) v2. ait. Even-Queus43. run(Unknown Source iava.ant.Evcnaqueu3.run unknonn Source) av2.securty.kicesiController.dof.ii-egedrNative Method va . security. ProtectionDnin$3wsecuritraccess-sp1.001 tersectionrivilegefurinom Source) t w.vt. EvenDQueus$4.run(Unknown Source at java.nt.Evencus4.run(unkncn Source) ut jw.security.AccessCoitroller.doP. -c8-dNative Method) Juc.eesurity . aruw% 11Duui1GJuvuseurityALLess-sp1.doLitersection rivilegi(Ufiuwri sourcc) Use Name amahing else 1fCMS.enals(.Input Passwo do sowethinsh ttl.setText pss treeel.setiier Wronz P trenel setvisitlefalse treeel.setvisitle(true) 215 public static void mainString ars at lavs.ls.g.CLass, tort.a#2e :Nativ Method) Source) at lavs.bg,CLass, tort.92(Unnon. at 1ava.ant. Button.processActionE ent(Unkoronn Sourcej at 1ava.ant. Button.processeventUknom Scurce 1av2.avit . Corponenc.dis2:c.Evetlial:u,known scurce: av2.avit . Corponent-dis2 tc-Event : U.known Scurce at at at at at av2.ait. EventQueus, accessese9(unkron so.rce) v2. ait. Even-Queus43. run(Unknown Source iava.ant.Evcnaqueu3.run unknonn Source) av2.securty.kicesiController.dof.ii-egedrNative Method va . security. ProtectionDnin$3wsecuritraccess-sp1.001 tersectionrivilegefurinom Source) t w.vt. EvenDQueus$4.run(Unknown Source at java.nt.Evencus4.run(unkncn Source) ut jw.security.AccessCoitroller.doP. -c8-dNative Method) Juc.eesurity . aruw% 11Duui1GJuvuseurityALLess-sp1.doLitersection rivilegi(Ufiuwri sourcc)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+4. Are there any disadvantages to this approach?

Answered: 1 week ago