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

import javax.swing.*;
public class StudyJava extends JFrame {
public StudyJava() {
setTitle("Let's study Java");
setSize(400,200);
setVisible(true);
}
public static void main(String[] args) {
new StudyJava();
}
}
BorderLayout을 사용하여 컴포넌트 사이의 수평 수직 간격이 각각 5픽셀, 7픽셀이 되도록 스윙 응용프로그램을 작성하라.

import javax.swing.*;
import java.awt.*;
public class SwingBorderLayout extends JFrame {
public SwingBorderLayout() {
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("North"), BorderLayout.NORTH);
c.add(new JButton("West"), BorderLayout.WEST);
c.add(new JButton("East"), BorderLayout.EAST);
c.add(new JButton("South"), BorderLayout.SOUTH);
setSize(500,300);
setVisible(true);
}
public static void main(String[] args) {
new SwingBorderLayout();
}
}
GridLayout을 사용하여 다음 그림과 같이 한 줄에 10개의 버튼을 배치하는 스윙 프로그램을 작성하라.

import javax.swing.*;
import java.awt.*;
public class TenButtons extends JFrame {
public TenButtons() {
setTitle("Ten Color Buttons Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(1,10));
for(int i=0; i<10; i++) {
c.add(new JButton(i+""));
}
setSize(600,200);
setVisible(true);
}
public static void main(String[] args) {
new TenButtons();
}
}
문제 3을 수정하여 다음 결과와 같이 각 버튼의 배경색을 서로 다르게 설정하라.

import javax.swing.*;
import java.awt.*;
public class TenColorButtons extends JFrame {
public TenColorButtons() {
setTitle("Ten Color Buttons Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(1,10));
for(int i=0; i<10; i++) {
Color[] cb = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA, Color.GRAY, Color.PINK, Color.LIGHT_GRAY};
JButton btn = new JButton(Integer.toString(i));
btn.setOpaque(true);
btn.setBackground(cb[i]);
c.add(btn);
}
setSize(600,200);
setVisible(true);
}
public static void main(String[] args) {
new TenColorButtons();
}
}
UI 차이인지 뭔지 버튼 내부가 아닌 외부의 배경색이 바뀌네요
GridLayout을 이용하여 다음 그림과 같이 Color.WHITE, Color.GRAY, Color.RED 등 16개의 색을 배경색으로 하는 4x4 바둑판을 구성하라.

import javax.swing.*;
import java.awt.*;
public class FourXFour extends JFrame {
public FourXFour() {
setTitle("4x4 Color Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(4,4));
for(int i=0; i<16; i++) {
Color[] cb = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA, Color.GRAY, Color.PINK, Color.LIGHT_GRAY, Color.WHITE, Color.BLACK, Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN};
JLabel la = new JLabel(i+"");
la.setOpaque(true);
la.setBackground(cb[i]);
c.add(la);
}
setSize(600,200);
setVisible(true);
}
public static void main(String[] args) {
new FourXFour();
}
}
20개의 10x10 크기의 JLabel 컴포넌트가 프레임 내에 (50,50) 위치에서 (250,250) 영역에서 랜덤한 위치에 출력되도록 스윙 프로그램을 작성하라. 프레임의 크기를 300x300으로 하고, JLabel의 배경색은 모두 파란색으로 하라.

import javax.swing.*;
import java.awt.*;
public class RandomLabels extends JFrame {
public RandomLabels() {
setTitle("Random Labels");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
for (int i=0; i<20; i++) {
int x = (int)(Math.random()*200)+50;
int y = (int)(Math.random()*200)+50;
JLabel la = new JLabel((int)(Math.random()*100)+"");
la.setLocation(x,y);
la.setSize(10,10);
la.setOpaque(true);
la.setBackground(Color.BLUE);
c.add(la);
}
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new RandomLabels();
}
}
다음과 같은 GUI 모양을 가진 스윙 프레임을 작성하라. Open Challenge의 힌트나 정답을 참고하라. 버튼은 JButton, 텍스트는 JLabel, 입력창은 JTextField를 사용하면 된다.

import javax.swing.*;
import java.awt.*;
public class Calc extends JFrame {
public Calc(){
setTitle("계산기 프레임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
JPanel pn = new JPanel();
JPanel pc = new JPanel();
JPanel ps = new JPanel();
pn.setBackground(Color.GRAY);
pc.setLayout(new GridLayout(4,4));
ps.setBackground(Color.YELLOW);
c.add(pn,BorderLayout.NORTH);
c.add(pc);
c.add(ps, BorderLayout.SOUTH);
JLabel nl = new JLabel("수식입력");
JTextField ntf = new JTextField(10);
pn.add(nl);
pn.add(ntf);
JLabel sl = new JLabel("계산 결과");
JTextField stf = new JTextField(10);
ps.add(sl);
ps.add(stf);
for(int i=0; i<16; i++) {
JButton btn = new JButton();
String[] str = {"CE", "계산", "+", "-", "x", "/"};
pc.add(btn);
if(i<10) {
btn.setText(i+"");
} else {
btn.setText(str[i-10]);
}
if(i>11) {
btn.setOpaque(true);
}
}
setSize(400,500);
setVisible(true);
}
public static void main(String[] args) {
new Calc();
}
}
다음과 같은 GUI 모양을 가진 스윙 프로그램을 작성하라. Open Challenge의 힌트나 정답을 참고하라. 10개의 '*' 문자는 JLabel을 이용하여 랜덤한 위치에 출력하라.

import javax.swing.*;
import java.awt.*;
public class StarPanel extends JFrame {
public StarPanel() {
setTitle("여러 개의 패널을 가진 프레임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
JPanel pn = new JPanel();
JPanel pc = new JPanel();
JPanel ps = new JPanel();
pc.setLayout(null);
c.add(pn, BorderLayout.NORTH);
c.add(pc);
c.add(ps, BorderLayout.SOUTH);
for (int i=0; i<4; i++) {
JButton btn = new JButton();
String[] str = {"열기", "닫기", "나가기", "Word Input"};
btn.setText(str[i]);
if(i<3) {
pn.add(btn);
} else {
ps.add(btn);
}
}
pn.setBackground(Color.GRAY);
ps.setBackground(Color.YELLOW);
JTextField stf = new JTextField(10);
ps.add(stf);
for(int i=0; i<10; i++) {
int x = (int)(Math.random()*200)+10;
int y = (int)(Math.random()*200)+10;
JLabel la = new JLabel("*");
la.setForeground(Color.RED);
la.setLocation(x,y);
la.setSize(30,30);
pc.setOpaque(true);
pc.add(la);
}
setSize(400,400);
setVisible(true);
}
public static void main(String[] args) {
new StarPanel();
}
}