Frames o frame is a window that is not contained inside another window. frame is the basis to contain other user interface components in Java GUI applications o The frame class can be used to create windows o For Swing gui programs, use JFrame class to create widows Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 16 Frames ⚫Frame is a window that is not contained inside another window. Frame is the basis to contain other user interface components in Java GUI applications. ⚫The Frame class can be used to create windows. ⚫For Swing GUI programs, use JFrame class to create widows
Creating Frames import Javax. swIng.大 public class my frame t public static void main(string[] args)t JFrame frame new fRame(Test Frame )i frame. setsize(400, 300) frame. setvisible(true)i frame setDefaultCloseoperation JFrame.EX工 T ON CLOSE); NOTE: You must have JDK 1.3 or higher to run Run he slides Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 17 Creating Frames Run import javax.swing.*; public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } } NOTE: You must have JDK 1.3 or higher to run the slides
Adding components into a frame / Add a button into the frame Title bar frame. getContentPane().add( new JButton(ok"))i Content pane MyFrameWithComponentsRun Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 18 Adding Components into a Frame // Add a button into the frame frame.getContentPane().add( new JButton("OK")); MyFrameWithComponents Run Title bar Content pane
NOTE The content pane is a subclass of Container. The statement in the preceding slide can be replaced by the following two lines Container container= frame. get ContentPaneo container. add (new JButton(OK ) You may wonder how a Container object is created. It is created when a J Frame object is created. A JFrame object uses the content pane to hold components in the frame Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 19 NOTE The content pane is a subclass of Container. The statement in the preceding slide can be replaced by the following two lines: Container container = frame.getContentPane(); container.add(new JButton("OK")); You may wonder how a Container object is created. It is created when a JFrame object is created. A JFrame object uses the content pane to hold components in the frame
Centering frames OBy default a frame is displayed in the upper left corner of the screen OTo display a frame at a specified location, you can use the setlocation(x, y) method in the JFrame class. This method places the upper-left corner of a frame at location(x, y) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 20 Centering Frames ⚫By default, a frame is displayed in the upperleft corner of the screen. ⚫To display a frame at a specified location, you can use the setLocation(x, y) method in the JFrame class. This method places the upper-left corner of a frame at location (x, y)