350 likes | 542 Views
Week 12. Graphical User Interfaces Chapter 11. Designing Program with Graphical User Interfaces. Programs have two parts/classes: the User Interface the engine In Connect4 User interface is board and drawing the board The engine is what does the logic: calculates win/lose etc.
E N D
Week 12 Graphical User Interfaces Chapter 11
Designing Program with Graphical User Interfaces • Programs have two parts/classes: • the User Interface • the engine • In Connect4 • User interface is board and drawing the board • The engine is what does the logic: calculates win/lose etc. • GUI is User Interface: how program is shown to user
Implementating GUIs:GUI parts • Components • buttons, menus, textfields, etc. • Event Handling • instead of executing methods when called (by BlueJ or in code), execute methods when an event occurs (e.g., mouse click on a button) • Layout • Where on the screen to put the components. use LayoutManager
GUIs • Components • JFrames and Windows • creating a Window • Menus • adding a Window to the menu • Event Handling • handling events that happen in the Window • Layouts • arranging components in the Window
Swing • Java has AWT (old, Abstract Window Toolkit) and Swing (based on AWT) • Swing prefixes components with J • e.g., Button is from AWT, JButton is Swing • We’ll use Swing • To use Swing: import javax.swing.*; // note ‘x’
Java GUI • The Java GUI packages • GUI classes are found in two packages: • AWT (Abstract Windows Toolkit) • Swing • Swing components are based on AWT classes • Java 2 released Swing as an alternative to AWT • AWT uses platform-specific GUI components • Swing is less reliant on platform-specific components AWT Swing more less reliance on platform-specific GUI components heavyweightcomponents lightweightcomponents
Java GUI The Java GUI inheritance hierarchy AWT LayoutManager Panel Applet JApplet Object Component Container Window Frame JFrame Color Dialog JDialog JComponent Swing Components we will use
Java GUI The Java GUI inheritance hierarchy JMenuBar JMenuItem AbstractButton JButton JComboBox JScrollPane JComponent … JLabel JTextArea JTextComponent JTextField JPasswordField JPanel … etc. Swing components we will use
Building a Window Frame (whole window) • Everything is in a top level container (JFrame) • JFrame has • Title bar • Menu bar (optional) • Content Pane title bar menu bar content pane
Code for a JFrame 3. pack frame. arrange components windowFrame.pack( ) 4. make the frame visible (can also make it invisible, as in quitWord) windowFrame.setVisible(true);
Code for a Frame To build a JFrame, do 4 things (From 150 lab3 GuiInterface.java) 1. Create new JFrame and put a title in private JFrame windowFrame = new JFrame(“Word Jumble 1.0”); 2. Tie parts to JFrame (component, menubar, etc) // if gameWordPanel already exists – type JPanel Container cp = WindowFrame.getContentPane( ); cp.add(gameWordPanel); OR windowFrame.getContentPane().add(gameWordPanel); OR (in Java5): windowFrame.add(gameWordPanel);
JFrame • Lots of parts to a JFrame, mostly ContentPane • content panes can hold any number of components • (4 components in windowFrame in lab3) • contentPane (only one) vs JPanel (can have lots) • Pre-Java5 • add content (components) to the contentPane • Now, add to the JFrame. adds to contentPane
Some Components • JButton • JPanel • JLabel • Lots, look at: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/package-summary.html
minimize maximize close come for free title bar { component (JPanel) Also in lab3, what to do on close (click on red X): windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUIs • JFrames and Windows • creating a Window • Menus • adding a Window to the menu • Event Handling • handling events that happen in the Window • Layouts • arranging components in the Window
Building a Window Frame (whole window) • Everything is in a JFrame • JFrame has • Title bar • Menu bar (optional) • Content Pane title bar menu bar content pane
Adding menus: 3 parts • JMenuBar • Displayed below the title. • Contains the menus. • JMenu • e.g. File. Contains the menu items. • JMenuItem • e.g. Open. Individual items.
private void makeMenuBar(JFrame frame) { JMenuBar menubar = new JMenuBar(); frame.setJMenuBar(menubar); // create the File menu JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); JMenuItem openItem = new JMenuItem("Open"); fileMenu.add(openItem); JMenuItem quitItem = new JMenuItem("Quit"); fileMenu.add(quitItem); } } To add JMenuBar to JFrame { To add JMenu to JMenuBar { To add JMenuItem to JMenu
Struts and Glue • Invisible components used as spacing. • Available from the Box class. • Strut: fixed size. • Component createHorizontalStrut(int width) • Component createVerticalStrut(int height) • Glue: fills available space. • Component createHorizontalGlue() • Component createVerticalGlue()
Pushing a menu to the right menu = new JMenu("File"); menubar.add(menu); menu = new JMenu("Filter"); menubar.add(menu); menubar.add(Box.createHorizontalGlue()); menu = new JMenu("Help"); menubar.add(menu); Glue (invisible)
GUIs • Components • JFrames and Windows • creating a Window • Menus • adding a Window to the menu • Event Handling • handling events that happen in the Window • Layouts • arranging components in the Window
Events • We can now put items on a JFrame and arrange them. We need to be able to do something with them. • Action Events • mouse click, mouse over, window action, menu choice, etc. • General, ActionListener • Something happened • in java.awt.event • (http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/event/package-summary.html)
“Check” button in Lab 3:add ActionListener event actionPerformed in the outer class class someclass { // inside some method: b = new JButton("Check"); b.addActionListener(this); // "this" is the current class public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } }
Action Listeners: Inner Classes class someclass { // inside some method: b = new JButton("Check"); b.addActionListener(new checkAction( )); private class checkAction implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } } // end of checkAction } // end of someclass
A third way:anonymous inner classes //Associate actionlistener with button and write actionPerformed b = new JButton("Check"); b.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); checkWord(); } } );
Anonymous class elements Anonymous object creation • b.addActionListener( • new ActionListener() • { • public void actionPerformed(ActionEvent e) • { • System.out.println("Check is " + • e.getActionCommand()); • checkWord(); • } • } • ); Class definition Actual parameter
GUIs • JFrames and Windows • creating a Window • Menus • adding a Window to the menu • Event Handling • handling events that happen in the Window • Layouts • arranging components in the Window
Layout managers • Manage limited space for components. • FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout (most flexible, hardest) • Manage Container objects, e.g. a content pane. • Each imposes its own style.
BoxLayout Note: no component resizing.
Layout Manager Lab 3 • 4 JPanels gameWordPanel, gameGuessPanel, gameButtonPanel, gameStatusPanel
Layout Managers – Lab 3 Container contentPane = windowFrame.getContentPane ( ); contentPane.setLayout(new GridLayout(4, 1)); OR windowFrame.getContentPane().setLayout(new GridLayout(4,1)); OR windowFrame.setLayout(new GridLayout(4, 1)); // add panels in order windowFrame.getContentPane().add(gameWordPanel); windowFrame.getContentPane().add(gameGuessPanel); windowFrame.getContentPane().add(gameButtonPanel); windowFrame.getContentPane().add(gameStatusPanel);
Buttons and nested layoutsUse JPanels and embedded layouts A GridLayout inside a FlowLayout inside a BorderLayout.