Danh mục

giáo trình Java By Example phần 4

Số trang: 35      Loại file: pdf      Dung lượng: 90.16 KB      Lượt xem: 21      Lượt tải: 0    
Thu Hiền

Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu giáo trình java by example phần 4, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 4 } public boolean action(Event evt, Object arg) { repaint(); return true; } } Tell Java that the applet uses the classes in the awt package. Tell Java that the applet uses the classes in the applet package. Derive the ListApplet class from Javas Applet class. Declare the list object. Override the init() method. Create the list object. Add items to the list object. Add the list to the applet. Set the applets size. Override the paint() method. Draw a label in the applets display. Get the selected item from the list box. If there is no item selected, set the string to None. Display the selected item. Override the action() method. Force the applet to repaint its display. Tell Java that the event was handled okay.When you run ListApplet with Appletviewer, you see the window shown in Figure 20.7. When youdouble-click an item in the list, Java calls the applets action() method in which the applet calls therepaint() method. This forces Java to call the paint() method, where the applet retrieves theselected item and displays it.Figure 20.7 : The scrolling list in this applet lets you choose a single musical artist.Notice the call to resize() in the init() method. The resize() method enables you to set theapplet to any size you wish. This size overrides any size setting thats included in the HTML documentthat ran the applet. http://www.ngohaianh.infoThe TextArea ControlThroughout this book, youve been using the TextField control to retrieve information from the user.In most cases, the TextField control works great, but it does have some limitations, the most seriousbeing the fact that it can display only one line of text at a time. There may be situations where youd liketo display one or more paragraphs of text in your applet, in a control that enables the user to edit existingtext, as well as enter his or her own text. This is where the TextArea control is useful. The TextAreacontrol is a text box that acts like a simple word processor. When you display a text box, the user cantype and edit multiple lines of text.To create a TextArea control, you call the classs constructor, like this: TextArea textArea = new TextArea(str, rows, cols);This constructors three arguments are the string to display in the control, the number of rows in thecontrol, and the number of columns. As with the other controls, after you create the TextField object,you add it to the applet by using the add() method.Example: Creating a TextArea ControlAs an example, suppose that you need to create a TextArea control thatll start off displaying eightlines of text. Listing 20.6 is an applet, called TextAreaApplet, that creates a TextArea control thatdisplays eight lines of text. Figure 20.8 shows what the applet looks like running under Appletviewer.When you run the applet, click on the TextArea controls box and try editing the text in the window.As youll discover, you not only can edit the existing text, but also add new text.Figure 20.8 : TextAreaApplet applet run-ning under Appletviewer. Listing 20.6 TEXTAREAAPPLET.JAVA: The TextAreaApplet Applet. import java.awt.*; import java.applet.*; public class TextAreaApplet extends Applet { TextArea textArea; http://www.ngohaianh.info public void init() { String s = This is an example of a ; s += textarea control, which is not ; s += unlike a textfield control. ; s += The big difference is that a ; s += textarea control can hold many ; s += lines of text, whereas a ; s += textfield control deals with ; s += only one line of text at a time. ; textArea = new TextArea(s, 9, 30); add(textArea); resize(300, 180); }}Tell Java that the applet uses the classes in the awt package.Tell Java that the applet uses the classes in the applet package.Derive the TextAreaApplet class from Javas Applet class.Declare the TextArea object.Override the init() method.Create the string to display in the control.Create the TextArea object.Add the control to the applet. http://www.ngohaianh.info Set the applets size. TIP If you look at how the TextArea controls display string is created in TextAreaApplet, youll see that you can store multiple lines of text into a sin ...

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