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
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 ...
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ìm kiếm theo từ khóa liên quan:
Nhập môn lập trình lập trình website lập trình ngôn ngữ html giáo trình php lập trình C++Gợi ý tài liệu liên quan:
-
Đề cương chi tiết học phần Cấu trúc dữ liệu và giải thuật (Data structures and algorithms)
10 trang 317 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 184 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 166 0 0 -
Giáo trình nhập môn lập trình - Phần 22
48 trang 138 0 0 -
[Thảo luận] Học PHP như thế nào khi bạn chưa biết gì về lập trình?
5 trang 131 0 0 -
8 trang 76 0 0
-
Bài giảng Nhập môn lập trình - Chương 12: Quản lý bộ nhớ
23 trang 63 0 0 -
Giáo trình Lập trình Website (Nghề Tin học ứng dụng - Trình độ Cao đẳng) - CĐ GTVT Trung ương I
156 trang 56 2 0 -
42 trang 48 0 0
-
Báo cáo đồ án môn học II: Thiết kế website bán đồng hồ
25 trang 47 0 0