tailieunhanh - Object Oriented Programming using Java phần 6
Bạn có thể cần một tài liệu tham khảo, ví dụ, nếu bạn muốn sử dụng tên đầy đủ của một biến dụ, bởi vì tên của các biến Ví dụ đơn giản là ẩn bởi một biến địa phương hoặc tham số. Java cung cấp một biến, đặc biệt được xác định trước có tên "này" mà bạn có thể sử dụng cho mục đích đó. | private static class HelloWorldDisplay extends JPanel public void paintComponent Graphics g g Hello World 20 30 The paintComponent method is called by the system when a component needs to be painted on the screen. In the JPanel class the paintComponent method simply fills the panel with the panel s background color. The paintComponent method in HelloWorldDisplay begins by calling g . This calls the version of paintComponent that is defined in the superclass JPanel that is it fills the panel with the background color. Then it calls to paint the string Hello World onto the panel. The net result is that whenever a HelloWorldDisplay is shown on the screen it displays the string Hello World . We will often use JPanels in this way as drawing surfaces. Usually when we do this we will define a nested class that is a subclass of JPanel and we will write a paintComponent method in that class to draw the desired content in the panel. Components and Layout Another way of using a JPanel is as a container to hold other components. Java has many classes that define GUI components. Before these components can appear on the screen they must be added to a container. In this program the variable named content refers to a JPanel that is used as a container and two other components are added to that container. This is done in the statements displayPanel okBUtton Here content refers to an object of type JPanel later in the program this panel becomes the content pane of the window. The first component that is added to content is displayPanel which as discussed above displays the message Hello World . The second is okButton which represents the button that the user clicks to close the window. The variable okButton is of type JButton the Java class that represents push buttons. The BorderLayout stuff in these statements has to do with how the two .
đang nạp các trang xem trước