Danh mục tài liệu

Java Programming for absolute beginner- P16

Số trang: 20      Loại file: pdf      Dung lượng: 433.82 KB      Lượt xem: 21      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Programming for absolute beginner- P16:Hello and welcome to Java Programming for the Absolute Beginner. You probablyalready have a good understanding of how to use your computer.These days it’s hard to find someone who doesn’t, given the importanceof computers in today’s world. Learning to control your computer intimatelyis what will separate you from the pack! By reading this book, you learnhow to accomplish just that through the magic of programming.
Nội dung trích xuất từ tài liệu:
Java Programming for absolute beginner- P16 JavaProgAbsBeg-07.qxd 2/25/03 8:53 AM Page 258 258 The output for this application is shown in Figure 7.14. Within the itemState- Changed(ItemEvent) method, two string objects are built based on which event Java Programming for the Absolute Beginner occurred. The getStateChange() method determines whether the item was selected or deselected by comparing the returned value to ItemEvent.SELECTED or ItemEvent.DESELECTED. The program then obtains the value of the item depend- ing on what type of component it is. The getItem() method in the ItemEvent class returns an Object object that represents the item that triggered the event. For the Checkbox and the Choice, this works out great because the object returned is a string that can be added to the Event: string. For the List, how- ever, I had to use the code: list.getItem(((Integer)e.getItem()).intValue()) FIGURE 7.14 ItemEvents are triggered by Checkboxes, Choices, and Lists. because e.getItem() returns the Integer index of the List item that was either selected or deselected. I had to explicitly cast it to an Integer object and call its intValue() method, which returns an int type value of the Integer object. Then I had to take that int value and pass it into list.getItem(int) so that I could get the String value of the List item. Table 7.8 lists some of the more common Ite- mEvent fields and methods. TA B L E 7 . 8 I TEM E VENT F I E L D S AND METHODS Field or Method Description int DESELECTED Signifies that the ItemEvent occurred because an item was deselected. int SELECTED Signifies that the ItemEvent occurred because an item was selected. int getStateChange() Returns a value, either ItemEvent.DESELECTED or ItemEvent.SELECTED depending on what type of state change is associated with this ItemEvent. Object getItem() Returns an Object that represents the item whose state changed. TEAM LinG - Live, Informative, Non-cost and Genuine!Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. JavaProgAbsBeg-07.qxd 2/25/03 8:53 AM Page 259 259 Handling AdjustmentEvents Chapter 7 The AdjustmentListener interface listens for AdjustmentEvents, which are trig- gered by objects that are adjustable, such as the Scrollbar component. Adjust- mentListener has only one method, adjustmentValueChanged(AdjustmentEvent), which is invoked when the value of an adjustable object is changed, as you can probably guess from the name of the method. The AdjustmentTest application tests this: Advanced GUI: Layout Managers and Event Handling /* * AdjustmentTest * Demonstrates the AdjustmentListener Interface on a scroll bar */ import java.awt.*; import java.awt.event.*; public class AdjustmentTest extends GUIFrame implements AdjustmentListener { ...

Tài liệu được xem nhiều:

Tài liệu có liên quan: