Presentation is loading. Please wait.

Presentation is loading. Please wait.

Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.

Similar presentations


Presentation on theme: "Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages."— Presentation transcript:

1 Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages A professional standard extension of AWT Uses a containment hierarchy to build a GUI

2 Differences from and Advantages Over AWT Components start with the letter J –JFrame, JPanel, JButton etc. Buttons and labels can have images. Easy to apply borders to any component. Easy to change behaviour and appearance. Components don’t have to be rectangular. Look and feel not limited to platform. Deals with accessibility issues

3 General The Swing API includes: –Containers –JComponents Inherits from Container and Component –Layout managers To control the way that components are laid out –Events –At the root, you need a JFrame

4 A First Go At a GUI import javax.swing.*; // import java.awt.JFrame class ExampleFrame{ public static void main (String arg[]){ JFrame f = new JFrame(“Example”); f.setVisible(); }

5 Explanation the JFrame is initially invisible as it is empty, it is initially created with the title bar displayed but no viewable area the window may be maximised and minimised and closed generally, your programs will define new classes that inherit from the JFrame class but have new specific settings or behaviours. For example, your new class might open a frame of a specific size.

6 Containment Hierarchy JFrame is top level container. JPanel is intermediate container. JButton, JLabel are atomic components

7 Code for This Frame ……….. frame = new JFrame(...); button = new JButton(...); label = new JLabel(...); pane = new JPanel(); pane.add(button); pane.add(label); frame.getContentPane().add(pane, BorderLayout.CENTER); …………..

8 Code Pattern The technique is always the same: –Get a top level container (JFrame) –Add components to its Content Pane JFrame is therefore different from the old AWT Frame. Neither Frame nor JFrame close unless you add the code to do so. Both come with the familiar three top right hand corner buttons

9 Layout Managers GridLayout - All equal size in a grid GridBagLayout – Most flexible CardLayout – area contains different components at different times

10 More Layout Managers BoxLayout – single column or row BorderLayout - Default for content pane FlowLayout – default for JPanel

11 Code example: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingApplication { private static String labelPrefix = "Number of button clicks: "; private int numClicks = 0; public Component createComponents() { final JLabel label = new JLabel(labelPrefix + "0 "); JButton button = new JButton("I'm a Swing button!"); /* * An easy way to put space between a top-level container and * its contents is to put the contents in a Jpanel that has an * "empty" border. */ JPanel pane = new JPanel(); pane.setBorder(BorderFactory.createEmptyBorder ( 30, 30,10, 30)); pane.setLayout(new GridLayout(0, 1)); pane.add(button); pane.add(label); return pane; }

12 public static void main(String[] args) { //Create the top-level container and add contents to it. JFrame frame = new JFrame("SwingApplication"); SwingApplication app = new SwingApplication(); Component contents = app.createComponents(); frame.getContentPane().add(contents, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); }


Download ppt "Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages."

Similar presentations


Ads by Google