Você está na página 1de 24

Graphical User Interfaces

Kishan Wimalawarne

Introduction
Java provides a rich windowing and graphical classes. Java packages
AWT JFC Swing SWT

Some Containers

Some Swing Components

A simple Frame Example


import javax.swing.JFrame; public class myFrame extends JFrame { myFrame(){ setTitle("My Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); setSize(450, 300); } public static void main(String[] args) { myFrame f = new myFrame(); f.setVisible(true); } }

A simple Frame Example

More on Frame Example


Adding menus and sub menus

import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; public class myFrame extends JFrame { JMenuBar mbar; JMenu menu; myFrame(){ mbar = new JMenuBar(); menu = new JMenu(); menu.setText("Menu1"); mbar.add(menu); setJMenuBar(mbar); setTitle("My Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(450, 300); } public static void main(String[] args) { myFrame f = new myFrame(); f.setVisible(true); } }

Main window with a menu

More on Frame Example


Adding menu item public class myFrame extends JFrame { JMenuBar mbar; JMenu menu; JMenuItem menuItem1 , menuItem2 ; myFrame(){ mbar = new JMenuBar(); menu = new JMenu(); menu.setText("Menu1"); menuItem1 = new JMenuItem("Menu Item 1"); menuItem2 = new JMenuItem("Menu Item 2"); menu.add(menuItem1); menu.add(menuItem2); mbar.add(menu); setJMenuBar(mbar);

Main window with a subenu

More Menu features


setMnemonic
Provide Keyword alternatives
menu.setMnemonic(KeyEvent.VK_A); Alt + A to access the menu

setAccelerator
menu.getAccessibleContext().setAccessibleDescription(This is a Menu");

Event Listeners
Listeners are objects which listen for a particular event and then react to it. Ex :- If a user click on a button, what action to take. If a user select a menu option what action to take. When exit button of them ain window selected what to do Listeners comes as interfaces found in java.awt.event. Ex :- WindowListener ActionListener http://java.sun.com/docs/books/tutorial/uiswing/events/even tsandcomponents.html

WindowListener Interface
public class myFrame extends JFrame implements WindowListener{ public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } public public public public public public } void void void void void void windowOpened(WindowEvent e) {} windowActivated(WindowEvent e) {} windowIconified(WindowEvent e) {} windowDeiconified(WindowEvent e) {} windowDeactivated(WindowEvent e) {} windowClosed(WindowEvent e) {}

ActionListener
Action listeners are probably the easiest and most common event handlers to implement. You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field. The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component. public interface ActionListener{ void actionPerformed(ActionEvent e) { }

ActionListener
ActionEvent class
Methods
String getActionCommand() Returns the string associated with this action Object getSource() Returns the object that fired the event. int getModifiers() Returns an integer representing the modifier keys the user was pressing when the action event occurred.

public class myFrame extends JFrame implements WindowListener, ActionListener{ . myFrame(){ . menuItem1 = new JMenuItem("Menu Item 1"); menuItem2 = new JMenuItem("Menu Item 2"); menuItem1.addActionListener(this); menuItem2.addActionListener(this); e.getSource() object that fired the event } public void actionPerformed(ActionEvent e) { if( e.getSource().equals(menuItem1) ){ JOptionPane.showMessageDialog(this, "You selected Menu Item 1."); }else if(e.getSource().equals(menuItem2)){ JOptionPane.showMessageDialog(this, "You selected Menu Item 2."); } }

public class myFrame extends JFrame implements WindowListener, ActionListener{ . myFrame(){ . menuItem1 = new JMenuItem("Menu Item 1"); menuItem2 = new JMenuItem("Menu Item 2"); menuItem1.addActionListener(this); e.getActionCommand() returns string menuItem2.addActionListener(this); associated with this action } public void actionPerformed(ActionEvent e) { if( e.getActionCommand().equals("Menu Item 1") ){ JOptionPane.showMessageDialog(this, "You selected Menu Item 1."); }else if(e.getActionCommand().equals("Menu Item 2")){ JOptionPane.showMessageDialog(this, "You selected Menu Item 2."); } }

Dialog box
JDialog class can be extended to create dialog boxes. Component can be placed on the dialog box using a layout manager. (setLayout() method)

Flowlayout, Borderlayout, ., ect


Use event listeners to handle events.

import import import import import import import import

java.awt.event.ActionEvent; java.awt.event.ActionListener; java.awt.event.WindowEvent; java.awt.event.WindowListener; javax.swing.JButton; javax.swing.JDialog; javax.swing.JLabel; javax.swing.JTextField;

public class myDialog extends JDialog implements WindowListener , ActionListener{ JTextField text1 , text2, text3; JButton add,clear; JLabel label1 , label2, label3; public myDialog() { setTitle("Dialog box"); text1 = new JTextField(10); text2 = new JTextField(10); text3 = new JTextField(10); add = new JButton("Add"); clear = new JButton("clear"); label1 = new JLabel("X = "); label2 = new JLabel("Y = ");

setLayout(null); add(text1); text1.setBounds(80, 20, 90, 20); text1.setHorizontalAlignment(javax.swing.JTextField.RIGHT); add(text2); text2.setBounds(80, 80, 90, 20); text2.setHorizontalAlignment(javax.swing.JTextField.RIGHT); add(text3); text3.setBounds(80, 140, 90, 20); text3.setHorizontalAlignment(javax.swing.JTextField.RIGHT); add(label1); label1.setBounds(10, 20, 60, 14); add(label2); label2.setBounds(10, 80, 60, 14); add(label3); label3.setBounds(10, 140, 60, 14); add(add); add.setBounds(10, 200, 73, 23); add(clear); clear.setBounds(100, 200, 73, 23); add.addActionListener(this); clear.addActionListener(this); setSize(230, 270); }

public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("Add")){ if(!text1.getText().equals("") && !text2.getText().equals("")){ int x = Integer.parseInt(text1.getText()); int y = Integer.parseInt(text2.getText()); text3.setText(String.valueOf(x+y) ); } }else if(e.getActionCommand().equals("clear")){ text1.setText(""); text2.setText(""); text3.setText(""); } } }

Event handling with anonymous inner classes


. add.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if(!text1.getText().equals("") && ! text2.getText().equals("")){ int x = Integer.parseInt(text1.getText()); int y = Integer.parseInt(text2.getText()); text3.setText(String.valueOf(x+y) ); } } }); clear.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { text1.setText(""); text2.setText(""); text3.setText(""); } });

Você também pode gostar