대학교 수업/Java 프로그래밍

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

Jongung 2022. 10. 3. 20:52

1번

import javax.swing.*;

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

 

2번

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

public class Chapter9 extends JFrame {
    Chapter9(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("BorderLayout Practice");
        setSize(400, 200);

        Container c = getContentPane();
        c.setLayout(new BorderLayout(5,7)); //수평 수직 간격 (gap)

        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);
        setVisible(true);
    }
    public static void main(String args[]){
        new Chapter9();
    }
}

 

3번

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

public class Chapter9 extends JFrame {
    Chapter9(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("BorderLayout Practice");
        Container c = getContentPane();
        c.setLayout(new GridLayout(1,10));

        for(int i=0; i<10; i++) {
            c.add(new JButton(Integer.toString(i)));
        }
        setSize(600, 300);
        setVisible(true);
    }
    public static void main(String args[]){
        new Chapter9();
    }
}

 

4번

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

public class Chapter9 extends JFrame {
    Chapter9(){
        Color[] colorArr= {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN,
                Color.CYAN, Color.BLUE, Color.MAGENTA,
                Color.GRAY, Color.PINK, Color.LIGHT_GRAY};
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("BorderLayout Practice");
        Container c = getContentPane();
        c.setLayout(new GridLayout(1,10));

        for(int i=0; i<10; i++) {
            JButton btn = new JButton(Integer.toString(i));
            btn.setOpaque(true);
            btn.setBackground(colorArr[i]);
            btn.setBorderPainted(false);
            c.add(btn);
        }
        setSize(600, 300);
        setVisible(true);
    }
    public static void main(String args[]){
        new Chapter9();
    }
}

 

5번

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

public class Chapter9 extends JFrame {
    Chapter9(){
        Color[] colorArr= {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN,
                Color.CYAN, Color.BLUE, Color.MAGENTA,
                Color.GRAY, Color.PINK, Color.LIGHT_GRAY,
                Color.WHITE, Color.DARK_GRAY, Color.BLACK, Color.ORANGE,
                Color.BLUE, Color.MAGENTA};
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("BorderLayout Practice");
        Container c = getContentPane();
        c.setLayout(new GridLayout(4,4));

        for(int i=0; i<16; i++) {
            JLabel lbl = new JLabel(Integer.toString(i));
            lbl.setOpaque(true);
            lbl.setBackground(colorArr[i]);
            c.add(lbl);
        }
        setSize(600, 300);
        setVisible(true);
    }
    public static void main(String args[]){
        new Chapter9();
    }
}

 

6번

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

public class Chapter9 extends JFrame {
    Chapter9(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Random Labels");
        Container c = getContentPane();
        c.setLayout(null);

        for (int i = 0; i < 20; i++) {
            JLabel lbl = new JLabel(Integer.toString(i));
            int x = (int) (Math.random() * 200) + 50;
            int y = (int) (Math.random() * 200) + 50;
            lbl.setLocation(x, y);
            lbl.setSize(10, 10);
            lbl.setOpaque(true);
            lbl.setBackground(Color.BLUE);
            c.add(lbl);
        }
        setSize(400, 300);
        setVisible(true);
    }
    public static void main(String args[]){
        new Chapter9();
    }
}

 

7번

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


public class Chapter9 extends JFrame {
    Chapter9(){
        setTitle("계산기 프레임");
        setSize(400, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container c = getContentPane();
        JPanel pNorth = new JPanel();
        JPanel pSouth = new JPanel();
        JPanel pCenter = new JPanel();

        pNorth.setBackground(Color.LIGHT_GRAY);
        pCenter.setLayout(new GridLayout(4,4));
        pSouth.setBackground(Color.YELLOW);

        c.add(pCenter);
        c.add(pNorth, BorderLayout.NORTH);
        c.add(pSouth, BorderLayout.SOUTH);

        JLabel l1=new JLabel("수식입력");
        JTextField tf1=new JTextField(10);
        pNorth.add(l1);
        pNorth.add(tf1);

        JLabel l2=new JLabel("계산결과");
        JTextField tf2=new JTextField(10);
        pSouth.add(l2);
        pSouth.add(tf2);

        for(int i=0; i<16; i++) {
            JButton b=new JButton();
            String[] str= {"CE","Enter","+","-","x","/"};
            pCenter.add(b);
            if(i < 10) {
                b.setText(i+"");
            } else {
                b.setText(str[i-10]);
            }
            if(i>11) {
                b.setOpaque(true);
            }
        }
        setVisible(true);
    }
    public static void main(String args[]){
        new Chapter9();
    }
}

 

8번

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

class NorthPanel extends JPanel{
    public NorthPanel() {
        setBackground(Color.LIGHT_GRAY);
        add(new JButton("열기"));
        add(new JButton("닫기"));
        add(new JButton("나가기"));
    }
}
class CenterPanel extends JPanel{
    public CenterPanel() {
        setLayout(null);
        for (int i = 0; i < 20; i++) {
            int x = (int)(Math.random()*250);
            int y = (int)(Math.random()*250);
            JLabel label=new JLabel("*");
            label.setForeground(Color.RED);
            label.setLocation(x,y);
            label.setSize(20, 20);
            label.setOpaque(true);
            add(label);
        }
    }
}
class SouthPanel extends JPanel{
    public SouthPanel() {
        setBackground(Color.YELLOW);
        add(new JButton("Integer Input"));
        add(new TextField(15));
    }
}


public class Chapter9 extends JFrame {
    Chapter9(){
        setTitle("여러 개의 패널을 가진 프레임");
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        NorthPanel nPanel = new NorthPanel();
        CenterPanel nCenter = new CenterPanel();
        SouthPanel nSouth = new SouthPanel();
        add(nPanel, BorderLayout.NORTH);
        add(nCenter, BorderLayout.CENTER);
        add(nSouth, BorderLayout.SOUTH);
        setVisible(true);
    }
    public static void main(String args[]){
        new Chapter9();
    }
}