Creating a JMenuBar, JMenu, and JMenuItem Component

When the user selects a menu item, it fires an action event.
// Create the menu bar JMenuBar menuBar = new JMenuBar(); // Create a menu JMenu menu = new JMenu("Menu Label"); menuBar.add(menu); // Create a menu item JMenuItem item = new JMenuItem("Item Label"); item.addActionListener(actionListener); menu.add(item); // Install the menu bar in the frame frame.setJMenuBar(menuBar);

Comments

10 Mar 2010 - 11:13pm by Archielsie (not verified)

meaning about JMenu class

15 Mar 2010 - 6:15pm by Anonymous (not verified)

// Create the menu bar JMenuBar menuBar = new JMenuBar(); // Create a menu JMenu menu = new JMenu("Menu Label"); menuBar.add(menu); // Create a menu item JMenuItem item = new JMenuItem("Item Label"); item.addActionListener(actionListener); menu.add(item); // Install the menu bar in the frame frame.setJMenuBar(menuBar);

18 Mar 2010 - 6:45pm by Anonymous (not verified)

java is hard..ahh..:'(

17 Apr 2010 - 7:10am by tatlong piraso (not verified)

this is good, thank you!
though i haven't tried it yet.
looks working for me :D

10 Aug 2010 - 7:33pm by Rishi (not verified)

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

class King implements MouseListener
{

JFrame jai;
Container con;
JLabel lab,lab1,lab2;
JTextField text,text1,text2;
JButton btn;

void show()
{

jai = new JFrame("ADDITION");

jai.setBounds(0,0,800,600);
jai.setVisible(true);
con = jai.getContentPane();
con.setLayout(null);
lab = new JLabel("First ");
lab.setBounds(10,10,200,100);
con.add(lab);

lab1 = new JLabel("Last Number ");
lab1.setBounds(10,50,200,100);
con.add(lab1);

lab2 = new JLabel("total Number ");
lab2.setBounds(10,80,200,100);
con.add(lab2);

text=new JTextField();
text.setBounds(100,55,280,25);
con.add(text);

text1=new JTextField();
text1.setBounds(100,88,280,25);
con.add(text1);

text2=new JTextField();
text2.setBounds(100,122,320,25);
con.add(text2);

btn=new JButton(" ADD ");
btn.setSize(100,30);
btn.setLocation(100,300);
btn.addMouseListener(this);
}

public void mouseClicked(MouseEvent ee);
1 {

String s1,s2,s3;
int a,b,s;

if(ee.getComponent()==btn)
{
s1=text.getText();
s2=text1.getText();

a=Integer.parseInt(str1);
b=Integer.parseInt(str2);
s=a+b;
s3=String.valueOf(s);
text3.setText(s3);
}
else

{
System.out.println("not found");
}

}
public void mousePressed(MouseEvent ee);
{
}

public void mouseRelesed(MouseEvent ee);
{
}
public void mouseExited(MouseEvent ee);
{
}
public void mouseEntered(MouseEvent ee);
{
}

public static void main(String gg[])
{

King mm = new King();
mm.show();
}
}
using listener..............

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.