gui basics: introduction. creating gui objects // create a button with text ok jbutton jbtok = new...
TRANSCRIPT
GUI Basics: Introduction
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 boldJCheckBox jchkBold = new JCheckBox("Bold"); // Create a radio button with text redJRadioButton jrbRed = new JRadioButton("Red"); // Create a combo box with choices red, green, and blueJComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"});
Button
Label Text field
Check Box
Radio Button
Combo Box
Swing vs. AWTThe biggest difference between the AWT components and Swing components are:
Swing components are implemented with absolutely no native code. Native code is methods implemented in another programming language such as C .
Swing features are present on every platform. Swing can have more functionality than AWT
components. Swing can be shipped as an add-on to JDK 1.1, in addition
to being part of the Java 2 Platform.
Swing vs. AWTSwing components have capabilities far beyond what the AWT components offer:
Swing buttons and labels can display images instead of, or in addition to, text.
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.
You can easily change the behavior or appearance of a Swing component by either invoking methods on it or creating a subclass of it.
Swing components don't have to be rectangular. Buttons, for example, can be round.
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
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.
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
Swing GUI Components
JMenuItem
JCheckBoxMenuItem
AbstractButton
JComponent
JMenu
JRadioButtonMenuItem
JToggleButton JCheckBox
JRadioButton
JComboBox
JInternalFrame
JLayeredPane
JList
JMenuBar
JOptionPane
JPopupMenu
JProgressBar
JFileChooser
JScrollBar
JScrollPane JSeparator JSplitPane
JSlider
JTabbedPane
JTable JTableHeader
JTextField JTextComponent
JTextArea
JToolBar JToolTip
JTree
JRootPane
JPanel
JPasswordField
JColorChooser
JLabel
JEditorPane
JSpinner
JButton
Components Covered in the Core Version
JMenuItem
JCheckBoxMenuItem
AbstractButton
JComponent
JMenu
JRadioButtonMenuItem
JToggleButton JCheckBox
JRadioButton
JComboBox
JInternalFrame
JLayeredPane
JList
JMenuBar
JOptionPane
JPopupMenu
JProgressBar
JFileChooser
JScrollBar
JScrollPane JSeparator JSplitPane
JSlider
JTabbedPane
JTable JTableHeader
JTextField JTextComponent
JTextArea
JToolBar JToolTip
JTree
JRootPane
JPanel
JPasswordField
JColorChooser
JLabel
JEditorPane
JSpinner
JButton
Components Covered in the Comprehensive Version
JMenuItem
JCheckBoxMenuItem
AbstractButton
JComponent
JMenu
JRadioButtonMenuItem
JToggleButton JCheckBox
JRadioButton
JComboBox
JInternalFrame
JLayeredPane
JList
JMenuBar
JOptionPane
JPopupMenu
JProgressBar
JFileChooser
JScrollBar
JScrollPane JSeparator JSplitPane
JSlider
JTabbedPane
JTable JTableHeader
JTextField JTextComponent
JTextArea
JToolBar JToolTip
JTree
JRootPane
JPanel
JPasswordField
JColorChooser
JLabel
JEditorPane
JSpinner
JButton
AWT (Optional)AWTEvent
Font
FontMetrics
Component
Graphics
Object Color
Canvas
Button
TextComponent
Label
List
CheckBoxGroup
CheckBox
Choice
Container Panel Applet
Frame
Dialog FileDialog
Window
TextField
TextArea
MenuComponent MenuItem
MenuBar
Menu
Scrollbar
LayoutManager
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 JFrame class can be used to create windows.
For Swing GUI programs, use JFrame class to create widows.
Creating Framesimport 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); }
}
Adding Components into a Frame
// Add a button into the frameframe.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 }
}
Content Pane Delegation in JDK 1.5
// Add a button into the frameframe.getContentPane().add( new JButton("OK"));
Title bar
Content pane// Add a button into the frameframe.add( new JButton("OK"));
JFrame Class
javax.swing.JFrame
+JFrame()
+JFrame(title: String)
+getSize(width: int, height: int): void
+setLocation(x: int, y: int): void
+setVisible(visible: boolean): void
+setDefaultCloseOperation(mode: int): void
+setLocationRelativeTo (c: Component): void
Creates a default frame with no title.
Creates a frame with the specified title.
Specifies the size of the frame.
Specifies the upper-left corner location of the frame.
Sets true to display the frame.
Specifies the operation when the frame is closed.
Sets the location of the frame relative to the specified component. If the component is null, the frame is centered on the screen.
Layout Managers Java’s layout managers provide a level of abstraction to
automatically map your user interface on all window systems.
The UI components are placed in containers. Each container has a layout manager to arrange the UI components within the container.
Layout managers are set in containers using the setLayout(LayoutManager) method in a container.
Kinds of Layout Managers
FlowLayout
GridLayout
BorderLayout