Creating Custom Exception Classes Use the exception classes in the API whenever possible. Create custom exception classes if the predefined classes are not sufficient. Declare custom exception classes by extending Exception or a subclass of Exception
39 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 1028 | Lượt tải: 0
The manifest file must have an entry to contain the main class. For example, to run TicTacToe, you need to insert the following two lines in the manifest file: Main-Class: TicTacToe Sealed: true Run the .jar file using the java command from the directory that contains TicTacToe.jar, java -jar TicTacToe.jar TIP: You can write an installation...
42 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 931 | Lượt tải: 0
Example: Creating Multiple Windows This example creates a main window with a text area in the scroll pane, and a button named "Show Histogram." When the user clicks the button, a new window appears that displays a histogram to show the occurrence of the letters in the text area.
54 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 935 | Lượt tải: 0
In Chapter 12, you drew a StillClock to show the current time. The clock does not tick after it is displayed. What can you do to make the clock display a new current time every second? The key to making the clock tick is to repaint it every second with a new current time. You can use a timer to control how to repaint the clock.
37 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 911 | Lượt tải: 0
You learned how to create image icons and display image icons in labels and buttons. For example, the following statements create an image icon and display it in a label: ImageIcon icon = new ImageIcon("image/us.gif"); JLabel jlblImage = new JLabel(imageIcon); An image icon displays a fixed-size image. To display an image in a flexible size, ...
36 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 865 | Lượt tải: 0
Java uses the javax.swing.ImageIcon class to represent an icon. An icon is a fixed-size picture; typically it is small and used to decorate components. Images are normally stored in image files. You can use new ImageIcon(filename) to construct an image icon. For example, the following statement creates an icon from an image file us.gif in the...
37 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 872 | Lượt tải: 0
Framework-Based Programming, cont. Once you understand the concept of Java and object-orient programming, the most important lesson from now on is learning how to use the API to develop useful programs. The most effective way to achieve it is to imitate good examples. The book provides many carefully designed examples to demonstrate the conc...
50 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 1171 | Lượt tải: 0
Arrays are objects. An array is an instance of the Object class. Furthermore, if A is a subclass of B, every instance of A[] is an instance of B[]. Therefore, the following statements are all true: new int[10] instanceof Object new GregorianCalendar[10] instanceof Calendar[]; new Calendar[10] instanceof Object[] new Calendar[10] instanceof O...
56 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 1130 | Lượt tải: 0
Hiding Fields and Static Methods, cont. When invoking an instance method from a reference variable, the actual class of the object referenced by the variable decides which implementation of the method is used at runtime. When accessing a field or a static method, the declared type of the reference variable decides which method is used at comp...
56 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 992 | Lượt tải: 0
Write a class named ReplaceText that replaces a string in a text file with a new string. The filename and strings are passed as command-line arguments as follows: java ReplaceText sourceFile targetFile oldString newString For example, invoking java ReplaceText FormatString.java t.txt StringBuilder StringBuffer replaces all the occurrences of ...
52 trang | Chia sẻ: dntpro1256 | Ngày: 23/11/2020 | Lượt xem: 1071 | Lượt tải: 0