Advertisement
Constructor | Description |
---|---|
public JMenuBar() | Creates a menu bar to which one or many menus are added. |
public JMenu(String title) | Creates a menu with a title. |
public JMenuItem(String title) | Creates a menu item with a title. |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Menu1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class Combo
class A implements ActionListener
{
JFrame jf;
JMenuBar menuBar;
JMenu menu1, menu2;
JMenuItem mItem1, mItem2, mItem3, mItem4, mItem5, mItem6, mItem7;
JButton button1, button2, button3;
JDialog d1, d2, d3;
A()
{
jf = new JFrame("JDialog");
//Creating a menu bar
menuBar= new JMenuBar();
//Creating first menu
menu1 = new JMenu("File");
mItem1 = new JMenuItem("New");
mItem2 = new JMenuItem("Open");
mItem3 = new JMenuItem("Save");
//Adding menu items to the menu
menu1.add(mItem1);
menu1.add(mItem2);
menu1.add(mItem3);
//Creating a second sub-menu
menu2 = new JMenu("Save-as");
mItem5 = new JMenuItem(".jpeg");
mItem6 = new JMenuItem(".png");
mItem7 = new JMenuItem(".pdf");
//Adding menu items to the sub-menu
menu2.add(mItem5);
menu2.add(mItem6);
menu2.add(mItem7);
//Adding the sub-menu to the first menu
menu1.add(menu2);
//Adding our menu to the menu bar
menuBar.add(menu1);
//Adding my menu bar to the frame
jf.add(menuBar, BorderLayout.NORTH);
jf.setSize(330,250);
jf.setVisible(true);
}
}
Advertisement
iimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Menu3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class Combo
class A implements ActionListener
{
JFrame jf;
JMenuBar menuBar;
JMenu menu1, menu2;
JMenuItem mItem1, mItem2, mItem3, mItem4, mItem5, mItem6, mItem7;
JFileChooser fc;
JLabel label1;
A()
{
jf = new JFrame("JDialog");
//Creating a menu bar
menuBar= new JMenuBar();
//Creating first menu
menu1 = new JMenu("File");
mItem1 = new JMenuItem("New");
mItem2 = new JMenuItem("Open");
mItem3= new JMenuItem("Save");
mItem4 = new JMenuItem("Exit");
//Adding menu items to the menu
menu1.add(mItem1);
menu1.add(mItem2);
menu1.add(mItem3);
menu1.add(mItem4);
//Creating a second sub-menu
menu2 = new JMenu("Save-as");
mItem5 = new JMenuItem(".jpeg");
mItem6 = new JMenuItem(".png");
mItem7 = new JMenuItem(".pdf");
//Adding menu items to the sub-menu
menu2.add(mItem5);
menu2.add(mItem6);
menu2.add(mItem7);
//Adding the sub-menu to the first menu
menu1.add(menu2);
//Adding the exit menu item at the last of menu
menu1.add(mItem4);
//Adding our menu to the menu bar
menuBar.add(menu1);
//Adding listener to "Open" and "Exit" menu items
mItem2.addActionListener(this);
mItem4.addActionListener(this);
label1 = new JLabel("", JLabel.CENTER);
jf.add(menuBar, BorderLayout.NORTH);
jf.add(label1,BorderLayout.CENTER);
jf.setSize(370,270);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Open"))
{
//Creating an object of JFileChooser
fc= new JFileChooser("Open a file");
//Showing a file chooser dialog window
int returnValue = fc.showOpenDialog(jf);
if(returnValue == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
label1.setText(file.getAbsolutePath());
System.out.println(file.getName());
}
fc.setSize(200,200);
fc.setVisible(true);
}
if(ae.getActionCommand().equals("Exit"))
{
System.exit(0);
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement