Add Images and Tooltips To Main Menu
Add Images and Tooltips To Main Menu
main menu
FROM :
RAJU B C
To add images and tooltips to a main menu in Java, you can use the
Swing library. Here's a step-by-step guide:
Step 1: Create Menu Items
1. Create J menu Item instances for each menu item.
2. Use J Menu to create the main menu.
Step 2: Add Images to Menu Items
1. Use Image Icon to load images for each menu item.
2. Set the icon for each J menu Item using set Icon().
Step 3: Add Tooltips to Menu Items
1. Use set Tool Tip Text() to add tooltips to each J menu
Item.
Step 4: Add Menu Items to Menu
1. Use add() to add J menu Item instances to the J Menu.
Step 5: Add Menu to Menu Bar
1. Create a J Menu Bar instance.
2. Use add() to add the J Menu to the J Menu Bar.
Example Code
import javax.swing.*;
import java.awt.*;
public class MainMenuExample {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Main Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create menu bar
JMenuBar menuBar = new JMenuBar();
// Create menu
JMenu menu = new JMenu("File");
// Create menu items with images and tooltips
JMenuItem openItem = new JMenuItem("Open", new
ImageIcon("open.png"));
openItem.setToolTipText("Open a file");
JMenuItem saveItem = new JMenuItem("Save", new ImageIcon("save.png"));
saveItem.setToolTipText("Save a file");
JMenuItem exitItem = new JMenuItem("Exit", new ImageIcon("exit.png"));
exitItem.setToolTipText("Exit the application");
// Add menu items to menu
menu.add(openItem);
menu.add(saveItem);
menu.addSeparator();
menu.add(exitItem);
// Add menu items to menu
menu.add(openItem);
menu.add(saveItem);
menu.addSeparator();
menu.add(exitItem);
// Add menu to menu bar
menuBar.add(menu);
// Set menu bar to frame
frame.setJMenuBar(menuBar);
// Set frame size and visibility
frame.setSize(400, 300);
frame.setVisible(true);
}
}