The document provides code for a Java application with a menu bar. It asks the user to modify the code to:
1. Add a new menu item called "Open" that displays an image when clicked.
2. Modify the "New" menu item to display a quote and related image when clicked.
3. Test the modified program.
It then asks the user to further modify the code to add a second column to the menu bar with items like "Welcome", "About", and duplicate the existing menu options.
Here is the given code, and the things I need to be done. Ive post.pdf
1. Here is the given code, and the things I need to be done. I've posted this about 3 times and
nobody has helped me yet..
import javax.swing.*;
import java.awt.event.*;
public class JavaMenus extends JFrame {
public JavaMenus()
{
super("Java Menu Example");
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem ItemNew = new JMenuItem("New");
ItemNew.setMnemonic('N');
file.add(ItemNew);
JLabel fileButton = new JLabel(new ImageIcon("dukejdk"));
JMenuItem ItemOpen = new JMenuItem("Open");
ItemOpen.setMnemonic('O');
file.add(ItemOpen);
JMenuItem ItemExit = new JMenuItem("Exit");
ItemExit.setMnemonic('x');
file.add(ItemExit);
final JLabel label1 = new JLabel(" Welcome");
add(label1);
this.setSize(100, 100);
setVisible(true);
ItemNew.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
label1.setText(" New");
JOptionPane.showMessageDialog(null, "New was Clicked",
"Result", JOptionPane.PLAIN_MESSAGE);
}
}
);
ItemOpen.addActionListener(
2. new ActionListener(){
public void actionPerformed(ActionEvent e)
{
label1.setText(" Open");
JOptionPane.showMessageDialog(null, "Open was Clicked",
"Result", JOptionPane.PLAIN_MESSAGE);
}
}
);
ItemExit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
label1.setText(" Exit");
JOptionPane.showMessageDialog(null, "Exit was Clicked",
"Result", JOptionPane.PLAIN_MESSAGE);
}
}
);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(file);
getContentPane();
setSize(250, 250);
setVisible(true);
}
public static void main(String[] args)
{
JavaMenus appMenu = new JavaMenus();
appMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Modify your program code for the JavaMenus.java file such that the program will include
a new menu item. To do this alter the menu component items scheme to appear as follows.
3. Open Item
A Graphic image appears drawn out in the Frame
New Item
A historical quote appears in the Frame along with a corresponding image related to the quote.
Example- picture of Steve Jobs along with a Jobs quote of sorts.
Edit Item
A message box appears with the user’s name.
Exit Item
The application exits.
Test your modified program.
STEP 5 Supplement the Program Code
Modify again your program code for the JavaMenus.java file such that the program will
include a new second column of menu items. To do this alter the menu bar such that it
will contain these items.
Welcome Item
A message box appears that describes the program.
About Item
A message box appears with your name, as the programmer and version number of the app.
Open Item
A Graphic image appears drawn out in the Frame
New Item
A historical quote appears in the Frame along with a corresponding image related to the quote.
Example- picture of Steve Jobs along with a Jobs quote of sorts.
Edit Item
A message box appears with the user’s name.
Exit Item
The application exits.
Solution
4. package test;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
public class JavaMenus extends JFrame {
public JavaMenus()
{
super("Java Menu Example");
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem ItemNew = new JMenuItem("New");
ItemNew.setMnemonic('N');
file.add(ItemNew);
JMenuItem ItemOpen = new JMenuItem("Open");
ItemOpen.setMnemonic('O');
file.add(ItemOpen);
JMenuItem ItemExit = new JMenuItem("Exit");
ItemExit.setMnemonic('x');
file.add(ItemExit);
final JLabel label1 = new JLabel(" Welcome");
add(label1);
this.setSize(100, 100);
setVisible(true);
ItemNew.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
label1.setText(" New");
setContentPane(new JLabel("Stay Hungry,Stay Foolish"));
setContentPane(new JLabel(new
ImageIcon("C:UsersComputerDownloadssteve.jpg")));
JOptionPane.showMessageDialog(null, "Stay Hungry,Stay Foolish",
"Result", JOptionPane.PLAIN_MESSAGE);
5. }
}
);
ItemOpen.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
label1.setText(" Open");
JFileChooser chooser=new JFileChooser();
int returnVal = chooser.showOpenDialog(new JFrame());
File file=chooser.getSelectedFile();
setContentPane(new JLabel(new ImageIcon(file.getAbsolutePath())));
}
}
);
ItemExit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
label1.setText(" Exit");
JOptionPane.showMessageDialog(null, "Exit was Clicked",
"Result", JOptionPane.PLAIN_MESSAGE);
}
}
);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(file);
getContentPane();
setSize(250, 250);
setVisible(true);
}
public static void main(String[] args)
{
6. JavaMenus appMenu = new JavaMenus();
appMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
don't know about what you mean by edit item