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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package ch13_1; import java.awt.Button; import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Bing { public static void main(String[] args) { BingoGame game = new BingoGame("Bingo Game"); // 프레임 객체 생성 } } class BingoGame extends Frame { Button[] btnArr = new Button[5 * 5]; String[] birdArr = new String[btnArr.length]; BingoGame() { this("Bingo Game"); } BingoGame(String title) { super("BingoGame"); // layoutManager를 gridLayout this.setLayout(new GridLayout(5, 5)); // iv를 초기화한다 (iv = button) // 새이름 초기화 // 버튼 생성 for (int i = 0; i < btnArr.length; i++) { birdArr[i] = "" + i; btnArr[i] = new Button(birdArr[i]); btnArr[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 버튼의 이름을 콘솔에 출력 System.out.println(e.getActionCommand()); // 버튼의 배경을 회색으로 Button btn = (Button) e.getSource(); btn.setBackground(Color.LIGHT_GRAY); } }); this.add(btnArr[i]); } this.setSize(new Dimension(500, 500)); this.setVisible(true); // 이벤트 등록 // frame을 화면에 보여준다 } } | cs |
카테고리 없음