Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

creating a menu in java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Menu implements ActionListener
{
    static JLabel text;
    //Driver function
    public static void main(String args[])
    {
	//Create a frame
	JFrame frame = new JFrame("Menu");
	frame.setSize(500,500);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setLayout(new FlowLayout());
	//Create an object
	Menu obj = new Menu();
	//Create a Menu
	JMenu menu = new JMenu("Select Here");
	//Create Menu Items
	JMenuItem item[] = new JMenuItem[5];
	for(int i=0;i<5;i++)
	{
	    item[i]=new JMenuItem("Item "+(i+1));
	    item[i].addActionListener(obj);
	    menu.add(item[i]);
	}
	//Create a menu bar
	JMenuBar mb=new JMenuBar();
	mb.add(menu);
	frame.setJMenuBar(mb);
	//Create the label
	text = new JLabel();
	frame.add(text);
	//Display the frame
	frame.setVisible(true);
    }
    //Function to display the menu item selected
    public void actionPerformed(ActionEvent e)
    {
	text.setText("Menu Item Selected : "+e.getActionCommand());
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: remove last character from stringbuffer 
Java :: his version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 4.2 or newer. 
Java :: fixed length array powershell PSv5+ 
Java :: design custom button in android 
Java :: Quick Sort Java Implementation 
Java :: sublist java 
Java :: java arraylist add 
Java :: set text from strings.xml 
Java :: java string.format system.currenttimemillis 
Java :: how to add to an arraylist java 
Java :: java actionevent 
Java :: java round up 
Java :: breadth first search bst java 
Java :: elif java 
Java :: implement stack using linked list java 
Java :: how to check if a person presses a button in jframe in java 
Java :: remove duplicates from string in java 
Java :: get executable path java 
Java :: java ceil 
Java :: list of arrays java 
Java :: how to get index of arraylist in java 
Java :: do while jaca 
Java :: java home 
Java :: java method overriding 
Java :: check if char is letter or digit 
Java :: constructor 
Java :: jframe maximized at startup 
Java :: maven skip make-assembly 
Java :: java require non null 
Java :: windows Jmeter java.net.BindException: Address already in use: connect 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =