Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8 Object Oriented Programming Using Java

Similar presentations


Presentation on theme: "Lecture 8 Object Oriented Programming Using Java"— Presentation transcript:

1 Lecture 8 Object Oriented Programming Using Java
By Rashid Ahmad Department of Computer Science University of Peshawar

2 Mouse Event Handling Two interfaces MouseListener and MouseMotionListener MouseListener Methods mousePressed mouseClicked mouseReleased mouseEntered mouseExited MouseMotionListener Methods mouseDragged mouseMoved

3 Mouse Events public class MouseTracker extends JFrame
implements MouseListener, MouseMotionListener { private JLabel lblstatus; public MouseTracker() { super("Mouse Events Demonstration"); Container content = getContentPane(); lblstatus = new JLabel(); content.add(lblstatus,BorderLayout.SOUTH); addMouseListener(this); addMouseMotionListener(this); setSize(300,300); setVisible(true); }

4 Mouse Events lblstatus.setText("Mouse Cliked At: "
public void mouseClicked(MouseEvent event){ lblstatus.setText("Mouse Cliked At: " + event.getX() + "," + event.getY()); } public void mousePressed(MouseEvent event){ lblstatus.setText("Mouse Pressed At: " public void mouseReleased(MouseEvent event){ lblstatus.setText("Mouse Released At: " public void mouseEntered(MouseEvent event){ lblstatus.setText("Mouse Entered At: "

5 Mouse Events Write main section for
public void mouseExited(MouseEvent event){ lblstatus.setText("Mouse outside of window "); } public void mouseDragged(MouseEvent event){ lblstatus.setText("Mouse Dragged At: " + event.getX() + "," + event.getY()); public void mouseMoved(MouseEvent event){ lblstatus.setText("Mouse Moved At: " Write main section for

6 Adapter Classes An adapter class implements an interface and provides a default implementation (with an empty method body) of every method in an interface. The programmer can extend the adapter class to inherit the default implementation of every method and can override them. Adapter Class Interface Componet Adapter ComponentListener ContainerAdapter ContainerListener FocusAdapter FocusListener KeyAdapter KeyListener MouseAdapter MouseListener MouseMotionAdapter MouseMotionListener WindowAdapter WindowListener

7 Using Adapter Classes public class AdapterClassTest extends JFrame {
private JLabel lblstatus; public AdapterClassTest() { super("Adapter class demo"); Container content = getContentPane(); lblstatus = new JLabel(); content.add(lblstatus,BorderLayout.SOUTH); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent ev){ lblstatus.setText("Mouse Clicked at: " + ev.getX() + "," + ev.getY()); } }); setSize(300,300); setVisible(true); } }

8 Key event handling KeyListener interface is implemented in order to handle key events. public class KeyEventTest extends JFrame implements KeyListener{ private JTextArea txtarea; String line1 = "", line2 = "", line3 = ""; public KeyEventTest() { super("Key Event Handling"); txtarea = new JTextArea(); txtarea.setText("Press any key on keyboard...."); txtarea.setEnabled(false); txtarea.setDisabledTextColor(Color.GRAY); getContentPane().add(txtarea); addKeyListener(this); setSize(300,300); setVisible(true); }

9 Key event handling public void keyPressed(KeyEvent ev){
line1 = "Key pressed: " + ev.getKeyText(ev.getKeyCode()); this.setLine2and3(ev); } public void keyReleased(KeyEvent ev){ line1 = "Key released: " + ev.getKeyText(ev.getKeyCode()); public void keyTyped(KeyEvent ev){ line1 = "Key typed: " + ev.getKeyChar();

10 Key event handling Write main section for
private void setLine2and3(KeyEvent ev){ line2 = "This key is: " + (ev.isActionKey() ? "":"not") + " an action key"; String temp = ev.getModifiersExText(ev.getModifiers()); line3 = "Modifiers keys pressed: " + (temp.equals("")? "none" : temp); txtarea.setText(line1 + "\n" + line2 + "\n" + line3 + "\n"); } Write main section for

11 Layout Managers Description Layout Manager FlowLayout
Places components sequentially left to right as they were added BorderLayout Arranges the components into five areas (NORTH,SOUTH,EAST,WEST and CENTER) GridLayout Arranges the components into rows and columns Layout Managers are provided to arrange GUI components in a container for presentation purposes. These provides layout capabilities to place the components in a container

12 Layout Demonstration public class GridLayoutTest extends JFrame implements ActionListener { private JButton buttons[]; private final String names[] = {"one","two","three","four","five","six"}; private boolean toggle = true; private Container content; private GridLayout grid1,grid2; public GridLayoutTest() { super("Layout Test"); content = getContentPane(); grid1 = new GridLayout(2,3,5,5); grid2 = new GridLayout(3,2); content.setLayout(grid1);

13 Layout Demonstration buttons = new JButton[names.length];
for(int count=0; count < names.length; count++){ buttons[count] = new JButton(names[count]); buttons[count].addActionListener(this); content.add(buttons[count]); } setSize(300,150); setVisible(true);

14 Layout Demonstration public void actionPerformed(ActionEvent ev){
if(toggle) content.setLayout(grid2); else content.setLayout(grid1); toggle = false; content.validate(); } }// End of class Home Work on Layouts

15 JComboBox with JCheckBox
public class ComboBoxTest extends JFrame implements ItemListener { private JComboBox cmboptions; private JCheckBox chkdb,chkoop,chkos,chkdld; public ComboBoxTest() { super("ComboBox Testing"); Container content = getContentPane(); content.setLayout(new FlowLayout()); chkdb = new JCheckBox("Database"); chkoop = new JCheckBox("OOP"); chkos = new JCheckBox("Operating Systems"); chkdld = new JCheckBox("DLD"); chkdb.addItemListener(this); chkoop.addItemListener(this); chkos.addItemListener(this); chkdld.addItemListener(this);

16 JComboBox with JCheckBox
content.add(chkdb); content.add(chkoop); content.add(chkos); content.add(chkdld); cmboptions = new JComboBox(); content.add(cmboptions); setSize(300,300); setVisible(true); }

17 JComboBox with JCheckBox
public void itemStateChanged(ItemEvent ev){ String itm = ""; String txt = ""; boolean found = false; if(ev.getSource() == chkdb){ itm = chkdb.getText(); } else if(ev.getSource() == chkoop){ itm = chkoop.getText(); else if(ev.getSource() == chkos){ itm = chkos.getText();

18 JComboBox with JCheckBox
else if(ev.getSource() == chkdld){ itm = chkdld.getText(); } for(int x=0; x < cmboptions.getItemCount(); x++){ txt = cmboptions.getItemAt(x).toString(); if(itm == txt){ found = true; break; } } if(!found) cmboptions.addItem(itm); }


Download ppt "Lecture 8 Object Oriented Programming Using Java"

Similar presentations


Ads by Google