TAILIEUCHUNG - Java Programming for absolute beginner- P15
Java Programming for absolute beginner- P15:Hello and welcome to Java Programming for the Absolute Beginner. You probably already have a good understanding of how to use your computer. These days it’s hard to find someone who doesn’t, given the importance of computers in today’s world. Learning to control your computer intimately is what will separate you from the pack! By reading this book, you learn how to accomplish just that through the magic of programming. | 2 25 03 8 53 AM Page 238 k 0 c c 0 2 V 0 a c E E s k a o k a s s First off it extends Frame so it is a Frame. It sets its background color to which as you might remember from the previous chapter is the color that your operating system uses for painting windows. Then it adds a WindowAdapter object as its WindowListener. Pay close attention to the syntax here. This is actually an inner-class declaration which you ll learn about later on in this chapter. Another cool thing it does is center itself no matter what size it is when it becomes visible. I did this by overriding the setVisible boolean method. If the argument passed in is true I get the screen size by calling .getScreenSize which returns a Dimension object representing the resolution of the computer screen whether it is 640 by 480 800 by 600 or whatever. The Toolkit class is the abstract subclass of all implementations of the AWT the default of which is different depending on what operating system you are running. To center the GUIFrame on screen I needed to know the screen size and the size of the GUIFrame. The position of the GUIFrame is set with the setLoca-tion int int method where the first int is the x location and the second int is the y location. The center location is half the difference of the screen width minus the GUIFrame width as the x position and half the difference of the screen height minus the GUIFrame height as the y position. visible is called so that the corresponding method in the Frame class can take care of actually making the GUIFrame visible. Take a look at Figure to see what the GUIFrame looks like. Here is a test of the GUIFrame class GUIFrameTest GUIFrameTest Demonstrates the GUIFrame Class public class GUIFrameTest public static void main String args GUIFrame frame new GUIFrame GUIFrame Test 400 300 true Using CardLayout The CardLayout layout manager lays .
đang nạp các trang xem trước