Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:

Similar presentations


Presentation on theme: "Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:"— Presentation transcript:

1 Java Swing

2 Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events: A user or programmatic action – Listeners: A method that responds to an event – Components: A GUI object – Containers: A collection of components – listener interface: An interface that contains listeners – Adapter class: A class that implements a listener interface with default methods – layout managers: An object that defines how components in a container present to the user – special features: Methods that customize a GUI's look and feel

3 Java GUI Facilities AWT (Abstract windowing toolkit) – Designed for creating applets – Not powerful enough for application programs – Simple and easy to use, and supported by almost all browsers – Peer model relies on platform-dependent native code (heavyweight) Swing (The creator was a "swing" dancer) – Newer and more sophisticated GUI facility – Swing class names start with the letter 'J'. – Consistent look and feel across operating systems – Does not depend on operating system facilities (lightweight) – Many swing classes extend their AWT counterparts – Most application developers now use Swing, not AWT – Supports a pluggable look and feel (UIManager.setLookAndFeel("javax.swing.plaf.windowsLookAndFeel"); AWT and Swing are each a collection of Java classes for GUI development Java GUI facilities are large and complex. We focus only on a small subset

4 Containers and Components Container – A Java class instantiated to hold groups of components – Examples: JApplet, JFrame, JPanel, JTabbedPanel, JScrollPane Component – A Java class instantiated to create a type of GUI object – Examples: JButton, JCheckBox, JComboBox, JColorChooser, JFileChooser, JLabel, JList, JMenu, JOptionPane, JPasswordField, JRadioButton, JSlider, JTextArea, JTextField, JToggleButton, JTree Java GUI applications principally consist of containers and components

5 5 Swing Components and containers: – superclasses and interfaces – extends and implements

6 Layout Manager Summary AWT managers – Flow: left-to-right, top-to-bottom in order (The default) – Border: Sections for North, South, Center, East, West – Card: Tab like capability, display one card at a time – Grid: two dimensional array of components – GridBag: two dimensional array of components where components can span rows and columns Swing managers – Box: vertical or horizontal list of components – Overlay: components that can overlap each other.

7 General Comments It is not hard to create a GUI application, but it can be tedious There is no drag and drop capability to create GUI components like we have in Visual Basic GUIs in java have many single line Statements – To create GUI components – To set GUI properties – To call special methods Good design – Break up GUI applications to a set of panel or component classes. – This makes the code easier to maintain

8 Steps to create a GUI Application 1.Create a class hierarchy diagram for the application 2.Determine components should be in their own Java class 3.Instantiate the application's JFrame with its title 4.Configure the window close procedure 5.Instantiate the components and set their properties 6.Call the JFrame getContentPane() method to get the default application container. 7.Add the components to the JFrame application container 8.Set the size of the frame 9.Make the frame visible Note: It is possible for a JFrame to have multiple containers (layers)

9 Steps to Create a Component or Container Class 1.GUI component class signature line – Extend the appropriate component class (extends) – Implement the appropriate listener (implements) 2.Create references to the needed sub- components 3.Instantiate the components in the constructor class or in the applet init() method 4.Define the layout for containers 5.Call methods for setting custom properties 6.Add components to containers 7.Add listeners

10 Components JButton JButton button = new Jbutton(“Add”); button.setMnemonic(‘A’); button.setToolTipText(“Add a record”); JFrame JFrame frame = new JFrame("A Title"); JTextField JTextField data = new JTextField(“”) String text = data.getText(); JLabel JLabel label = new JLabel(“label to display”); label.setText(“new Text”); JComboBox JComboBox box = new JComboBox(array or object); JRadioButton JRadioButton button= new JRadioButton(“end”, true); JCheckBox JCheckBox box = new JCheckBox((“Bold”, true); Jlist Jlist list = new Jlist(array or object); JScrollPane JScrollPane scroll = new JScrollPane(array or object); JtabbedPane JTabbedPane pane = new JTabbedPane(); pane.addTab(“label”, container);

11 11 JFrame example 1 A simple program that creates and shows a JFrame : import javax.swing.*; public class SimpleFrame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setVisible(true); } Graphical output:

12 12 JFrame example 2 A program that sets several properties of the JFrame : import java.awt.*; import javax.swing.*; public class SimpleFrame2 { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setForeground(Color.WHITE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(new Point(10, 50)); frame.setSize(new Dimension(300, 120)); frame.setTitle("A frame"); frame.setVisible(true); } Graphical output:

13 Examples import java.awt.*; import javax.swing.*; public class JFrameExample { public static void main(String[] args) { JFrame f = new JFrame("This is a test"); f.setSize(400, 150); Container content = f.getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); content.add(new JButton("Button 1")); content.add(new JButton("Button 2")); content.add(new JButton("Button 3")); f.setVisible(true); }

14 14 A frame is a graphical window that can be used to hold other components public JFrame() public JFrame(String title) Creates a frame with an optional title. public void setTitle(String text) Puts the given text in the frame’s title bar. public void setDefaultCloseOperation(int op) Makes the frame perform the given action when it closes. Common value: JFrame.EXIT_ON_CLOSE public void add(Component comp) Places the given component or container inside the frame. – How would we add more than one component to the frame? NOTE: Call setVisible(true) to make a frame appear on screen after creating it. JFrame

15 15 JFrame properties JFrames have the following unique properties that you can get or set in your graphical programs: nametypedescriptionmethods default close operation int what should happen when frame is closed getDefaultCloseOperation, setDefaultCloseOperation icon image Image icon in the window's title bar getIconImage, setIconImage layout LayoutManager how the frame should position its components getLayout, setLayout resizable boolean whether the window can be resized isResizable, setResizable title String window's title bar text getTitle, setTitle

16 16 Component properties All components also have the following properties: nametypedescriptionmethods background Color background color getBackground, setBackground enabled boolean whether the component can be interacted with isEnabled, setEnabled font Font font used to display any text on the component getFont, setFont foreground Color foreground color getForeground, setForeground location Point (x, y) position of component on screen getLocation, setLocation size Dimension width, height of component getSize, setSize preferred size Dimension width, height that the component wants to be getPreferredSize, setPreferredSize visible boolean whether the component can be seen on screen isVisible, setVisible

17 17 Swing component hierarchy Graphical components in Java form an inheritance hierarchy: java.lang.Object +--java.awt.Component +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.JButton | +--javax.swing.JLabel | +--javax.swing.JMenuBar | +--javax.swing.JOptionPane | +--javax.swing.JPanel | +--javax.swing.JTextArea | +--javax.swing.JTextField | +--java.awt.Window +--java.awt.Frame +--javax.swing.JFrame When doing GUI programming, always import these packages: import java.awt.*; import javax.swing.*;

18 Listeners: Methods responding to Events Examples MouseListener – respond to user mouse events – Add "implements MouseListener" to the GUI class – Code listener methods (e.g. mouseClicked()) and attach to the GUI object MouseMotionListener – respond to mouse movements – Add "implements MouseMotionListener" to the GUI class – Code listener methods (e.g. mouseMoved()) and attach to the GUI object ActionListener – Recponds once to button selections – Add "implements ActionListener" to the GUI class – Code the "actionPerformed" method and attach to the GUI object ItemListener – Responds multiple times to changes to a component – Add "implements ItemListener" to the GUI class – Code the "itemStateChanged" method – Attach the ItemListener to the GUI object Window Listener – respond to clicks of a frame's X button – Create a class that extends WindowAdapter – Code the WindowListener methods and attach to the frame


Download ppt "Java Swing. Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: – Events:"

Similar presentations


Ads by Google