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:") Display GUI Components ▣x OK Enter your name:Type Name HereBold Red Red Red Green Blue /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"));
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 Label Text field Check Box Radio Button Combo Box
Swing vs.AWT So why do the GUI component classes have a prefix J?Instead of JButton,why not name it simply Button?In fact,there is a class already named Button in the java awt package. When Java was introduced,the GUI classes were bundled in a library known as the Abstract Windows Toolkit(AWT).For every platform on which Java runs,the AWT components are automatically mapped to the platform-specific components through their respective agents,known as peers.AWT is fine for developing simple graphical user interfaces,but not for developing comprehensive GUI projects.Besides,AWT is prone to platform-specific bugs because its peer-based approach relies heavily on the underlying platform.With the release of Java 2,the AWT user-interface components were replaced by a more robust,versatile,and flexible library known as Swing components.Swing components are painted directly on canvases using Java code,except for components that are subclasses of java awt Window or java awt Panel,which must be drawn using native GUI on a specific platform.Swing componentsare less dependent on the target platform and use less of the native GUI resource.For this reason,Swing components that don't rely on native GUI are referred to as lightweight components,and AWT components are referred to as heavyweight components
Swing vs. AWT So why do the GUI component classes have a prefix J? Instead of JButton, why not name it simply Button? In fact, there is a class already named Button in the java.awt package. When Java was introduced, the GUI classes were bundled in a library known as the Abstract Windows Toolkit (AWT). For every platform on which Java runs, the AWT components are automatically mapped to the platform-specific components through their respective agents, known as peers. AWT is fine for developing simple graphical user interfaces, but not for developing comprehensive GUI projects. Besides, AWT is prone to platform-specific bugs because its peer-based approach relies heavily on the underlying platform. With the release of Java 2, the AWT user-interface components were replaced by a more robust, versatile, and flexible library known as Swing components. Swing components are painted directly on canvases using Java code, except for components that are subclasses of java.awt.Window or java.awt.Panel, which must be drawn using native GUI on a specific platform. Swing components are less dependent on the target platform and use less of the native GUI resource. For this reason, Swing components that don’t rely on native GUI are referred to as lightweight components, and AWT components are referred to as heavyweight components
GUI Class Hierarchy (Swing) Dimension Classes in the java awt LayoutManager package Heavyweight Font FontMetrics Object Color Panel Applet JApplet Graphics Component Container Window Frame JFrame Dialog JDialog JComponent Swing Components in the javax.swing package 444444444 444444444444444444 Lightweight
GUI Class Hierarchy (Swing) Dimension Font FontMetrics Component Graphics Object Color Container Panel Applet Frame Dialog Window JComponent JApplet JFrame JDialog Swing Components in the javax.swing package Lightweight Heavyweight Classes in the java.awt package 1 LayoutManager *
Container Classes Dimension Classes in the java awt LayoutManager package Heavyweight Font 1 FontMetrics Object Color Panel Applet JApplet Graphics Component Container Window Frame JFrame Dialog JDialog JComponent JPanel Swing Components Container classes can in the javax.swing package contain other GUI components Lightweight
Container Classes Dimension Font FontMetrics Component Graphics Object Color Container Panel Applet Frame Dialog Window JComponent JApplet JFrame JDialog Swing Components in the javax.swing package Lightweight Heavyweight Classes in the java.awt package 1 LayoutManager * JPanel Container classes can contain other GUI components
GUI Helper Classes Dimension Classes in the java.awt LayoutManager package Heavyweight Font FontMetrics Object Color Panel Applet JApplet Graphics Component Container Window Frame JFrame Dialog JDialog The helper classes are not subclasses JComponent JPanel Swing Components in the javax.swing package of Component.They are used to describe the properties of GUI components such as graphics context, Lightweight colors,fonts,and dimension
Dimension Font FontMetrics Component Graphics Object Color Container Panel Applet Frame Dialog Window JComponent JApplet JFrame JDialog Swing Components in the javax.swing package Lightweight Heavyweight Classes in the java.awt package 1 LayoutManager * JPanel 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