Đang chuẩn bị liên kết để tải về tài liệu:
Java™ How to Program ( Deitel - Deitel) - Phần 8

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Tham khảo sách 'java™ how to program ( deitel - deitel) - phần 8', 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ả | 1 Fig. 4.20 DrawPanelTest.java 2 Application to display a DrawPanel. 3 import javax.swing.JFrame 4 5 public class DrawPanelTest 6 7 public static void main String args 8 9 create a panel that contains our drawing 10 DrawPanel panel new DrawPanel 11 12 create a new frame to hold the panel 13 JFrame application new JFrame 14 15 set the frame to exit when it is closed 16 application.setDefaultCloseOperation JFrame.EXIT ON CLOSE 17 18 application.add panel add the panel to the frame 19 application.setSize 250 250 set the size of the frame 20 application.setVisible true make the frame visible 21 end main 22 end class DrawPanelTest Line 6 uses the keyword extends to indicate that class DrawPanel is an enhanced type of JPanel. The keyword extends represents a so-called inheritance relationship in which our new class DrawPanel begins with the existing members data and methods from class JPanel. The class from which DrawPanel inherits JPanel appears to the right of keyword extends. In this inheritance relationship JPanel is called the superclass and DrawPanel is called the subclass. This results in a DrawPanel class that has the attributes data and behaviors methods of class JPanel as well as the new features we are adding in our DrawPanel class declaration specifically the ability to draw two lines along the diagonals of the panel . Inheritance will be explained in more detail in Chapter 9. Page 159 Every JPanel including our DrawPanel has a paintComponent method lines 922 which the system automatically calls every time it needs to display the JPanel. Method paintComponent must be declared as shown on line 9otherwise the system will not call the method. This method is called when a JPanel is first displayed on the screen when it is covered then uncovered by a window on the screen and when the window in which it appears is resized. Method paintComponent requires one argument a Graphics object that is provided for you by the system when it calls paintComponent. The first .