Results 1 to 3 of 3
- 12-14-2010, 12:08 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 11
- Rep Power
- 0
Help with drawing a string using mouse events
Hey guys, I'm trying to create a string in the position that I click on a JFrame, but I get an error that the values for my x and y cannot be accessed from outside the package and I'm rather confused. I would appreciate any help!
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class adventure extends JFrame implements MouseListener { String message = " "; public adventure() { setSize(500,500); setVisible(true); show(); addMouseListener(this); } public void mouseClicked(MouseEvent e) { int x = e.getX(), y = e.getY(); message="Click"; repaint(); } public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void paint(Graphics g) { super.paint(g); g.drawString(message, x, y); } }
-
Your x and y variables are declared inside the mouseClicked method and are only visible inside of that method. You want to make these variables non-static class variables so that they're visible throughout the class.
- 12-14-2010, 11:09 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Help with some basics please (mouse events)
By tigersarehot in forum AWT / SwingReplies: 4Last Post: 04-28-2010, 02:22 AM -
Major help needed with drawing rectangles using JFrame and Mouse Events
By DamienCurr in forum New To JavaReplies: 1Last Post: 07-16-2009, 02:15 PM -
Mouse events, are they best or only way to go?
By dbashby in forum New To JavaReplies: 2Last Post: 04-10-2009, 04:34 PM -
Need help with looping mouse Events.
By busdude in forum New To JavaReplies: 1Last Post: 04-08-2009, 08:25 PM -
Demonstration of mouse events
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks