Download presentation
Presentation is loading. Please wait.
Published byRodney Shaw Modified over 8 years ago
1
SE-1020 Dr. Mark L. Hornick 1 Creating Graphical User Interfaces
2
SE-1020 Dr. Mark L. Hornick 2 JTextField JLabel JFrameJButton 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,
3
SE-1020 Dr. Mark L. Hornick 3 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
4
SE-1020 Dr. Mark L. Hornick 4 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.
5
SE-1020 Dr. Mark L. Hornick 5 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);
6
SE-1020 Dr. Mark L. Hornick 6 JTextField JLabel JFrameJButton Exercise Replicate the following UI using absolute positioning.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.