Download presentation
Presentation is loading. Please wait.
Published byNorma Ferguson Modified over 9 years ago
1
GUI Basics: Introduction
2
Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblName = new JLabel("Enter your name: "); // Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here"); // Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold"); // Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red"); // Create a combo box with choices red, green, and blue JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"}); Button LabelText field Check Box Radio Button Combo Box
3
Swing vs. AWT The biggest difference between the AWT components and Swing components are: F Swing components are implemented with absolutely no native code. Native code is methods implemented in another programming language such as C. F Swing features are present on every platform. F Swing can have more functionality than AWT components. F Swing can be shipped as an add-on to JDK 1.1, in addition to being part of the Java 2 Platform.
4
Swing vs. AWT Swing components have capabilities far beyond what the AWT components offer: F Swing buttons and labels can display images instead of, or in addition to, text. F You can easily add or change the borders drawn around most Swing components. For example, it's easy to put a box around the outside of a container or label. F You can easily change the behavior or appearance of a Swing component by either invoking methods on it or creating a subclass of it. F Swing components don't have to be rectangular. Buttons, for example, can be round.
5
GUI Class Hierarchy (Swing)
6
Container Classes Container classes can contain other GUI components.
7
The helper classes are not subclasses of Component. They are used to describe the properties of GUI components such as graphics context, colors, fonts, and dimension. GUI Helper Classes
8
Swing GUI Components
9
Components Covered in the Core Version
10
Components Covered in the Comprehensive Version
11
AWT (Optional)
12
Frames F 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. F The JFrame class can be used to create windows. F For Swing GUI programs, use JFrame class to create widows.
13
Creating Frames 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); }
14
Adding Components into a Frame // Add a button into the frame frame.getContentPane().add( new JButton("OK")); Title bar Content pane import javax.swing.*; public class MyFrameWithComponents { public static void main(String[] args) { JFrame frame = new JFrame("MyFrameWithComponents"); // Add a button into the frame JButton jbtOK = new JButton("OK"); frame.add(jbtOK); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); // New since JDK 1.4 }
15
Content Pane Delegation in JDK 1.5 // Add a button into the frame frame.getContentPane().add( new JButton("OK")); Title bar Content pane // Add a button into the frame frame.add( new JButton("OK"));
16
JFrame Class
17
Layout Managers F Java’s layout managers provide a level of abstraction to automatically map your user interface on all window systems. F The UI components are placed in containers. Each container has a layout manager to arrange the UI components within the container. F Layout managers are set in containers using the setLayout(LayoutManager) method in a container.
18
Kinds of Layout Managers F FlowLayout F GridLayout F BorderLayout
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.