Design a class called MyJFrame to produce output as shown in Figures 1 – 7. This
ID: 3822525 • Letter: D
Question
Design a class called MyJFrame to produce output as shown in Figures 1 – 7. This frame must be inherited from the class JFrame. The frame must contain only a title as shown in Figure 1
Figure 1
Create a menu bar object and set it in place. Add menu choices onto the menu bar as shown in Figure 2.
Figure 2
Add menu items to each menu choice as shown in Figure 3 and Figure 4. Place separators where indicated in the Figures.
Figure 3
For the menu choice Tools, create a cascading menu for the menu choice Edit. This submenu must contain menu items as shown in the Figure 4.
Figure 4
The following is an outline of the code for the class MyJFrame. You may consult your notes and class handouts for further information.
1. Use an interface to maintain constant values and generic methods
2. Use arrays to contain:
(a) The list of menu choices
(b) The list of menu items for the menu choices – File, Tool, and Edit.
// Area reserve for importing classes.
class MyJFrame extends JFrame
{
JMenuBar mbar;
JMenu jm;
JMenuItem mi;
MyJFrame(String title)
{
super(title);
mbar = new JMenuBar(); //Create menu bar and set it
setJMenuBar(mbar);
buildMenu(); // Call method which actually builds the menu
}
void buildMenu()
{
for (int i = 0; i
{
jm = new JMenu(Constants.MENU[i]);
/* Determine the menu choice and create menu items for each menu
* choice and add each set of menu items to its respective menu choice
*/
switch(i)
{
case 0: // File
//Create separator or create file menu item and add each to the menu choice
break;
case 1: //Tool
/* First, check for the string Edit. When found, use it to create a sub menu
object. Next, create menu items, in this case – Copy and Paste as menu
items. Add these menu items to the sub menu. Finally, add this sub menu to
the menu choice Tool.
If the string is not Edit, then it must be a menu item for menu choice Tool.
Create each menu item objects, in this case – Sort and Search, and add each
to the menu choice Tool.
*/
break;
case 2: // Help, at this point no menu item will be attached
break;
default: // No action taken at this time
break;
}// End switch
mbar.add(jm); // Add each menu choice with its menu items to the menu bar.
}// End for
}// End build
}// End class MyJFrame
Part II
Additing Functionality to your GUI
Create an anonymous WindowListener class such that when you click the close button on your frame, a child frame (JOptionPane frame) appears, similar to Figure 5 Remember to import the class WindowAdapter and the class WindowEvent.
Figure 5
Go to the class MyJFrame and register each menu item to the ActionListener object. The general format is:
mi.addActionListener(new Actions());
where the class Actions is partially defined below
The class Actions determines which menu item has been activated. The general format of this class is as follows:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class Actions implements ActionListener
{
DisplayText dt;
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equalsIgnoreCase("New"))
{
dt = new DisplayText("Untitled..... ", "");
}
else if(e.getActionCommand().equalsIgnoreCase("Open"))
{
BasicFile f = new BasicFile();
f.selectFile();
dt = new DisplayText(f.getName(), f.getContent());
}
//::::::::::::
else
;
/*
*
*::::::::::::::::::::::::
*
*/
}
}
(b) Implement the class DisplayText that was given to you. This class accepts two strings - the first represents the name of a file and the second string, the contents of the file. This class constructs a frame object from the class MyJFrame accepts the first string which is used as a title. Place the second string is the text to be displayed in a JScrollPane.
(c) Use the class called BasicFile from Assignment 4. This file uses the FileChooser class to create a File dialog. At this point the three most important methods are:
· selectFile() which creates the file dialog, from which you can select a file.
· getName() returns the name of the file you selected
· getContents() returns the contents of the file you selected.
Running the Program
When you run the program and select File – New, your output should be similar to Figure 6.
Figure 6
Similarly, when you select File – Open, your output should be similar to Figure 7.
Figure 7
This should be familiar to you. If you select a file its contents would be displayed as shown in Figure 8.
Figure 8
· Implement the Copy - Paste options. That is use the mouse to highlight a portion of the displayed text, and copy it to a different location in the document.
Part III
Adding buttons to the frame.
To add buttons to the frame you will need the following classes:
(a) JFrame
(b) GridBagLayout
(c) GridbagConstraints
(d) Insets
Produce output similar to Figure 5
Figure 5.
You may use the following code to ASSIST you.
for (int i = 0; i < button.length; i++)
{
b = new JButton(button[i]);
switch(i)
{
case 0:
b = new JButton(button[i]);
constraints.gridx = 0; // Specify the (x,y) coordinate for this component
constraints.gridy = 0; // (x,y) = (0,0)
//constraints.insets = new Insets(0,50,0,0);
break;
case 1:
b = new JButton(button[i]);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.insets = new Insets(20,0,40,200);
break;
case 2:
b = new JButton("(0,1)", new ImageIcon(button[i]));
constraints.gridx = 0;
constraints.gridy = 1;
constraints.insets = new Insets(20,300,0,0);
break;
case 3:
b = new JButton(button[i]);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.ipadx = 200;
//constraints.insets = new Insets(25,50,0,50);
break;
}
Write a class called Browser called Browser.java, When the horizontal button marked Browser on your frame is pressed, the browser as shown in Figure 10 .
Figure 10
Part I.
(a) Create a frame object from MyJFrame object on which to mount your browser interface.
(b) Obtain the content pane of the frame.
Part II
(a) Create a JTextFiled object. You may initialize it to say http://www
(b) Use the BorderLayout manager to place the text field to the NORTH of the frame
Part III
(a) Create a JEditorPane object
(b) Use the method setEditable from this class, false. Note JEditorPane is preferable since it can be used to display text and images
(c) Create a JScrollPane object and place the editor pane object in this object.
(d) Use the BorderLayout manager to place the JScrollPane to the CENTER of the frame
Part IV
(a) Set the orientation and size of the frame
(b) Remember to set the frame visible.
LASTLY
Write a class called MyGraphics.java, When the image button on your frame is pressed, the a new frame appears as shown in Figure 11.
Figure 11
In carrying out this exercise bear the following in mind:
Use the class JFrame to create your drawing.
Create the star and the front of the house using the class Polygon
Create the door of the house using the method fillRect. Finish the house using freehand drawing. Use freehand drawing for the tree also.
Use the Font class to write the label - use a font that you like- a style and size that are appealing to you.
In your presentation, choose colors that appeal to you also.
NOTE: YOU ARE NOT RESPONSIBLE FOR ADDING FUNCTIONALITY TO ITEMS THAT YOU ARE NOT ASKED TO DO.
My First Java Graphical user Interface ProgramExplanation / Answer
Create submenu:
Add checkbox in tool menu:
Create an anonymous WindowListener class such that when you click the close button on your frame, a child frame (JOptionPane frame) appears:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.