Creating a GUI with JFC/Swing

What are the JFC and Swing? What are the JFC and Swing? • JFC C – Java Foundation Classes – a group of features to help people build graphical user  interfaces (GUIs)  – APIs API • AWT, Swing, Java2D, Accessibility, Drag and Drop

• Swing S i – Components for GUIs – to use Swing, you have to import javax.swing package. 2110210 Programming Methodology

2

Component

An abstract object with  graphics displayed and  interacted with the user

Container

An abstract component that  can contain other  components t

Panel/ JPanel

FFrame or window without  i d ith t the titlebar, menu, nor  border

Window

Frame/ JFrame

Top‐level container without  menu, nor border A window with the  menubar, border, and  , , resizing corner 2110210 Programming Methodology

3

Top Level Containers Top‐Level Containers • JFrame • JDialog • JApplet

• every GUI components  must be a part of  container • contained only once

2110210 Programming Methodology

4

JFrame

2110210 Programming Methodology

5

// TopLevelDemo.java import java.awt.*; i import j java.awt.event.*; import javax.swing.*; public class TopLevelDemo { private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("TopLevelDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create the menu bar. Make it have a green background. JMenuBar greenMenuBar = new JMenuBar(); () greenMenuBar.setOpaque(true); greenMenuBar.setBackground(new Color(154, 165, 127)); greenMenuBar setPreferredSize(new Dimension(200, greenMenuBar.setPreferredSize(new Dimension(200 20)); //Create a yellow label to put in the content pane. JLabel yellowLabel = new JLabel(); yellowLabel.setOpaque(true); yellowLabel.setBackground(new Color(248, 213, 131)); yellowLabel.setPreferredSize(new Dimension(200, 180));

2110210 Programming Methodology

6

//Set the menu bar and add the label to the content pane. f frame.setJMenuBar(greenMenuBar); ( ) frame.getContentPane().add(yellowLabel, BorderLayout.CENTER); //Display the window. window frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: // //creating i and d showing h i this hi application's li i ' GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }

2110210 Programming Methodology

7

Menu

2110210 Programming Methodology

8

Adding Menu (1) Adding Menu (1) //Create the menu bar. menuBar = new JMenuBar(); //Build the first menu. menu = new Jmenu(“A Menu”); menu.setMnemonic(KeyEvent.VK_A); menu getAccessibleContext() setAccessibleDescription( menu.getAccessibleContext().setAccessibleDescription( "The only menu in this program that has menu items"); menuBar.add(menu); //a group of JMenuItems menuItem = new JMenuItem(“A text-only menu item", KeyEvent.VK_T); menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem getAccessibleContext() setAccessibleDescription( menuItem.getAccessibleContext().setAccessibleDescription( "This doesn't really do anything"); menu.add(menuItem);

2110210 Programming Methodology

9

Adding Menu (2) Adding Menu (2) menuItem = new JMenuItem("Both text and icon", new ImageIcon("images/middle.gif")); menuItem setMnemonic(KeyEvent VK B); menuItem.setMnemonic(KeyEvent.VK_B); menu.add(menuItem); menuItem = new JMenuItem(new ImageIcon("images/middle.gif")); menuItem setMnemonic(KeyEvent VK D); menuItem.setMnemonic(KeyEvent.VK_D); menu.add(menuItem); //a group of radio button menu items menu addSeparator(); menu.addSeparator(); ButtonGroup group = new ButtonGroup(); rbMenuItem = new JRadioButtonMenuItem("A radio button menu item"); rbMenuItem.setSelected(true); rbMenuItem setMnemonic(KeyEvent VK R); rbMenuItem.setMnemonic(KeyEvent.VK_R); group.add(rbMenuItem); menu.add(rbMenuItem);

2110210 Programming Methodology

10

Enable Keyboard Operation Enable Keyboard Operation //Setting //S tti the th mnemonic i when h constructing t ti a menu item: it menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T); //Setting the mnemonic after creation time: menuItem.setMnemonic(KeyEvent.VK_T); //Setting the accelerator: menuItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent VK T ActionEvent.ALT_MASK)); KeyEvent.VK_T, ActionEvent ALT MASK));

2110210 Programming Methodology

11

JFrame (top‐level containers) (top level containers)

2110210 Programming Methodology

12

JFrame (top‐level containers) (top level containers) • Glass Pane • Content Pane – Menu Bar

• Layered Pane Layered Pane • Root Pane

2110210 Programming Methodology

13

Glass Pane Glass Pane myGlassPane Gl P = new MyGlassPane(...); M Gl P ( ) changeButton.addItemListener(myGlassPane); frame.setGlassPane(myGlassPane); ... class MyGlassPane extends JComponent implements ItemListener { ... //React to change button clicks. public void itemStateChanged(ItemEvent e) { setVisible(e.getStateChange() == ItemEvent.SELECTED); } ... }

2110210 Programming Methodology

14

Layer Pane Layer Pane

2110210 Programming Methodology

15

What are the objects in an application? What are the objects in an application? Menu Bar -- optional

JFrame – the toplevel container class

Content Pane – contains the visible components in the top-level container’s GUI

2110210 Programming Methodology

16

Look and Feel - without JFrame setDefaultLookAndFeelDecorated(true); JFrame.setDefaultLookAndFeelDecorated(true);

- with JFrame.setDefaultLookAndFeelDecorated(true);

2110210 Programming Methodology

17

frame.setSize(450, 200);

frame.setSize(250, 200);

2110210 Programming Methodology

18

Exercise 1. Create a calculator application that has  buttons for 0 – 9, +, −, and = signs.   It should  , , , g have a display are that shows the result. 2 Modify the program from (1) to have more  2. Modify the program from (1) to have more functions such as, *, /, %, or handle real  number. b

2110210 Programming Methodology

19

Reference java.sun.com/docs/book/tutorial/uiswing www.cp.eng.chula.ac.th/~chate/2110210 www cp eng chula ac th/~vishnu/progmeth www.cp.eng.chula.ac.th/ vishnu/progmeth

2110210 Programming Methodology

20