0% found this document useful (0 votes)
8 views4 pages

PROGRAM8

This document contains a Java program that creates a GUI with menu bars and pull-down menus using Swing. It includes functionalities for file operations (New, Open, Save, Exit), editing options (Cut, Copy, Paste, Select All), and a Help section with an About dialog. The program is structured with a JFrame containing a JTextArea and a JMenuBar with various JMenuItems linked to action listeners for user interaction.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

PROGRAM8

This document contains a Java program that creates a GUI with menu bars and pull-down menus using Swing. It includes functionalities for file operations (New, Open, Save, Exit), editing options (Cut, Copy, Paste, Select All), and a Help section with an About dialog. The program is structured with a JFrame containing a JTextArea and a JMenuBar with various JMenuItems linked to action listeners for user interaction.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM-8

WRITE A JAVA PROGRAM TO CREATE MENU BARS AND PULL DOWN MENUS
SOURCE CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class jframe implements ActionListener
{
String text="";
JTextArea content;
JFrame jf;
JMenuBar menuBar;
JMenu fileMenu,editMenu,helpMenu;
JMenuItem newItem, openItem, saveItem, exitItem, cutItem, copyItem, pasteItem, selectallItem, aboutItem;
JButton submit;
public jframe()
{ jf=new JFrame("Menu Program");
// Create labels and text fields
content=new JTextArea(500,500);
content.setFont(new Font("Serif",Font.PLAIN,20));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
newItem = new JMenuItem("New");
newItem.addActionListener(this);
openItem = new JMenuItem("Open");
saveItem = new JMenuItem("Save");
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);

fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);

editMenu = new JMenu("Edit");


cutItem = new JMenuItem("Cut");
cutItem.addActionListener(this);
copyItem = new JMenuItem("Copy");
copyItem.addActionListener(this);
pasteItem = new JMenuItem("Paste");
pasteItem.addActionListener(this);
selectallItem = new JMenuItem("Select All");
selectallItem.addActionListener(this);

editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
editMenu.add(selectallItem);

helpMenu = new JMenu("Help");


aboutItem = new JMenuItem("About");
aboutItem.addActionListener(this);
helpMenu.add(aboutItem);

menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
jf.setJMenuBar(menuBar);
jf.add(new JScrollPane(content));
jf.setVisible(true);
jf.setSize(500, 500);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("New"))
{
content.setText("");
}
else if(ae.getActionCommand().equals("Exit"))
{
System.exit(0);
}
else if(ae.getActionCommand().equals("Cut"))
{
text=(String)content.getSelectedText();
content.replaceSelection("");
}
else if(ae.getActionCommand().equals("Copy"))
{
text=(String)content.getSelectedText();
}
else if(ae.getActionCommand().equals("Paste"))
{
int pos=content.getCaretPosition();
content.insert(text,pos);
}
else if(ae.getActionCommand().equals("Select All"))
{
content.selectAll();
}
else if(ae.getActionCommand().equals("About"))
{
JOptionPane.showMessageDialog(jf, "Menu Bar and Pull DownMenu Program");
}
}
}
class PL8
{
public static void main(String args[])
{
jframe j=new jframe();
}
}
PROGRAM-8-OUTPUT

You might also like