Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Assignment5 Notes 
 
The main topics of discussion for this Assignment are: 
1. learn to use JMenuBar, JMenu, JMenuItem, and JTextArea 
2. learn to use builtin Vector class that incorporates array-like handling and ease of 
Serializaton of objects. 
 
Working with Menus 
The most common Graphical User Interface in today’s Windows-based applications it the 
MenuBar which we see in all Microsoft Office applications(displaying the words File, 
Edit, View, Insert, etc…)  These graphical tools are available in Java as well.  The figure 
below helps illustrate what is described in the following text.  The JMenuBar is a place 
across the top of a JFrame(just below the title bar) to attach a list of JMenu components.  
A JMenu is a list of JMenuItems.  A JMenuItem represents one of several selection 
choices that will dropdown(or pop out) when the menu it belongs to is clicked.   
                                                                                   Example of a JFrame with a 
JMenuBar 
 
                    Jframe 
                             JMenuBar 
                                   JMenu 
                                       JMenuItem 
 
 
 
 
 
 
 
 
 
For this type of interface, your processing class is built on the JFrame class.  First, you 
call the constructor of the super class(JFrame).  Then, you should construct the 
JMenuItems.  The JMenuItem constructor is passed a String for the label displayed on the 
component.  This label also is the ActionCommand string which can be tested for in a 
listener method.  Next a listener is added to the JMenuItem.  Finally, the JMenuItem is 
added to its parent JMenu.  This is repeated for all the JMenuItem components to be 
placed on the JMenu.  The code would look something like the code below: 
 
        JMenu elementsMenu = new JMenu("Elements"); 
        JMenuItem addElement = new JMenuItem("Add Element"); 
        addElement.addActionListener(ml); 
        elementsMenu.add(addElement); 
 
After all JMenuItem components have been added to the JMenu, the JMenu is added to 
the JMenuBar.  Note that a JMenu can also be added as one of the members of a JMenu, 
thus allowing a Menu selection to be another menu (which will popout  when clicked.)!  
Finally, the JMenuBar is added to the JFrame with the method setMenuBar of the JFrame 
class.  The code for the last steps would look something like the code below: 
 
        JMenuBar bar = new JMenuBar( ); 
        bar.add(elementsMenu); 
        setJMenuBar(bar); 
 
The listener class method receives an ActionEvent whenever one of the JMenuItem 
components is clicked.  The clicking of a JMenu component to open the list of choices on 
the menu is not an ActionEvent.  The listener can obtain the name of the source of the 
ActionEvent by using the method getActionCommand(): 
 
        String source = event.getActionCommand(); 
If two MenuItems happen to have the same text for their display, an alternate string can 
be set for the MenuItem to identify it uniquely to a listener using the method 
setActionCommand(String alt_name).   
 
The JTextArea component is used to display text on the screen within a JFrame as part(or 
all) of the contentPane.  It is easily constructed with row and size parameters, then placed 
into the contentPane of the JFrame  with code such as: 
 
         JTextArea ta = new JTextArea(10,50); 
         frame.getContentPane().add(ta); 
 
When the program needs to display some text in the component, the method 
setText(String text) of the JTextArea class is used, similar to: 
 
         ta.setText(message); 
 
 
Vectors  
A Vector is a list of similar objects(like an array or ArrayList)  that dynamically 
increases when needed. The Vector class implements Serializable interface, and is a 
growable array of objects. Like an array, it contains components that can be accessed 
using an integer index. However, the size of a Vector can grow or shrink as needed to 
accommodate adding and removing items after the Vector has been created.  The 
capacity is always at least as large as the vector size; it is usually larger because as 
components are added to the vector, the vector's storage increases in chunks the size of 
capacityIncrement. An application can increase the capacity of a vector before 
inserting a large number of components; this reduces the amount of incremental 
reallocation. It can be constructed with an initial capacity N, and when the Nth element is 
added, the vector automatically doubles in size. Some of the methods that come with the 
class are: 
– Vector(int initialSize)  - Constructs an empty vector with the specified 
initial capacity. 
– void addElement(Obj) - Adds the specified component to the end of this 
vector, increasing its size by one. 
–  int capacity() - Returns the current capacity of this vector. 
– void copyInto(Obj[] array) - Copies the components of this vector into the 
specified array. 
– Obj elementAt(int pos) - Returns the component at the specified index. 
–  int indexOf( Obj) - Searches for the first occurence of the given argument, 
testing for equality using the equals method. 
– Obj firstElement() - Returns the first component (the item at index 0) of 
this vector. 
– Obj lastElement() - Returns the last component of the vector. 
– int size() - Returns the number of components in this vector. 
– void trimToSize() - Trims the capacity of this vector to be the vector's 
current size. 
If you are reading/writing a array or list of objects from/to a file,  The Vector is a handy 
tool  for ease of I/O and for keeping track of how many objects you have. 
See the Sample Project MyElements4 in the Instructors Folder, for a detailed example of 
the use of all the components and objects discussed above.