티스토리 뷰
반응형
⊙ 문제
난수를 발생해 알파벳을 임의의 자리에 표시하는 프로그램을 작성하시오.
알파벳은 JLabel 객체를 사용하고, 화면 크기는 300*200으로 설정한다. 절대 좌표로 GUI 컴포넌트를 배치하려면 배치 관리자가 없어야 한다. |
⊙ 문제 접근 과정
배치 관리자를 없애는 함수 = setLayout(null);
⊙ 문제 풀이
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
Main() {
setTitle("난수 알파벳");
setSize(300,200);
setLayout(new BorderLayout());
JPanel panel = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(null);
setVisible(true);
JLabel a = new JLabel("a");
JLabel b = new JLabel("b");
JLabel c = new JLabel("c");
JLabel d = new JLabel("d");
JLabel e = new JLabel("e");
JLabel f = new JLabel("f");
JLabel g = new JLabel("g");
panel.add(a);
panel.add(b);
panel.add(c);
panel.add(d);
panel.add(e);
panel.add(f);
panel.add(g);
a.setBounds((int)(Math.random()*100),(int)(Math.random()*100),10,10);
b.setBounds((int)(Math.random()*100),(int)(Math.random()*100),10,10);
c.setBounds((int)(Math.random()*100),(int)(Math.random()*100),10,10);
d.setBounds((int)(Math.random()*100),(int)(Math.random()*100),10,10);
e.setBounds((int)(Math.random()*100),(int)(Math.random()*100),10,10);
f.setBounds((int)(Math.random()*100),(int)(Math.random()*100),10,10);
g.setBounds((int)(Math.random()*100),(int)(Math.random()*100),10,10);
add(panel,BorderLayout.CENTER);
}
public static void main(String[] args) {
new Main();
}
}
⊙ 결과

⊙ 마무리
NONE
좋아요는 로그인하지 않아도 누를 수 있습니다!
728x90
반응형
'쉽게 배우는 자바 프로그래밍 > 14장' 카테고리의 다른 글
[쉽게 배우는 자바 프로그래밍] 14장 : 7번 - JAVA[자바] (0) | 2021.06.02 |
---|---|
[쉽게 배우는 자바 프로그래밍] 14장 : 6번 - JAVA[자바] (0) | 2021.06.02 |
[쉽게 배우는 자바 프로그래밍] 14장 : 4번 - JAVA[자바] (0) | 2021.06.02 |
[쉽게 배우는 자바 프로그래밍] 14장 : 3번 - JAVA[자바] (0) | 2021.06.02 |
[쉽게 배우는 자바 프로그래밍] 14장 : 2번 - JAVA[자바] (0) | 2021.06.02 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 연습문제
- BFS
- 구현
- Web
- 정답
- 정렬
- Python
- OS
- 답
- 알고리즘
- 해답
- java
- 프로그래머스
- 쉽게배우는
- 파이썬
- JS
- 그리디
- 정리
- 백준
- 우종정
- 쉽게배우는자바프로그래밍
- C++
- 쉽게 배우는 자바 프로그래밍
- 풀이
- 운영체제
- 자바
- CPP
- py
- 문자열
- 자바스크립트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함