Results 1 to 5 of 5
Thread: MouseListener - Flashing label
- 11-29-2010, 01:46 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
MouseListener - Flashing label
Good morning again everyone. I am trying to create a program that has a flashing label , and it stops flashing when we click/hold on the mouse, then starts flashing again when we release the click on the mouse. I thought I have all the code below, so it flashes, but when I try to use the MouseListener, the MouseListener is not working. The flashing never stops. '
Can someone point me in the right direction? Like, was MouseListener not declared... etc etc. There are no compiler errors at all. Thanks to all moderators and people who help us noobs out there........
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FlashingLabel extends JFrame { public FlashingLabel() { this.getContentPane().add(new FlashLabel("Welcome to Java")); } // Main method public static void main(String[] args) { // Create a frame JFrame frame = new FlashingLabel(); frame.setTitle("Flashing Label"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Display the frame frame.setSize(200, 200); frame.setLocationRelativeTo(null); // Center the frame frame.setVisible(true); } class FlashLabel extends JPanel implements ActionListener, MouseListener { private boolean show = true; private Timer timer = new Timer(200, this); String label = "Welcome to Java"; public FlashLabel(String label) { this.label = label; timer.start(); } public void setLabel(String label) { this.label = label; } public void paintComponent(Graphics g) { super.paintComponent(g); if (show) g.drawString(label, 20, 20); show = !show; } public Dimension getPreferredSize() { return new Dimension(200, 50); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { timer.stop(); } public void mouseReleased(MouseEvent e) { timer.start(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void actionPerformed(java.awt.event.ActionEvent actionEvent) { repaint(); } } }
- 11-29-2010, 02:24 PM #2
I don't see any calls to addMouseListener() anywhere in your code. And the only thing the Timer does is call repaint(), which won't do anything by itself. You shouldn't have "business logic" in your paintComponet method: what would happen if the user resized the JFrame?
- 11-29-2010, 03:12 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
I will find out where to add the mouselistener. Thanks for pointing that out.
I wanted the timer to call repaint() - my idea was to have the label start flashing again after the mousepress is released. I guess I should just call repaint(), but that would only repaint it once , correct? That is why I wanted the timer to start up again.
- 11-29-2010, 03:20 PM #4
The Timer is a fine idea, if that's what you want. But all your logic (and by that, I mean toggling of the shown variable) is in your paintComponent() method. That's a no-no. Instead, put your logic in your Timer, then call repaint.
- 11-29-2010, 09:31 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
can't get x and y from mouselistener
By j2me64 in forum Java 2DReplies: 3Last Post: 04-24-2010, 04:57 PM -
Remove Flashing in Applet
By Unome in forum Java AppletsReplies: 5Last Post: 05-30-2009, 07:26 PM -
Flashing
By Supamagier in forum Java 2DReplies: 6Last Post: 04-29-2009, 03:43 PM -
i need help for MouseListener
By sfaxianovic in forum New To JavaReplies: 2Last Post: 08-21-2008, 03:30 AM -
MouseListener
By Aswq in forum New To JavaReplies: 12Last Post: 07-18-2008, 08:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks