<명품 JAVA Programming> - Chapter9 실습문제

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

package chapter9;
import javax.swing.*;

public class Q1 extends JFrame{
	public Q1() {
		setTitle("Let's study Java");
		setSize(400, 200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Q1();
	}
}

 

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

package chapter9;
import javax.swing.*;
import java.awt.*;

public class Q2 extends JFrame{
	public Q2() {
		setTitle("BorderLayout Practice");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = getContentPane();
		
		c.setLayout(new BorderLayout(5, 7));
		c.add(new JButton("Center"), BorderLayout.CENTER);
		c.add(new JButton("East"), BorderLayout.EAST);
		c.add(new JButton("West"), BorderLayout.WEST);
		c.add(new JButton("North"), BorderLayout.NORTH);
		c.add(new JButton("South"), BorderLayout.SOUTH);
		
		setSize(300, 200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Q2();
	}
}

 

 

 

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

package chapter9;
import javax.swing.*;
import java.awt.*;

public class Q3 extends JFrame{
	public Q3() {
		setTitle("Ten Color Buttons Frame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		GridLayout grid = new GridLayout(1, 10);
		grid.setVgap(1);
		Container c = getContentPane();
		
		c.setLayout(grid);
		for(int i = 0; i < 10; i++) {
			//버튼의 내용은 toString 메서드로 i를 문자열로 바꾸어 입력
			c.add(new JButton(Integer.toString(i)));
		}
		
		setSize(450, 200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Q3();
	}
}

 

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

package chapter9;
import javax.swing.*;
import java.awt.*;

public class Q4 extends JFrame{
	public Q4() {
		setTitle("Ten Color Buttons Frame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		GridLayout grid = new GridLayout(1, 10);
		grid.setVgap(1);
		Container c = getContentPane();
		
		c.setLayout(grid);
		for(int i = 0; i < 10; i++) {
			//버튼을 만들고
			JButton btn = new JButton(Integer.toString(i));
			//버튼에 색을 입혀
			btn.setBackground(new Color(255, 255 - 20 * i, 20 * i));
			//추가
			c.add(btn);
			
		}
		
		setSize(450, 200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Q4();
	}
}

 

 

5. GridLayout을 이용하여 다음 그림과 같이 Color.WHITE, Color.GRAY, Color.RED 등 16개의 색을 배경색으로 하는 4X4 바둑판을 구성하라.

package chapter9;
import javax.swing.*;
import java.awt.*;

public class Q5 extends JFrame{
	public Q5() {
		setTitle("4X4 Color Frame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		GridLayout grid = new GridLayout(4, 4);
		grid.setVgap(1);
		Container c = getContentPane();
		
		c.setLayout(grid);
		for(int i = 0; i < 16; i++) {
			//버튼을 만들고
			JButton btn = new JButton(Integer.toString(i));
			//버튼에 색을 입혀
			btn.setBackground(new Color(100, 255 - 15 * i, 15 * i));
			//추가
			c.add(btn);
			
		}
		
		setSize(450, 200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Q5();
	}
}

 

 

6. 20개의 10X10 크기의 JLabel 컴포넌트가 프레임 내에 (50, 50) 위치에서 (250, 250) 영역에서 랜덤한 위치에 출력되도록 스윙 프로그램을 작성하라. 프레임의 크기를 300X300으로 하고, JLabel의 배경색은 모두 파란색으로 하라.

package chapter9;
import javax.swing.*;
import java.awt.*;

public class Q6 extends JFrame {
	public Q6() {
		setTitle("Random Labels");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		setVisible(true);

		Container c = getContentPane();
		c.setLayout(null);
		
		for(int i = 0; i < 20; i++) {
			JLabel label = new JLabel(Integer.toString(i));
			int x = (int)(Math.random()*200) + 50;
			int y = (int)(Math.random()*200) + 50;
			label.setLocation(x, y);
			label.setSize(10, 10);
			label.setOpaque(true);
			label.setBackground(Color.blue);
			
			c.add(label);
		}
	}
	public static void main(String[] args) {
		new Q6();
	}
}

 

 

7. 다음과 같은 GUI 모양을 가진 스윙 프레임을 작성하라. 버튼은 JButton, 텍스트는 JLabel, 입력창은 JTextField를 사용하면 된다.

package chapter9;
import javax.swing.*;
import java.awt.*;

class North extends JPanel {
	public North() {
		setBackground(Color.LIGHT_GRAY);
		setOpaque(true);
		setLayout(new FlowLayout());
		add(new JLabel("수식입력"));
		add(new JTextField(20));
	}
}

class Center extends JPanel {
	JButton b [] = { new JButton("+"), new JButton("-"), new JButton("x"), new JButton("/") };
	public Center() {
		setBackground(Color.white);
		setLayout(new GridLayout(4,4,5,5));
		for(int i = 0; i < 10; i++) {
			JButton num = new JButton(Integer.toString(i));
			add(num);
		}
		add(new JButton("CE"));
		add(new JButton("계산"));
		
		for(int i=0; i<b.length; i++) {
			b[i].setBackground(Color.CYAN);
			add(b[i]);
		}
	}
}

class South extends JPanel {
	public South() {
		setBackground(Color.YELLOW);
		setOpaque(true);
		setLayout(new FlowLayout(FlowLayout.LEFT));
		add(new JLabel("계산 결과"));
		add(new JTextField(15));
	}
}
public class Q7 extends JFrame{
	public Q7() {
		setTitle("계산기 프레임");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		setVisible(true);
		
		Container c = getContentPane(); // 컨텐트팬은 디폴트로 BorderLayout 배치 관리자.
		
		c.add(new North(), BorderLayout.NORTH);
		c.add(new Center(), BorderLayout.CENTER);
		c.add(new South(), BorderLayout.SOUTH);
	}
	
	public static void main(String[] args) {
		new Q7();
	}
}

 

 

8. 다음과 같은 GUI 모양을 가진 스윙 프레임을 작성하라. 10개의 '*' 문자는 10개의 JLabel을 이용하여 랜덤한 위치에 출력하라.

package chapter9;
import javax.swing.*;
import java.awt.*;

class NorthPanel extends JPanel {
	public NorthPanel() {
		setBackground(Color.LIGHT_GRAY);
		setOpaque(true);
		setLayout(new FlowLayout());
		add(new JButton("열기"));
		add(new JButton("닫기"));
		add(new JButton("나가기"));
	}
}

class CenterPanel extends JPanel {
	public CenterPanel() {
		setBackground(Color.white);
		setLayout(null);
		for(int i = 0; i < 10; i++) {
			JLabel label = new JLabel("*");
			int x = (int)(Math.random()*200) + 10;
			int y = (int)(Math.random()*200) + 10;
			label.setLocation(x, y);
			label.setSize(30, 30);
			setOpaque(true);
			
			add(label);
		}
	}
}

class SouthPanel extends JPanel {
	public SouthPanel() {
		setBackground(Color.YELLOW);
		setOpaque(true);
		setLayout(new FlowLayout(FlowLayout.LEFT));
		add(new JButton("Word Input"));
		add(new JTextField(15));
	}
}
public class Q8 extends JFrame{
	public Q8() {
		setTitle("여러 개의 패널을 가진 프레임");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		setVisible(true);
		
		Container c = getContentPane(); // 컨텐트팬은 디폴트로 BorderLayout 배치 관리자.
		
		c.add(new NorthPanel(), BorderLayout.NORTH);
		c.add(new CenterPanel(), BorderLayout.CENTER);
		c.add(new SouthPanel(), BorderLayout.SOUTH);
	}
	
	public static void main(String[] args) {
		new Q8();
	}
}