|
Os
arquivos abaixo foram extraídos do Livro: An Introduction to Graphical User Interfaces with Java Swing |
|
package its.MouseEvents; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class MouseEventDriver { public static void main(String[] args) { MouseEventFrame MEF = new
MouseEventFrame(); MEF.showIt(); } } |
|
package its.MouseEvents; import
its.SimpleFrame.SimpleFrame; import
java.awt.BorderLayout; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
MouseEventFrame extends SimpleFrame { MouseEventPanel mePanel = new
MouseEventPanel(); StatusPanel stPanel = new StatusPanel(); public MouseEventFrame() { this.setTitle("Mouse
application");
this.getContentPane().add(mePanel,BorderLayout.CENTER);
this.getContentPane().add(stPanel,BorderLayout.SOUTH); pack(); MyMousePositionsListener mPosAdpt = new
MyMousePositionsListener(stPanel); mePanel.addMouseMotionListener(mPosAdpt); MyMouseListener MAdpt = new
MyMouseListener(stPanel); mePanel.addMouseListener(MAdpt); } } |
|
package its.MouseEvents; import java.awt.Color; import java.awt.Dimension; import javax.swing.JPanel; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
MouseEventPanel extends JPanel { public MouseEventPanel() { this.setBackground(Color.white); this.setPreferredSize(new
Dimension(300,300)); } } |
|
package its.MouseEvents; import
java.awt.event.MouseEvent; import
java.awt.event.MouseListener; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
MyMouseListener implements MouseListener { private StatusPanel
statusPane; public MyMouseListener(StatusPanel s) { statusPane = s; } public void mouseEntered(MouseEvent e) { statusPane.setInOut("yes"); } public void mouseExited(MouseEvent e) { statusPane.setInOut("no"); statusPane.setCoordinates(-1,-1); } public void mouseClicked(MouseEvent e) { statusPane.incrementClickCount(); } public void mousePressed(MouseEvent e) { //implementd with empty body } public void mouseReleased(MouseEvent e) { //implementd with empty
body } } |
|
package its.MouseEvents; import
java.awt.event.MouseEvent; import
java.awt.event.MouseMotionListener; /** * Example program for Introduction
to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
MyMousePositionsListener implements MouseMotionListener { private StatusPanel statusPane; public MyMousePositionsListener(StatusPanel
s) { statusPane = s; } public void mouseMoved(MouseEvent evt) {
statusPane.setCoordinates(evt.getX(),evt.getY()); } public void mouseDragged(MouseEvent evt) { // implemented with emtpy
body } } |
|
package its.MouseEvents; import
java.awt.GridLayout; import javax.swing.JLabel; import javax.swing.JPanel; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class StatusPanel
extends JPanel { private JLabel posText = new JLabel("position:"); private JLabel XCoord = new JLabel("0",
JLabel.RIGHT); private JLabel YCoord = new JLabel("0",
JLabel.RIGHT); private JLabel countText = new
JLabel("no. of clicks"); private JLabel counts = new JLabel("0",
JLabel.RIGHT); private JLabel dummy1 = new JLabel(); private JLabel inOutText = new
JLabel("mouse is in comp."); private JLabel inOut = new JLabel("no",
JLabel.RIGHT); private JLabel dummy2 = new JLabel(); private int clickCount = 0; public StatusPanel() { this.setLayout(new GridLayout(3,3)); this.add(posText); this.add(XCoord); this.add(YCoord); this.add(countText); this.add(counts); this.add(dummy1); this.add(inOutText); this.add(inOut); this.add(dummy2); } // Updates the displayed
coordinates of the mouse position. public void setCoordinates(int x, int y) { XCoord.setText(Integer.toString(x)); YCoord.setText(Integer.toString(y)); } // Sets the innformation
whether the mouse is in or out. public void setInOut(String str) { inOut.setText(str); } // Increments the click
count and displays it. public void incrementClickCount(){ clickCount++;
counts.setText(Integer.toString(clickCount)); } } |
esboço:
