Compile, Execute and Test the Program Modify your program code for the JavaMenus
ID: 3774586 • Letter: C
Question
Compile, Execute and Test the Program
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.
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.
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);
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(
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);
}
}
After you have typed the given program code, build, compile and execute the program. Observe the graphical user interface that opens when the application is executed.
Test the operation of your program. Click the New menu item. The following message dialog box appears.
PROJECT ( Creating a Menu GUI Application )
Also the label within the application changes its text.
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.
Explanation / Answer
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JToolBar;
public class ToolbarsEx extends JFrame {
public ToolbarsEx() {
initUI();
}
public final void initUI() {
createToolBars();
setTitle("Toolbars");
setSize(360, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void createToolBars() {
JToolBar toolbar1 = new JToolBar();
JToolBar toolbar2 = new JToolBar();
ImageIcon newi = new ImageIcon("new.png");
ImageIcon open = new ImageIcon("open.png");
ImageIcon save = new ImageIcon("save.png");
ImageIcon exit = new ImageIcon("exit.png");
JButton newb = new JButton(newi);
JButton openb = new JButton(open);
JButton saveb = new JButton(save);
toolbar1.add(newb);
toolbar1.add(openb);
toolbar1.add(saveb);
JButton exitb = new JButton(exit);
toolbar2.add(exitb);
exitb.addActionListener((ActionEvent event) ->
System.exit(0);
});
createLayout(toolbar1, toolbar2);
}
private void createLayout(JComponent... arg) {
Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(arg[0], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(arg[1], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addComponent(arg[1])
);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
ToolbarsEx ex = new ToolbarsEx()
ex.setVisible(true);
});
}
}
such that the program will include a new menu item and Observe the graphical user interface that opens when the application is executed.
Test the operation of your program. Click the New menu item. The following message dialog box appears.
PROJECT ( Creating a Menu GUI Application )Also the label within the application changes its text.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.