티스토리 뷰

반응형

⊙ 문제

 

난수를 발생해 알파벳을 임의의 자리에 표시하는 프로그램을 작성하시오.

 

알파벳은 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
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함