[명품자바프로그래밍] 5판 9장 실습문제

hyeseong·2025년 1월 30일
0

명품자바프로그래밍

목록 보기
12/12
post-thumbnail

명품자바프로그래밍 개정5판 9장 연습문제 실습문제 풀이 p.546-550

1번

코드

import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;

public class Swing {

	public static void main(String[] args) {
		JFrame frame = new JFrame("Make a Window using Swing");
		frame.setSize(400, 150);
		
		Container c = frame.getContentPane();
		c.setBackground(Color.YELLOW);
        
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}

2번

코드

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

public class Swing {

	public static void main(String[] args) {
		JFrame f = new JFrame("BoderLayout 배치 관리자 연습");
		f.setLayout(new BorderLayout(5,7));
		
		Container c = f.getContentPane();
		c.setBackground(Color.yellow);
		
		c.add(new JButton("North"), BorderLayout.NORTH);
		c.add(new JButton("West"), BorderLayout.WEST);
		c.add(new JButton("Center"), BorderLayout.CENTER);
		c.add(new JButton("East"), BorderLayout.EAST);
		c.add(new JButton("South"), BorderLayout.SOUTH);
		
		f.setSize(400,300);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
}

3번

코드

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

public class Swing {
	JFrame f;
	
	public Swing() {
		f = new JFrame("GridLayout으로 10개의 버튼을 배치한 프레임");
		buildGUI();
		f.setSize(600,200);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
	
	private void buildGUI() {
		f.setLayout(new GridLayout(1, 10));
		for(int i=0; i<10; i++) {
			f.add(new JButton(Integer.toString(i)));
		}
	}
	
	public static void main(String[] args) {
		new Swing();
	}
}

4번

코드

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

public class Swing {
	JFrame f;
	Color [] colors = {Color.red, Color.orange, Color.yellow, 
			Color.green, Color.cyan, Color.blue, Color.magenta,
			Color.gray, Color.pink, Color.lightGray};
	
	public Swing() {
		f = new JFrame("배경색을 가진 10개의 버튼");
		buildGUI();
		f.setSize(600,200);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
	
	private void buildGUI() {
		f.setLayout(new GridLayout(1, 10));
		for(int i=0; i<10; i++) {
			JButton btn = new JButton(Integer.toString(i));
			btn.setBackground(colors[i]);
			f.add(btn);
		}
	}
	
	public static void main(String[] args) {
		new Swing();
	}
}

5번

코드

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

public class Swing {
	JFrame f;
	private Color [] colors = {Color.red, Color.orange, 
			Color.yellow, Color.green, Color.cyan, Color.blue, 
			Color.magenta, Color.gray, Color.pink, Color.lightGray,
			Color.white, Color.darkGray, Color.black, 
			Color.orange, Color.blue, Color.magenta};
	
	public Swing() {
		f = new JFrame("4x4 Color 프레임");
		buildGUI();
		f.setSize(700,300);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
	
	private void buildGUI() {
		f.setLayout(new GridLayout(4, 4));
		for(int i=0; i<16; i++) {
			JLabel label = new JLabel(Integer.toString(i));
			label.setOpaque(true); // 불투명 설정
			label.setBackground(colors[i]);
			f.add(label);
		}
	}
	
	public static void main(String[] args) {
		new Swing();
	}
}

JLabel의 기본 값은 투명이므로, 배경색을 지정하려면 setOpaque(true)를 추가하여 불투명하게 설정해야 한다.

6번

코드

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

public class Swing {
	JFrame f;

	public Swing() {
		f = new JFrame("배치관리자 없는 컨테이너");
		buildGUI();
		f.setSize(300,300);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
	
	private void buildGUI() {
		f.setLayout(null);
		for(int i=0; i<20; i++) {
			createLabel();
		}
	}
	
	private void createLabel() {
		int r = (int)(Math.random()*256);
		int g = (int)(Math.random()*256);
		int b = (int)(Math.random()*256);
		
		JLabel label = new JLabel();
		label.setOpaque(true);
		label.setBackground(new Color(r,g,b));

		int x = (int)(Math.random()*240)+10;
		int y = (int)(Math.random()*240)+10;
        
        label.setBounds(x, y, 10, 10);
		f.add(label);
	}
	
	public static void main(String[] args) {
		new Swing();
	}
}

7번

코드

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

public class Swing extends JFrame {

	public Swing() {
		super("자바 스윙 계산기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		buildGUI();
		
		setSize(300,300);
		setVisible(true);
	}	
	
	private void buildGUI() {
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		c.add(new NorthPanel(),BorderLayout.NORTH);
		c.add(new CenterPanel(), BorderLayout.CENTER);
		c.add(new SouthPanel(), BorderLayout.SOUTH);
	}
	
	class NorthPanel extends JPanel {
		public NorthPanel() {
			setLayout(new FlowLayout());
			setOpaque(true);
			setBackground(Color.lightGray);
			add(new JLabel("수식"));
			add(new JTextField(15));
		}
	}
	
	class SouthPanel extends JPanel {
		public SouthPanel() {
			setLayout(new FlowLayout(FlowLayout.LEFT));
			setOpaque(true);
			setBackground(Color.yellow);
			add(new JLabel("계산 결과"));
			add(new JTextField(15));
		}
	}
	
	class CenterPanel extends JPanel {
		private JButton buttons[] = {
			new JButton("C"), new JButton("UN"), new JButton("BK"), new JButton("/"),
			new JButton("7"), new JButton("8"), new JButton("9"), new JButton("x"),
			new JButton("4"), new JButton("5"), new JButton("6"), new JButton("-"),
			new JButton("1"), new JButton("2"), new JButton("3"), new JButton("+"),
			new JButton("0"), new JButton("."), new JButton("="), new JButton("%")
		};
		
		public CenterPanel() {
			setLayout(new GridLayout(5,4,5,5));
			for(int i=0; i<20; i++) {
				add(buttons[i]);
			}
			buttons[18].setBackground(Color.cyan);
		}
	}
	
	public static void main(String[] args) {
		new Swing();
	}
}

8번

코드

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

public class Swing extends JFrame {

	public Swing() {
		super("숨겨진 이미지 찾기");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		buildGUI();
		
		setSize(300,300);
		setVisible(true);
	}	
	
	private void buildGUI() {
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
	
		c.add(new NorthPanel(),BorderLayout.NORTH);
		c.add(new WestPanel(), BorderLayout.WEST);
		c.add(new CenterPanel(), BorderLayout.CENTER);
		c.add(new EastPanel(), BorderLayout.EAST);
		c.add(new SouthPanel(), BorderLayout.SOUTH);
	}
	
	class NorthPanel extends JPanel {
		public NorthPanel() {
			setLayout(new FlowLayout());
			setOpaque(true);
			setBackground(Color.yellow);
			add(new JLabel("숨겨진 이미지 찾기"));
		}
	}
	
	class SouthPanel extends JPanel {
		public SouthPanel() {
			setLayout(new FlowLayout());
			setOpaque(true);
			setBackground(Color.yellow);
			add(new JButton("실행 시작"));
		}
	}
	
	class CenterPanel extends JPanel {
		private JLabel[] images = new JLabel[16];

		public CenterPanel() {
			setLayout(new GridLayout(4,4,5,5));
			setOpaque(true);
			setBackground(Color.white);
			
			for(int i=0; i<16; i++) {
				images[i] = new JLabel(Integer.toString(i));
				images[i].setOpaque(true);
				images[i].setBackground(Color.green);
				add(images[i]);
			}
		}
	}
	
	class WestPanel extends JPanel {
		public WestPanel() {
			setBackground(Color.lightGray);
			add(new JLabel("         "));
		}
	}
	
	class EastPanel extends JPanel {
		public EastPanel() {
			setBackground(Color.lightGray);
			add(new JLabel("         "));
		}
	}
	
	public static void main(String[] args) {
		new Swing();
	}
}

images[i].setSize(30, 30) 으로 JLabel의 크기를 설정하려고 했지만, 적용되지 않는 문제가 있었다.

  • 레이아웃 관리자는 컨테이너 내의 컴포넌트 배치와 크기를 자동으로 관리한다.
  • GridLayout : 모든 컴포넌트는 동일한 크기로 자동 조정
  • 따라서, setSize()로 설정된 크기는 레이아웃 관리자가 무시한다.

9번

코드

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

public class Swing extends JFrame{
	
	public Swing() {
		super("랜덤한 별을 가진 프로그램");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		buildGUI();
		setSize(400, 400);
		setVisible(true);
	}
	
	private void buildGUI() {
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		c.add(createNorthPanel(), BorderLayout.NORTH);
		c.add(createCenterPanel(), BorderLayout.CENTER);
		c.add(createSouthPanel(), BorderLayout.SOUTH);
	}
	
	private JPanel createNorthPanel() {
		JPanel northPanel = new JPanel();
		northPanel.setBackground(Color.gray);
		northPanel.add(new JLabel("별 개수"));
		northPanel.add(new JTextField(15));
		northPanel.add(new JButton("별 만들기"));
		return northPanel;
	}
	
	private JPanel createSouthPanel() {
		JPanel southPanel = new JPanel();
		southPanel.setBackground(Color.yellow);
		southPanel.add(new JButton("Exit"));
		return southPanel;
	}
	
	private JPanel createCenterPanel() {
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(null);
		
		JButton refreshButton = new JButton("Refresh");
		refreshButton.setBounds(10, 10, 80, 20);
		centerPanel.add(refreshButton);
		
		for(int i=0; i<15; i++) {
			JLabel starLabel = new JLabel("*");
			int x = (int)(Math.random()*280);
			int y = (int)(Math.random()*180);
			starLabel.setBounds(x, y, 10, 10);
			starLabel.setForeground(Color.red);
			centerPanel.add(starLabel);
		}
		return centerPanel;
	}
	
	public static void main(String[] args) {
		new Swing();
	}
}

10번

코드

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

public class Swing extends JFrame{
	
	public Swing() {
		super("West Grid 프레임");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		buildGUI();
		setSize(400, 400);
		setVisible(true);
	}
	
	private void buildGUI() {
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		c.add(new WestPanel(), BorderLayout.WEST);
		c.add(new CenterPanel(), BorderLayout.CENTER);
	}
	
	class WestPanel extends JPanel{
		Color []colors = { Color.red, Color.gray, Color.blue, 
				Color.yellow, Color.cyan, Color.gray, Color.pink, 
				Color.green, Color.orange, Color.magenta
		};
		
		public WestPanel(){
			setLayout(new GridLayout(10,1));
			for(int i=0; i<10; i++) {
				JButton btn = new JButton();
				btn.setBackground(colors[i]);
				add(btn);
			}	
		}
	}
	
	class CenterPanel extends JPanel{
		public CenterPanel() {
			setLayout(null);
			for(int i=0; i<10; i++) {
				JLabel num = new JLabel(Integer.toString(i));
				int x = (int)(Math.random()*151)+50;
				int y = (int)(Math.random()*151)+50;
				num.setBounds(x, y, 10, 10);
				num.setForeground(Color.red);
				add(num);
			}
		}
	}
	
	public static void main(String[] args) {
		new Swing();
	}
}

개인 풀이이므로 틀린 부분이나 피드백이 있으면 댓글로 남겨주시면 감사하겠습니다!

0개의 댓글