Question: SOFTWARE ENGINEERING COMPUTER SCIENCE Divide this program into 3 classes Model, View, and Controller. import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import

SOFTWARE ENGINEERING

COMPUTER SCIENCE

Divide this program into 3 classes Model, View, and Controller.

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.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 javax.swing.JPanel; import java.util.*;

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

public class ButtonExample1 extends WindowAdapter implements ActionListener {// main class Button blueButton, redButton; TextField tf,tf1; Label loginLabel,loginLabel1; String password; Map UserName = new HashMap(); Frame frame, frame1; JPanel buttonPanel, buttonPanel1; public ButtonExample1(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 UserName.put("KhangPham", "student12+"); UserName.put("Professor", "teachclass"); UserName.put("TA1", "gradeproject"); UserName.put("TA2", "gradehw"); 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 frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } });

}

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(){ // prepare a new screen for password String title = "Welcome"; 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); frame1.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String cmd = e.getActionCommand(); if (cmd.equals("Input Username")){ // if username has been submitted String enter = tf.getText(); // get the user name try{ if (UserName.containsKey(enter)){ password = UserName.get(enter); // get password. will be used later frame.setVisible(false); // make this screen invisible to start the password screen createNew(); // start the password screen return; } else { 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 frame1.setTitle("You have sucessfully logged in"); } else{ tf1.setText(""); // password does not match frame1.setTitle("Wrong Password"); frame1.setVisible(false); frame1.setVisible(true); } } } public static void main(String args[]){ new ButtonExample1("Log in Screen"); } }

The program first set up the login window. Then ask for a username. Once a username is submitted, the program would look for it inside of the map UserName. If there is a match, the program then asks for password. I want to divide it up to the MVC.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!