Compile and run the program. Observe the horizontal menu bar at the top which ha
ID: 3532380 • Letter: C
Question
Compile and run the program. Observe the horizontal menu bar at the top
which has only one menu choice, Notes. We will be adding an item to this
menu bar called Viewsthat has two submenus. One named Look and Feel and
one named Scroll Bars. The submenu named Look and Feel lets the user
change the look and feels: Metal, Motif, and Windows. The submenu named
Scroll Bars offers the user three choices: Never, Always, and As Needed.
When the user makes a choice, the scroll bars are displayed according to the
choice.
3. We want to logically break down the problem and make our program easier to
read and understand. We will write separate methods to create each of the vertical menus. The three methods that we will be writing are createViews(),
createScrollBars(), and createLookAndFeel(). The method
headings with empty bodies are provided.
4. Let
Explanation / Answer
Hey this is the modified code..I have complied and run it..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIHelp extends JFrame
{
//constants for set up of note taking area
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 13;
public static final int CHAR_PER_LINE = 45;
//objects in GUI
private JTextArea theText; //area to take notes
private JMenuBar mBar; //horizontal menu bar
private JPanel textPanel; //panel to hold scrolling text area
private JMenu notesMenu; //vertical menu with choices for notes
//****THESE ITEMS ARE NOT YET USED- YOU WILL BE CREATING THEM IN THIS LAB
private JMenu viewMenu; //vertical menu with choices for views
private JMenu lafMenu; //vertical menu with look and feel
private JMenu sbMenu; //vertical menu with scroll bar option
private JScrollPane scrolledText; //scroll bars
//default notes
private String note1 = "No Note 1.";
private String note2 = "No Note 2.";
//constructor
public GUIHelp()
{
//create a closeable JFrame with a specific size
super("Note Taker");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//set layout of the window
setLayout(new BorderLayout());
//creates the vertical menus
createNotes();
createViews();
//creates horizontal menu bar and adds vertical menus to it
mBar = new JMenuBar();
mBar.add(notesMenu);
mBar.add(viewMenu);
//****ADD THE viewMenu TO THE MENU BAR HERE
setJMenuBar(mBar);
//creates a panel to take notes on
textPanel = new JPanel();
textPanel.setBackground(Color.BLUE);
theText = new JTextArea(LINES, CHAR_PER_LINE);
theText.setBackground(Color.WHITE);
//****CREATE A JScrollPane OBJECT HERE CALLED scrolledText AND PASS IN theText, THEN
//****CHANGE THE LINE BELOW BY PASSING IN scrolledText
textPanel.add(theText);
add(textPanel, BorderLayout.CENTER);
//scrolledText = new JScrollPane();
//theText = new JTextArea(LINES, CHAR_PER_LINE);
//theText.setBackground(Color.WHITE);
//scrolledText.add(theText);
// scrolledText.setVisible(false);
}
//creates vertical menu associated with Notes menu item on menu bar
public void createNotes()
{
notesMenu = new JMenu("Notes");
JMenuItem item;
item = new JMenuItem("Save Note 1");
item.addActionListener(new MenuListener());
notesMenu.add(item);
item = new JMenuItem("Save Note 2");
item.addActionListener(new MenuListener());
notesMenu.add(item);
item = new JMenuItem("Open Note 1");
item.addActionListener(new MenuListener());
notesMenu.add(item);
item = new JMenuItem("Open Note 2");
item.addActionListener(new MenuListener());
notesMenu.add(item);
item = new JMenuItem("Clear");
item.addActionListener(new MenuListener());
notesMenu.add(item);
item = new JMenuItem("Exit");
item.addActionListener(new MenuListener());
notesMenu.add(item);
}
//creates vertical menu associated with Views menu item on the menu bar
public void createViews()
{
viewMenu = new JMenu("Views");
createLookAndFeel();
createScrollBars();
viewMenu.add(lafMenu);
viewMenu.add(sbMenu);
}
//creates the look and feel submenu
public void createLookAndFeel()
{
lafMenu=new JMenu("Look And Feel");
JMenuItem item;
item=new JMenuItem("Metal");
item.addActionListener(new MenuListener());
lafMenu.add(item);
item=new JMenuItem("Mofit");
item.addActionListener(new MenuListener());
lafMenu.add(item);
item=new JMenuItem("Windows");
item.addActionListener(new MenuListener());
lafMenu.add(item);
}
//creates the scroll bars submenu
public void createScrollBars()
{
sbMenu=new JMenu("Scroll Bars");
JMenuItem item;
item=new JMenuItem("Never");
item.addActionListener(new MenuListener());
sbMenu.add(item);
item=new JMenuItem("Always");
item.addActionListener(new MenuListener());
sbMenu.add(item);
item=new JMenuItem("As Needed");
item.addActionListener(new MenuListener());
sbMenu.add(item);
}
private class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Save Note 1"))
{
note1 = theText.getText();
}
else if (actionCommand.equals("Save Note 2"))
{
note2 = theText.getText();
}
else if (actionCommand.equals("Clear"))
{
theText.setText("");
}
else if (actionCommand.equals("Open Note 1"))
{
theText.setText(note1);
}
else if (actionCommand.equals("Open Note 2"))
{
theText.setText(note2);
}
else if (actionCommand.equals("Exit"))
{
System.exit(0);
}
else if(actionCommand.equals("Metal"))
{
textPanel.setBackground(Color.GRAY);
}
else if(actionCommand.equals("Mofit"))
{
Color newColor = JColorChooser.showDialog(textPanel,"Choose Background Color",
textPanel.getBackground());
textPanel.setBackground(newColor);
}
else if(actionCommand.equals("Windows"))
{
textPanel.setBackground(java.awt.SystemColor.activeCaption);
}
else if(actionCommand.equals("Never"))
{
// textPanel.setVisible(true);
// scrolledText.setVisible(false);
}
else if(actionCommand.equals("Always"))
{
//textPanel.setVisible(false);
//scrolledText.setVisible(true);
//add(scrolledText,BorderLayout.CENTER);
}
else if(actionCommand.equals("As Needed"))
{
//not clear what to do..please explain it ..
}
//****ADD 6 BRANCHES TO THE ELSE-IF STRUCTURE TO ALLOW ACTION TO BE PERFORMED FOR EACH
//MENU ITEM YOU HAVE CREATED
else
theText.setText("Error in memo interface");
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
GUIHelp gui=new GUIHelp();
gui.setVisible(true);
}
}
);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.