tailieunhanh - Linux programming unleash phần 8

Đ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 new public void mouseClicked MouseEvent e 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 we import all classes and interfaces from the packages for the graphic components and containers and for the event-handling classes and interfaces and from the package to get the Swing or JFC classes. In Listing we define a static public main method that is executed when we compile and run this sample program by typing javac 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 . The sample program in Listing that implements a

TỪ KHÓA LIÊN QUAN