Java Lecture Chapter 8

Marvic

Member
Designing Window and Simple Menu

Window and PopUp Menu
We can design a free-floating window in Java programming language with a message in it. We can design and develop a popup menu too. A popup menu is usually triggered by right-clicking the mouse button. Let us have an examples on these two last controls that we will cover in this book. Are you ready now?

Example 1:
Design and develop a Java program that demonstrates how to design and develop a window. Follow the given design specification below:

Solution:

1. At the Microsoft NotePad, write the following code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class window1 extends Applet implements ActionListener {
Button cmdShow, cmdHide;
frmFrame1 winWindow1;
public void init() {
cmdShow = new Button("Show Window");
add(cmdShow);
cmdShow.addActionListener(this);
cmdHide = new Button("Hide Window");
add(cmdHide);
cmdHide.addActionListener(this);
winWindow1 = new frmFrame1("Display Window");
winWindow1.setSize(200,100);
}
public void actionPerformed(ActionEvent objEvent) {
if (objEvent.getSource() == cmdShow) {
winWindow1.setVisible(true);
}
if (objEvent.getSource() == cmdHide) {
winWindow1.setVisible(false);
}
}
}
class frmFrame1 extends Frame {
Label lblLabel1;
frmFrame1(String strMessage) {
super(strMessage);
setLayout(new GridLayout(1,1));
lblLabel1 = new Label("Tara, Bianca, Jacque & Dimple!");
add(lblLabel1);
}
}

2. Then save the Java program with the filename: window1.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
<html>
<!- Web page written with Java Applet>
<head>
<title>window1</title>
</head>

<body>
<hr>
<applet
code=window1.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: window1.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac window1.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following
at your Web browser:
C:\javaprog\window1.htm

Explanation:
Our window object winWindow1 is based on the Java Frame class, which creates a frame window on the screen.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class window1 extends Applet implements ActionListener {
Button cmdShow, cmdHide;
frmFrame1 winWindow1

We are now initializing all our objects within the init( ) method as you can see in our code below:

public void init( ) {
cmdShow = new Button("Show Window");
add(cmdShow);
cmdShow.addActionListener(this);
cmdHide = new Button("Hide Window");
add(cmdHide);
cmdHide.addActionListener(this);
winWindow1 = new frmFrame1("Display Window");
winWindow1.setSize(200,100);
}

We can also give the new window an initial size in pixels by using the setSize( ) method.
We use the setVisible(true) method so that we can show the window, and to hide it, we use the setVisible(false) method.

public void actionPerformed(ActionEvent objEvent) {
if (objEvent.getSource( ) == cmdShow) {
winWindow1.setVisible(true);
}
if (objEvent.getSource( ) == cmdHide) {
winWindow1.setVisible(false);
}
}
}

The super( ) method is used in any derived class’s constructor to call the base class constructor. Since windows and dialog boxes in Java programming language have no default layout manager, thus we apply here the GridLayout manager:

class frmFrame1 extends Frame {
Label lblLabel1;
frmFrame1(String strMessage) {
super(strMessage);
setLayout(new GridLayout(1,1));
lblLabel1 = new Label("Tara, Bianca, Jacque & Dimple!");
add(lblLabel1);
}
}

Example 2:
Design and develop a Java program that demonstrates how to design and develop a popup menu. Follow the given design specification below:

Solution:

1. At the Microsoft NotePad, write the following code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class popup1 extends Applet implements ActionListener,
MouseListener {
TextField text1;
PopupMenu popup1;
MenuItem menuitem1, menuitem2, menuitem3;
public void init() {
popup1 = new PopupMenu ("Popup Menu");
menuitem1 = new MenuItem("Menu item 1");
menuitem1.addActionListener(this);
menuitem2 = new MenuItem("Menu item 2");
menuitem2.addActionListener(this);
menuitem3 = new MenuItem("Menu item 3");
menuitem3.addActionListener(this);
popup1.add(menuitem1);
popup1.add(menuitem2);
popup1.addSeparator();
popup1.add(menuitem3);
add(popup1);
text1 = new TextField(20);
add(text1);
addMouseListener(this);
}
public void mousePressed(MouseEvent e) {
if (e.getModifiers() != 0) {
popup1.show(this, e.getX(), e.getY());
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}

public void paint(Graphics objLabel)
{
objLabel.drawString("Right-click the mouse now!",50,120);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == menuitem1) {
text1.setText("You chose menu item 1");
}
if (event.getSource() == menuitem2) {
text1.setText("You select menu item 2");
}
if (event.getSource() == menuitem3) {
text1.setText("You click menu item 3");
}
}


2. Then save the Java program with the filename: popup1.java.
3. This time, open a new file at the NotePad to write the HTML script needed for the applet and type the following code:
<html>
<!- Web page written with Java Applet>
<head>
<title>popup1</title>
</head>
<body>
<hr>
<applet
code=popup1.class
width=200
height=200>
</applet>
<hr>
</body>
</html>

4. Now save the HTML script with the filename: popup1.htm.
5. This time, activate the Java compiler batch file with the following steps:
Type the word command at the Run menu of Windows operating system, then at the C:\> prompt, type:
C:\>cd javaprog (then press the Enter key)
C:\JAVAPROG>javap (then press the Enter key)
6. Finally, you can now compile your program with the following MS-DOS command:
C:\JAVAPROG>javac popup1.java (then press the Enter key)
7. When no error is encountered during the compilation process, you can now type the following
at your Web browser:
C:\javaprog\popup1.htm

Explanation:
We can declare the popup1 object using the PopupMenu class as you can see in our code below:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class popup1 extends Applet implements ActionListener,
MouseListener {
TextField text1;
PopupMenu popup1;

We are declaring here also the text field text1 so that we can report what menu item was selected by the user.

public void init( ) {
popup1 = new PopupMenu ("Popup Menu");
menuitem1 = new MenuItem("Menu item 1");
menuitem1.addActionListener(this);
menuitem2 = new MenuItem("Menu item 2");
menuitem2.addActionListener(this);
menuitem3 = new MenuItem("Menu item 3");
menuitem3.addActionListener(this);
popup1.add(menuitem1);
popup1.add(menuitem2);
popup1.addSeparator( );
popup1.add(menuitem3);
add(popup1);
text1 = new TextField(20);
add(text1);
addMouseListener(this);
}

We create a new object of class PopupMenu which we named popup1. We add three menu items here: Menu item 1, Menu item 2, and Menu item 3. We applied the addSeparator( ) method here to separate the menu item 3 from the above two menu items (it is like a white line that separates the menu items). We also add an ActionListener to make these menu items active. Moreover, we implement the MouseListener interface to handle mouse events.

We add five methods to implement mouse handling. They are mousePressed( ), mouseClicked( ), mouseRead( ), mouseEntered( ), and mouseExited( ).

public void mousePressed(MouseEvent e) {
if (e.getModifiers( ) != 0) {
popup1.show(this, e.getX( ), e.getY( ));
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}

In the mousePressed( ) method, we apply an if conditional statement that checks whether the right mouse button was clicked (this button brings up the popup menu). If the right mouse button was indeed clicked, the program will show the popup menu with its show( ) method at the present mouse location. We can get the mouse location by using the getX( ) and getY( ) MouseEvent methods.
We are applying here the paint( ) method to prompt the user to right-click the mouse. This method will simply display this prompt message at the applet.

public void paint(Graphics objLabel)
{
objLabel.drawString("Right-click the mouse now!",50,120);
}

We applied here the actionPerformed( ) method to handle menu selections:

public void actionPerformed(ActionEvent event) {
if (event.getSource( ) == menuitem1) {
text1.setText("You chose menu item 1");
}
if (event.getSource( ) == menuitem2) {
text1.setText("You select menu item 2");
}
if (event.getSource( ) == menuitem3) {
text1.setText("You click menu item 3");
}
}
}

In this lecture, we have learned the fundamental skills needed to program Java applet. I hope that the knowledge you gained will motivate you to study more about the other side of Java programming language. Enjoy your journey to Java programming excellence!

QagqXaT.png


Java Lecture Pages
 

Similar threads


Top Bottom