728x90
반응형
package Ex01;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CClick extends JFrame{
private JLabel la = new JLabel("C");
public CClick() {
setTitle("클릭");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
la.setSize(20,20);
la.setLocation(100,100);
c.add(la);
la.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JLabel la = (JLabel)e.getSource();
Container c = la.getParent();
//컨테이너 크기 내에서 랜덤한 레이블 위치 설정
int xBound = c.getWidth() - la.getWidth();
int yBound = c.getHeight() - la.getHeight();
int x = (int)(Math.random()*xBound);
int y = (int)(Math.random()*yBound);
la.setLocation(x, y);
}
});
setVisible(true);
}
public static void main(String[] args) {
new CClick();
}
}
(int)(Math.random()*xBound) 처럼 Math.random()*xBound를 ()처리 해주어야 함.
728x90
반응형
'JAVA > 예제' 카테고리의 다른 글
[JAVA] 신호등 (0) | 2022.10.28 |
---|---|
[JAVA] addActionListener 카운터증가 (0) | 2022.10.28 |
[JAVA] Vector를 활용한 그래픽 에디터 (0) | 2022.10.27 |
[JAVA] HashMap를 활용한 장학생선발 (0) | 2022.10.27 |
[JAVA] HashMap를 활용한 포인트관리 (0) | 2022.10.27 |
댓글