How to make the title of the text shift away from the mouse, when the mouse is directed to the title of the text in java.
Asked
Active
Viewed 40 times
-1
-
What did you tried? – Blasanka Jun 05 '17 at 07:13
-
Welcome to SO. I think your question is very vague. What text ? what do you mean by "the title of the text" ? – c0der Jun 05 '17 at 07:58
-
Yes. I mean the title of the text – viky Jun 05 '17 at 23:39
-
I tried to make a the titlle of the text .When to hold the mouse in the title of the text then the text is moving away – viky Jun 05 '17 at 23:44
1 Answers
0
You need to use mouseExit
and mouseEntered
actionListener
's to do this:
Simple Example:
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Shift {
JFrame frame;
JLabel label;
Shift(){
initComp();
}
public void initComp(){
frame = new JFrame("Example");
frame.setSize(300,200);
label = new JLabel("Hello");
label.setSize(10, 10);
label.setBackground(Color.RED);
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent evt) {
labelToMiddle(evt);
}
@Override
public void mouseExited(MouseEvent evt) {
labelToEdge(evt);
}
});
frame.add(label);
frame.setVisible(true);
}
private void labelToEdge(MouseEvent evt) {
label.setForeground(Color.RED);
label.setLocation(0,0);
label.repaint();
}
private void labelToMiddle(MouseEvent evt) {
label.setForeground(Color.GREEN);
label.setLocation(100,0);
label.repaint();
}
public static void main(String[] args) {
Shift s = new Shift();
}
}

Blasanka
- 21,001
- 12
- 102
- 104