Lab 6

263 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
263
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lab 6

  1. 1. Lab 6CSE 115Spring 2013Victoria Minorczykvaminorc@buffalo.eduDavis 302 | Thursday 1-1:50pm
  2. 2. Java Collections• There are some classes in the Java Libraries which implement the interface Collection<E>.• A collection can manage several references to the same type of object at once!• For this lab, you will use the ArrayList<String> collection to store adverbs, nouns, ect…• ArrayList<String> has a ‘get(int index)’ method, which returns the element at thespecified position in the list.• Collections also has a static method named ‘shuffle’. It is a static method, so to invoke it,you use the syntax <class name>.<method name>. For examplejava.util.Collections.suffle(list). The parameter ‘list’ is the list to be shuffled. You will usethis method in your lab.
  3. 3. Layout Managers• Java provides several classes (in the Java Libraries)which implement the ‘LayoutManager’ interface.• Remember, classes in the Java Libraries are classesdefined for you that you can use • Graphical components (ie. Jbuttons, Jlabels…) canprovide information about their preferred size andalignment, but a container’s (ie. JFrame’s) layoutmanager has the ‘final say’ on the size and positionof ALL components within that container (ie.JFrame).• There are LOTS and LOTS of Layout Managers!
  4. 4. Layout ManagersA container will not follow a certain layout until:-You create the container-You create the layout (instantiate it)-Set the layout of the containerFor example:JFrame f = new JFrame();GridBagLayout g = new GridBagLayout();f.setLayout(g);
  5. 5. BoxLayout ManagerGridBagLayout is a very complicated layout manager.For this lab, you use a much simpler layout called a‘BoxLayout’ Manager.With this Layout, you can stacks components in acontainer OR place them in a rowFor this lab, you will want to place them in a row. Keepreading to find out how!
  6. 6. BoxLayout ManagerThe box layout manager constructor has TWOparameters. The first is the container you want to set thelayout of. The second parameter specifies where eachcomponent will be added to container.For this lab, Dr. Alphonce requires you add componentsalong the BoxLayout.X-AXISLets look at the example on the next slide…
  7. 7. public class WindowWithBoxLayout {public WindowWithBoxLayout(){//create frameJFrame f = new JFrame("BoxLayout Demo");f.setVisible(true);//create layoutBoxLayout layout = new BoxLayout(f.getContentPane(),BoxLayout.X_AXIS);f.setLayout(layout); //Notice the arguments above ^ for BoxLayout//create some buttonsJButton b1 = new JButton("Button 1");JButton b2 = new JButton("Button 2");JButton b3 = new JButton("Button 3");//add buttons to framef.add(b1);f.add(b2);f.add(b3);f.pack();}
  8. 8. How to define EventHandlers!Every class that is to be an “EventHandler” MUSThave BOTH of the following:-implement a Listener (ie. ActionListener or MouseListener)-define the methods listed in that Listener Interface*Remember, an interface specifies a list of methodsignatures that a class is REQUIRED to have (even ifthe method body is left blank!For example…
  9. 9. public class EventHandler implements ActionListener {@Overridepublic void actionPerformed(ActionEvent arg0) {//code that should be executed when the button is//pressed goes HERE!}}*Remember EventHandlers MUST be added to your JButtons! Otherwiseyour JButtons will NOT do ANYTHING!For example:JButon b = new JButton();EventHandler listener = new EventHandler();b.addActionListener( listener );

×