TAILIEUCHUNG - Programming with Java, Swing and Squint phần 3

Hình : Ví dụ về một phương pháp lựa chọn mục trình đơn đăng nhập để đảm bảo Java mà chúng ta sẽ làm việc với một String ngay cả nếu getSelectedItem trả về một số loại khác của đối tượng. Thứ hai, thay vì chỉ sử dụng thêm để thêm các mục trình đơn trả lại bởi getSelectedItem để đăng nhập vào, | Figure Sample of a menu item selection log method to assure Java that we will be working with a String even if getSelectedItem returns some other type of object. Second rather than just using append to add the menu item returned by getSelectedItem to the log we instead first use the operator to concatenate the text of the menu item with the strange looking String literal n . To appreciate the purpose of the literal n recall that when we first introduced the concatenation operation in the example code I m touched numberOfClicks time s we stressed that if we did not explicitly include extra spaces in the literals I m touched and time s Java would not include any spaces between the words touched and time s and the numerical value of numberOfClicks. Similarly if we were to simply tell the append method to add the text of each menu item selected to the JTextArea by placing the command .toString in the menuItemSelected method Java would not insert blanks or start new lines between the items. The text of the menu items would all appear stuck together on the same line as shown in Figure . To have the items displayed on separate lines we must add something to each item we append that will tell the JTextArea to start a new line. The literal n serves this purpose. Backslashes are used in String literals to tell Java to include symbols that can t simply be typed in as part of the literals for various reasons. The symbol after each slash is interpreted as a code for what is to be included. The combination of the backslash and the following symbol is called an escape sequence. The n in the escape sequence n stands for new line . There are several other important escape sequences. can be included in a String literal to indicate that the quotation mark should be included in the String rather than terminating the literal. For example if you wanted to display the two lines 65 Class MenuLog - Displays a history of the items a user selects

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.