티스토리 뷰
반응형
⊙ 문제
14장 프로그래밍 문제(https://tooo1.tistory.com/184)에서 작성한 카드 돌리기 프로그램의 외형에서 각 버튼을 클릭하면 최초 카드, 이전 카드, 다음 카드, 마지막 카드로 넘기도록 프로그램을 완성하시오.
[쉽게 배우는 자바 프로그래밍] 14장 : 8번 - JAVA[자바]
⊙ 문제 5개의 카드 돌리기 프로그램의 외형을 구성하시오. 버튼을 사용해 카드를 넘기는 이벤트 처리는 다음 장에서 살펴보고, 여기서는 화면만 구성한다. 카드번호가 나오는 부분은 CardLayout
tooo1.tistory.com
⊙ 문제 접근 과정
총 4개의 색에 대해 만들었다.
⊙ 문제 풀이
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame implements ActionListener{
JPanel panel;
Cards cards;
public Main(){
setTitle("카드 레이아웃");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
panel = new JPanel(new GridLayout(0, 4, 10, 0));
addButton("<<", panel);
addButton("<", panel);
addButton(">", panel);
addButton(">>", panel);
add(panel, BorderLayout.NORTH);
cards = new Cards();
add(cards, BorderLayout.CENTER);
setVisible(true);
}
public void addButton(String str, Container target){
JButton bt = new JButton(str);
bt.addActionListener((ActionListener) this);
target.add(bt);
}
class Cards extends JPanel{
CardLayout layout;
public Cards(){
layout = new CardLayout();
setLayout(layout);
for(int i=0 ; i<=3 ; i++){
JButton bt = new JButton();
bt.setText("카드 번호" + (i+1) + "!");
Color[] color = {Color.YELLOW, Color.BLUE, Color.PINK, Color.RED};
bt.setBackground(color[i]);
add(bt, BorderLayout.CENTER);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("<<")){
cards.layout.first(cards);
}
if(e.getActionCommand().equals("<")){
cards.layout.previous(cards);
}
if(e.getActionCommand().equals(">")){
cards.layout.next(cards);
}
if(e.getActionCommand().equals(">>")){
cards.layout.last(cards);
}
}
public static void main(String[] args){
new Main();
}
}
⊙ 결과
⊙ 마무리
NONE
좋아요는 로그인하지 않아도 누를 수 있습니다!
728x90
반응형
'쉽게 배우는 자바 프로그래밍 > 15장' 카테고리의 다른 글
[쉽게 배우는 자바 프로그래밍] 15장 : 6번 - JAVA[자바] (0) | 2021.06.02 |
---|---|
[쉽게 배우는 자바 프로그래밍] 15장 : 5번 - JAVA[자바] (0) | 2021.06.02 |
[쉽게 배우는 자바 프로그래밍] 15장 : 3번 - JAVA[자바] (0) | 2021.06.02 |
[쉽게 배우는 자바 프로그래밍] 15장 : 1번 - JAVA[자바] (0) | 2021.06.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 답
- 프로그래머스
- 자바스크립트
- JS
- Web
- 정렬
- 쉽게배우는
- java
- 파이썬
- 알고리즘
- 구현
- py
- 자바
- 연습문제
- 쉽게배우는자바프로그래밍
- 해답
- 우종정
- 그리디
- 운영체제
- OS
- BFS
- CPP
- 백준
- C++
- 정답
- 정리
- 쉽게 배우는 자바 프로그래밍
- 풀이
- Python
- 문자열
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함