loading...

CS506 Web Design and Development Assignment 2 Solution Fall 2014

Problem Statement:

You are required to write a java program which handles the “MouseEvent” by implementing “MouseListener” interface.

Detailed Description:

Develop a GUI component and set the layout using “FlowLayout” manger. The program should have two text fields used for displaying your name and VUID as a default text. There should also be two labels in your GUI.  Following four controls should be used in your GUI application.

  • Student Name (JLabel)
  • VU ID(JLabel)
  • Student_Name (JTextField)
  • VU_ID (JTextField)

Following is the behavior of your GUI Application which handles the Mouse Events program.

  • When mouse enter the boundary of frame window “default text should be appeared” in the respective textfields i.e. your name and VUID
  • When mouse exit the boundary of frame window “default text should be removed” from the text fields, it sets empty. 

For this your class has to implement the MouseListener interface, MouseListener interface has following five abstract methods.

public interface MouseListener {

public void mousePressed (MouseEvent me);

public void mouseClicked (MouseEvent me);

public void mouseReleased (MouseEvent me);

public void mouseEntered (MouseEvent me);

public void mouseExited (MouseEvent me);        

}

But your class will implement the logic in last two methods,

public void mouseEntered (MouseEvent me);

You will write the logic in above method when mouse enter the frame window i.e. setting of text in respective text fields

public void mouseExited (MouseEvent me);

You will write the logic in above method when mouse leaves the frame window i.e. clearing of text in respective text fields

Remaining methods will have an empty body.

  • Set the frame size “width 300 and height 200”.
  • You can use the setText(String) method for setting of text of Two JTextField.  Use your name and your VUID to store values in these two text fields
  • When you exit from the program, it should close the frame.

Solution:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MyProg implements MouseListener{
JFrame MyFrame=new JFrame(“Flow Layout”);
JLabel MyName =new JLabel(“Student Name:”);
JLabel MyId = new JLabel(“Student Id:”);
JTextField TxtName =new JTextField(“Your Name”);
JTextField TxtId=new JTextField(“Your Id”);
public void GuiInit(){

MyFrame.addMouseListener(this);
TxtName.setPreferredSize(new Dimension(150,20));
TxtId.setPreferredSize(new Dimension(150,20));
Container c= MyFrame.getContentPane();
c.setLayout(new FlowLayout());
c.add(MyName);
c.add(TxtName);
c.add(MyId);
c.add(TxtId);
MyFrame.setSize(300, 200);
MyFrame.setVisible(true);
MyFrame.addMouseListener(this);
}
public MyProg(){
GuiInit();
}

public void mousePressed (MouseEvent me){

}
public void mouseClicked (MouseEvent me){

}
public void mouseReleased (MouseEvent me){

}
public void mouseEntered (MouseEvent me){
TxtName.setText(“Ali”);
TxtId.setText(“mc100000000”);
}
public void mouseExited (MouseEvent me){
TxtName.setText(null);
TxtId.setText(null);
}
public static void main(String[] args){
MyProg MyNewProg=new MyProg();
}
}

DOWNLOAD SOLUTION HERE
loading...