Đang chuẩn bị liên kết để tải về tài liệu:
Linux programming unleash phần 8
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Điều duy nhất ở tất cả các phức tạp về ví dụ này là xử lý các sự kiện liên quan đến người sử dụng click chuột vào đối tượng nút. Tuy nhiên, những sự kiện này được xử lý với chỉ một vài dòng mã trong cùng một cách mà chúng ta xử lý các sự kiện trong chương trình ví dụ AWT trong | Programming the User Interface 590 Part IV The only thing at all complicated about this example is handling the events associated with the user clicking on the button object. However these events are handled with only a few lines of code in the same way that we handled events in the AWT example program in Listing 32.9 countButton.addMouseListener new java.awt.event.MouseAdapterO public void mouseClicked MouseEvent e countLabel.setText button clicked 11 count times The addMouseListener method is used to associate an instance of the MouseListener with a button or any other component . Now it is possible to define a separate class that implements the MouseListener listener interface which requires defining five methods mouseClicked mouseEntered mouseExited mousePressed and mouseReleased . However there is a MouseAdapter utility class that defines do nothing versions of these five methods. In this code example we are creating an anonymous inner class for example it is an inner class with no name associated with it that overrides the mouseClicked. Our mouseClicked method uses the setText method to change the displayed text of a label. In Listing 32.13 we import all classes and interfaces from the packages java.awt for the graphic components and containers and java.awt.event for the event-handling classes and interfaces and from the package javax.swing to get the Swing or JFC classes. In Listing 32.11 we define a static public main method that is executed when we compile and run this sample program by typing javac JFCSample.java java JFCSample The JFCSample program does not handle window closing events so it must be terminated by typing Ctrl C in the terminal window where you run the program. Writing a Chat Program Using JFC We will develop the ChatJFC program in this section. It is almost identical to the ChatAWT program only it uses the JFC classes instead of the AWT classes. Both programs were shown in Figure 32.2. The sample program in Listing 32.12 that implements a