본문 바로가기
IT/프로그래밍

명품 JAVA Programming 연습문제 9장

by nutrient 2020. 11. 29.
728x90
728x170

명품 JAVA Programming 연습문제 9장

 

1.다음 그림과 같이 “Let's study java"라는 문자열을 타이틀로 가지고 프레임의 크기가 400*200인 스윙 프로그램을 작성하라.

import javax.swing.*;



 class F1 extends JFrame

{

F1()

{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("Let's study java");

setSize(400,200);

setVisible(true);

}

}



public class M1

{

public static void main(String[] args)

{

F1 A = new F1();

}

}

 

2. BorderLayout을 사용하여 컴포넌트 사이의 수평 간격이 5픽셀, 수직 간격이 7픽셀이 되도록 다음 그림과 같은 스윙 응용프로그램을 작성하라.

import javax.swing.*;

import java.awt.*;



 class F2 extends JFrame

{

F2()

{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("BorderLayout Practice");



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 M2

{

public static void main(String[] args)

{

F2 A = new F2();

}

}

 

3. GridLayout을 사용하여 다음 그림과 같이 한 줄에 10개의 버튼을 동일한 크기로 배치하는 스윙 프로그램을 작성하라.

import javax.swing.*;

import java.awt.*;



 class F3 extends JFrame

{

F3()

{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("BorderLayout Practice");



GridLayout grid = new GridLayout(1,10);

setLayout(grid);

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(600,200);

setVisible(true);

}

}



public class M3

{

public static void main(String[] args)

{

F3 A = new F3();

}

}

 

4. 문제 3을 수정하여 다음 결과와 같이 각 버튼의 배경색을 서로 다르게 설정하라.

(힌트 : 컴포넌트의 배경색을 노란색으로 설정하려면 comp.setBackground(Color.yellow);로 하면 된다.)

import javax.swing.*;

import java.awt.*;



 class F4 extends JFrame

{

F4()

{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("BorderLayout Practice");



GridLayout grid = new GridLayout(1,10);

setLayout(grid);



JButton A0 = new JButton("0");

A0.setBackground(Color.black);

add(A0);



JButton A1 = new JButton("1");

A1.setBackground(Color.red);

add(A1);



JButton A2 = new JButton("2");

A2.setBackground(Color.blue);

add(A2);



JButton A3 = new JButton("3");

A3.setBackground(Color.yellow);

add(A3);



JButton A4 = new JButton("4");

A4.setBackground(Color.gray);

add(A4);



JButton A5 = new JButton("5");

A5.setBackground(Color.green);

add(A5);



JButton A6 = new JButton("6");

A6.setBackground(Color.white);

add(A6);



JButton A7 = new JButton("7");

A7.setBackground(Color.orange);

add(A7);



JButton A8 = new JButton("8");

A8.setBackground(Color.pink);

add(A8);



JButton A9 = new JButton("9");

A9.setBackground(Color.magenta);

add(A9);



setSize(600,200);

setVisible(true);

}

}



public class M4

{

public static void main(String[] args)

{

F4 A = new F4();



}

}


5. GridLayout을 이용하여 다음 그림과 같이 Color.white, color.gray, Color.red 등 16개의 색을 배경색으로 하는 4*4 바둑판을 구성하라.

(힌트: 16개의 JLabel 컴포넌트를 생성하고 각 레이블 컴포넌트의 배경색을 칠한 다음 하나씩 GridLayout을 가진 컨테이너에 붙이면 된다.)


import javax.swing.*;

import java.awt.*;



 class F5 extends JFrame

{

F5()

{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setTitle("BorderLayout Practice");



GridLayout grid = new GridLayout(4,4);

setLayout(grid);



JButton A0 = new JButton("0");

A0.setBackground(Color.black);

add(A0);



JButton A1 = new JButton("1");

A1.setBackground(Color.red);

add(A1);



JButton A2 = new JButton("2");

A2.setBackground(Color.blue);

add(A2);



JButton A3 = new JButton("3");

A3.setBackground(Color.yellow);

add(A3);



JButton A4 = new JButton("4");

A4.setBackground(Color.gray);

add(A4);



JButton A5 = new JButton("5");

A5.setBackground(Color.green);

add(A5);



JButton A6 = new JButton("6");

A6.setBackground(Color.white);

add(A6);



JButton A7 = new JButton("7");

A7.setBackground(Color.orange);

add(A7);



JButton A8 = new JButton("8");

A8.setBackground(Color.pink);

add(A8);



JButton A9 = new JButton("9");

A9.setBackground(Color.magenta);

add(A9);



JButton A10 = new JButton("10");

A10.setBackground(Color.RED);

add(A10);



JButton A11 = new JButton("11");

A11.setBackground(Color.green);

add(A11);



JButton A12 = new JButton("12");

A12.setBackground(Color.blue);

add(A12);



JButton A13 = new JButton("13");

A13.setBackground(Color.white);

add(A13);



JButton A14 = new JButton("14");

A14.setBackground(Color.orange);

add(A14);



JButton A15 = new JButton("15");

A15.setBackground(Color.pink);

add(A15);



setSize(600,200);

setVisible(true);

}

}



public class M5

{

public static void main(String[] args)

{

F5 A = new F5();



}

}

 



6. 20개의 10*10 크기의 JLabel 컴포넌트가 프레임 내의(50,50)위치에서 (250,250)내의 영역에서 랜덤한 위치에 출력되도록 스윙 프로그램을 작성하라. 프레임의 크기를 300*300으로 하고, JLabel의 배경색은 모두 파란색으로 하라.
(힌트 : JLabel 컴포넌트의 ndlcl를 랜덤하게 설정하기 위해(x,y) 좌표는 다음과 같이 구한다.

int x = (int) (Math.random()*200) + 50;
int y = (int) (Math.random()*200) + 50;
label.setLocation(x,y);
label.setSize(10,10);)
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class M6 extends JFrame

{



 M6 () {



  setSize(300, 300);

  setTitle("Random Labels");

  Container con = getContentPane();

  setLayout(null);



  for(int i = 0; i < 20; i++) {



   int x = (int)(Math.random() * 200) + 50;

   int y = (int)(Math.random() * 200) + 50;

   JLabel label = new JLabel("");

   label.setOpaque(true);

   label.setBounds(x, y, 10, 10);

   label.setBackground(Color.BLUE);

   add(label);



  }

  setVisible(true);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }



 public static void main(String[] args)

{

  new M6(); 

 }



}
 
728x90
그리드형

댓글