60 likes | 74 Views
Learn about JFrame class functionalities and different ways to place GUI components using absolute positioning or layout managers.
E N D
Creating Graphical User Interfaces SE-1020 Dr. Mark L. Hornick
The JFrame class is the fundamental “window” class • It contains rudimentary functionalities to support features found in any frame window. • A JFrame object serves as the “host” for containing other components, JTextField JLabel JFrame JButton SE-1020 Dr. Mark L. Hornick
Placing GUI components on a JFrame There are two ways to put GUI components on the content pane of a JFrame: • Absolute positioning • You explicitly specify the position and size of GUI objects within the content pane • Use a Layout manager • A class that controls automatic placement of components • There are many different Layout manager classes available SE-1020 Dr. Mark L. Hornick
To place components within a JFrame window… • First, access the JFrame’s Container object (called the content pane) • the area of the frame excluding the title and menu bars and the border • this is the drawable area you can modify • The Container is accessed via the JFrame’s getContentPane() method: Container contentPane = jf.getContentPane(); • The Container has various properties (attributes) • background color can be changed by calling the Container’s setBackground() method. SE-1020 Dr. Mark L. Hornick
Using Absolute Positioning • Set the layout manager of a JFrame’s Container to “none” by passing null to the setLayout() method: contentPane.setLayout(null); • Create a button (or other GUI component) and place it at the desired position and size by calling the button’s setBounds() method:JButton button = new JButton(); button.setBounds( 75, 125, 80, 30); // window coords • The first two arguments specify the button’s position in the JFrame • The last two arguments specify the width and height of the button. • To make a button appear on the JFrame, add it to the Container by calling the add() method.contentPane.add(okButton); SE-1020 Dr. Mark L. Hornick
Exercise • Replicate the following UI using absolute positioning. JTextField JLabel JFrame JButton SE-1020 Dr. Mark L. Hornick