Add menu items to the window in JAVA and process the response of menu items

Add menu items to the window in JAVA and process the response of menu items Title restatement Adds a menu to the windo...

Add menu items to the window in JAVA and process the response of menu items

Title restatement

Adds a menu to the window and handles events raised by menu items
Program function: add a menu bar in the window, add a menu in the menu bar, and add menu items and submenus in the menu. Different events can be triggered by selecting menu items to perform different operations. For example, clicking the "open" menu item can open an "open file dialog box".

Write MyMenu.java program file, and the source code is as follows.
import java.awt.;
import java.awt.event.;
public class MyMenu extends Frame implements ActionListener {
static Frame f; // Declare a framework
static FileDialog fd1; // Declare a file dialog object
static FileDialog fd2;
public static void main(String args[]) {
MyMenu k = new MyMenu();
f = new Frame(); // Create a frame f
... / / create a menu bar mb
... / / add two menus m1 ("file") and m2("help") to the menu bar mb
... / * "file" menu m1 has three menu items: m11 ("open")
m12 ("save") and m13 ("exit")/
... / "help" menu m2 has three menu items: m21 ("contents")
m22 ("index") and m23 ("about")/
... / / "file" menu m1 also has a submenu m3 ("Edit")
... / "Edit" submenu m3 has three menu items: m31 ("copy")
m32 ("cut") and m33 ("paste")/
... / allows the menu item "open" to raise an event whose listener is the current object k/
... / allow the menu item "save" to raise an event whose listener is the current object k/
f. SetSize (350200); / / set the size of frame f
f.setMenuBar(mb); / / set the menu bar of frame f to mb
f.setVisible(true);
}
public void actionPerformed (ActionEvent e) {/ handles events raised by the menu items "open" and "exit"*/
if (e.getActionCommand() = = "open") {/ * if the "open" menu item raises an event, open the "open file dialog box"/
... / / create and open an "open file dialog"
}
if (e.getActionCommand() = = "save") / if the "save" menu item raises an event, open the "save file dialog box"*/
... / / create and open a "save file dialog box"
}
}
Add the vacant part in the above program, compile and execute the program.

Problem analysis and solution ideas

To be improved (please wait patiently)

Program code
import java.awt.*; import java.awt.event.*; public class MyMenu extends Frame implements ActionListener { static Frame f; static FileDialog fd1; static FileDialog fd2; public static void main(String args[]) { MyMenu k=new MyMenu(); f=new Frame(); MenuBar m=new MenuBar(); Menu m1=new Menu("file"); Menu m2=new Menu("help"); MenuItem m11=new MenuItem("open"); MenuItem m12=new MenuItem("preservation"); MenuItem m13=new MenuItem("sign out"); MenuItem m21=new MenuItem("catalogue"); MenuItem m22=new MenuItem("Indexes"); MenuItem m23=new MenuItem("about"); Menu m3=new Menu("edit"); MenuItem m31=new MenuItem("copy"); MenuItem m32=new MenuItem("shear"); MenuItem m33=new MenuItem("paste"); m3.add(m31); m3.add(m32); m3.add(m33); m1.add(m3); m1.add(m11); m1.add(m12); m1.add(m13); m2.add(m21); m2.add(m22); m2.add(m23); m.add(m1); m.add(m2); f.setSize(350,200); f.setMenuBar(m); f.setVisible(true); } }

27 October 2021, 19:12 | Views: 2399

Add new comment

For adding a comment, please log in
or create account

0 comments