반응형
객체지향프로그래밍 9장 GUI 실습
실습문제 1번 swing 패키지 JFrame 상속
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import javax.swing.*;
public class MyApp extends JFrame{
public MyApp(){
setTitle("Let's study Java");
setSize(400,200);
setVisible(true);
}
}
class ttt{
public static void main(String[] args) {
MyApp f = new MyApp();
}
}
|
cs |
실습문제 2번 awt 패키지 JButton 활용
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
|
import javax.swing.*;
import java.awt.*;
class App extends JFrame{
public App(){
setTitle("BorderLayout Practice");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(5,7));
add(new JButton("Center"), BorderLayout.CENTER);
add(new JButton("North"), BorderLayout.NORTH);
add(new JButton("South"), BorderLayout.SOUTH);
add(new JButton("East"), BorderLayout.EAST);
add(new JButton("West"), BorderLayout.WEST);
setSize(400,200);
setVisible(true);
}
}
public class Bordor{
public static void main(String[] args) {
App f = new App();
}
}
|
cs |
실습문제 3번 awt와 스윙(swing)
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
|
import javax.swing.*;
import java.awt.*;
class App extends JFrame{
public App(){
setTitle("Ten Buttons Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1,10,5,5));
add(new JButton("0"));
add(new JButton("1"));
add(new JButton("2"));
add(new JButton("3"));
add(new JButton("4"));
add(new JButton("5"));
add(new JButton("6"));
add(new JButton("7"));
add(new JButton("8"));
add(new JButton("9"));
setSize(400,200);
setVisible(true);
}
}
public class Grid{
public static void main(String[] args) {
App f = new App();
}
}
|
cs |
실습문제 4번 컴포넌트 활용
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
|
import javax.swing.*;
import java.awt.*;
class App extends JFrame{
public App(){
setTitle("Ten Buttons Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1,10,5,5));
// Container contentPane = getContentPane();
// contentPane.setBackground(Color.red);
Component cmp;
cmp = add(new JButton("0"));
cmp.setBackground(Color.red);
cmp = add(new JButton("1"));
cmp.setBackground(Color.orange);
cmp = add(new JButton("2"));
cmp.setBackground(Color.YELLOW);
cmp = add(new JButton("3"));
cmp.setBackground(Color.green);
cmp = add(new JButton("4"));
cmp.setBackground(Color.blue);
cmp = add(new JButton("5"));
cmp.setBackground(Color.cyan);
cmp = add(new JButton("6"));
cmp.setBackground(Color.LIGHT_GRAY);
cmp= add(new JButton("7"));
cmp.setBackground(Color.pink);
cmp = add(new JButton("8"));
cmp.setBackground(Color.DARK_GRAY);
cmp = add(new JButton("9"));
cmp.setBackground(Color.magenta);
setSize(400,200);
setVisible(true);
}
}
public class Grid2{
public static void main(String[] args) {
App f = new App();
}
}
|
cs |
실습문제 5번 4x4 Color Frame 구현
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
|
import javax.swing.*;
import java.awt.*;
public class Lay extends JFrame{
Lay(){
setTitle("4x4 Color Frame");
setSize(500, 300);
setLayout(new GridLayout(4,4));
JLabel l[] = {new JLabel("0"),new JLabel("1"),new JLabel("2"),
new JLabel("3"),new JLabel("4"),new JLabel("5"),
new JLabel("6"),new JLabel("7"),new JLabel("8"),
new JLabel("9"),new JLabel("10"),new JLabel("11"),
new JLabel("12"),new JLabel("13"),new JLabel("14"),
new JLabel("15"),};
l[0].setBackground(Color.red);
l[1].setBackground(Color.orange);
l[2].setBackground(Color.yellow);
l[3].setBackground(Color.green);
l[4].setBackground(Color.cyan);
l[5].setBackground(Color.blue);
l[6].setBackground(Color.magenta);
l[7].setBackground(Color.LIGHT_GRAY);
l[8].setBackground(Color.pink);
l[9].setBackground(Color.gray);
l[10].setBackground(Color.white);
l[11].setBackground(Color.DARK_GRAY);
l[12].setBackground(Color.black);
l[13].setBackground(Color.orange);
l[14].setBackground(Color.blue);
l[15].setBackground(Color.magenta);
for(int i =0; i<16; i++){
l[i].setOpaque(true);
add(l[i]); // Fram에 만든 Label을 붙임
}
setVisible(true);
}
public static void main(String[] args) {
new Lay();
}
}
|
cs |
실습문제 6번 Random Labels 구현
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
|
import javax.swing.*;
import java.awt.*;
public class BorderLayoutEx extends JFrame{
BorderLayoutEx(){
setTitle("Random Labels");
setSize(300, 300);
setLayout(null); // Layout 초기화
for (int i = 0; i<20; i++){
JLabel l = new JLabel();
int x=(int)(Math.random()*200)+50;
int y=(int)(Math.random()*200)+50;
l.setLocation(x, y);
l.setSize(10, 10);
l.setBackground(Color.blue);
l.setOpaque(true); // 색상 보여주기
add(l);
}
setVisible(true); // 프레임 보여주기
}
public static void main(String[] args) {
new BorderLayoutEx();
}
}
|
cs |
반응형
'학습공간 > JAVA, 객체지향프로그래밍' 카테고리의 다른 글
[8주차] JDBC 실습Ⅰ (0) | 2019.11.28 |
---|---|
[7주차] 자바의 이벤트 처리 (0) | 2019.11.28 |
[5주차] 제네릭과 컬렉션 (0) | 2019.11.28 |
[4주차] 상속 (0) | 2019.11.28 |
[3주차] 클래스와 객체 (0) | 2019.11.28 |