Danh mục

Java Programming for absolute beginner- P14

Số trang: 20      Loại file: pdf      Dung lượng: 408.17 KB      Lượt xem: 14      Lượt tải: 0    
Hoai.2512

Phí tải xuống: 18,000 VND Tải xuống file đầy đủ (20 trang) 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- P14: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- P14 JavaProgAbsBeg-06.qxd 2/25/03 8:52 AM Page 218 218 Telling the Story: Creating the MadLib Game Frame Java Programming for the Absolute Beginner The MadLib.java program uses a MadDialog object to retrieve user input. The MadLib class extends Frame. It listens for when the user closes the MadDialog dia- log box and then builds the story and displays it in its un-editable TextArea. The MadDialog Frame itself will remain hidden until after the story is already built. Here’s why. The MadDialog object is modal and it is shown first by calling dia- log.setVisible(true). It will maintain focus until it is hidden again. When the user clicks the x on the MadDialog window, the buildStory() method is called before the dialog is actually closed. Once the story is done, the MadDialog will dis- appear and the MadLib will display the completed story. The buildStory() method itself works by creating an array of Strings that rep- resent segments of the story that break where words should be inserted. The String[] getStringArray() method defined in the MadDialog class returns the String array sorted in the order they should be inserted into the story. This makes the two arrays’ indices correspond with each other, so they are processed in a for loop to build the story. Here is the source for MadLib.java. /* * MadLib * A MadLib game */ import java.awt.*; import java.awt.event.*; public class MadLib extends Frame implements WindowListener { private TextArea display; private MadDialog dialog; public MadLib() { super(“MadLib Game”); display = new TextArea(““, 7, 60, TextArea.SCROLLBARS_VERTICAL_ONLY); display.setFont(new Font(“Timesroman”, Font.PLAIN, 16)); display.setEditable(false); add(display); addWindowListener(this); setLocation(100, 150); pack(); dialog = new MadDialog(this); dialog.addWindowListener(this); dialog.setLocation(150, 100); dialog.setVisible(true); setVisible(true); } TEAM LinG - Live, Informative, Non-cost and Genuine!Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. JavaProgAbsBeg-06.qxd 2/25/03 8:52 AM Page 219 219 public static void main(String args[]) { MadLib ml = new MadLib(); Chapter 6 } private void buildStory() { String story = ““; String[] segs = {“One fine “, “ night, a “, “ named “, “ “, “ had a dream. It was the “, “ “, “ dream since “, “ dreamt that a “, “ “, “ “, “ and “, “ on a “, “ “, Creating a GUI Using the Abstract Windowing Toolkit “. In this dr ...

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