8 장 1 번 public class MyFrame extends JFrame { public MyFrame(String title) { super(title); this. setsize(400,200); new MyFrame("Let's study Java"); 2번 public class MyBorderLayoutFrame extends JFrame { public MyBorderLayoutFrame() { super("borderlayout Practice"); this. c.setlayout(new BorderLayout(5, 7)); 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); c.add(new JButton("Center"), BorderLayout.CENTER); setsize(400,200); new MyBorderLayoutFrame(); 3번
public class FlowLayoutPractice extends JFrame { public FlowLayoutPractice() { super("flowlayout Practice"); this. Container contentpane = getcontentpane(); contentpane.setlayout(new FlowLayout()); contentpane.add(new JLabel("100")); contentpane.add(new JLabel("+")); contentpane.add(new JLabel("200")); contentpane.add(new JButton("=")); contentpane.add(new JLabel("300")); setsize(400,100); new FlowLayoutPractice(); 4번 public class TenColorButtonFrame extends JFrame { public TenColorButtonFrame() { super("ten Color Buttons Frame"); this. Container contentpane = getcontentpane(); contentpane.setlayout(new GridLayout(1, 10)); Color [] color = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA, Color.GRAY, Color.PINK, Color.LIGHT_GRAY; for(int i=0; i<10; i++) { JButton button = new JButton(Integer.toString(i)); button.setopaque(true); // 배경색이보이게함
button.setbackground(color[i]); // 각버튼의색설정 contentpane.add(button); // 컨텐트팬에버튼부착 setsize(500,200); new TenColorButtonFrame(); 5번 public class Plate4x4Frame extends JFrame { public Plate4x4Frame() { super("4x4 Color Frame"); this. c.setlayout(new GridLayout(4, 4)); JLabel [] label = new JLabel [16]; Color [] color = {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; for(int i=0; i<label.length; i++) { label[i] = new JLabel(Integer.toString(i)); label[i].setopaque(true); label[i].setbackground(color[i]); c.add(label[i]); setsize(500,200); new Plate4x4Frame();
6번 public class RandomLabelFrame extends JFrame { public RandomLabelFrame() { super("random Labels"); this. c.setlayout(null); for(int i=0; i<20; i++) { JLabel label = new JLabel(Integer.toString(i)); label.settext(""); // 문자열없도록초기화 label.setopaque(true); label.setbackground(color.blue); int x = (int)(math.random()*200) + 50; int y = (int)(math.random()*200) + 50; label.setlocation(x,y); label.setsize(10,10); c.add(label); setsize(300,300); new RandomLabelFrame();
9장 1번 public class MouseEventFrame extends JFrame { public MouseEventFrame() { super(" 마우스올리기내리기연습 "); c.setlayout(new FlowLayout()); JLabel label = new JLabel(" 자기야 "); label.addmouselistener(new MouseAdapter() { public void mouseentered(mouseevent e) { la.settext(" 사랑해 "); public void mouseexited(mouseevent e) { la.settext(" 자기야 "); ); c.add(label); static public void main(string [] args) { new MouseEventFrame();... public class MouseEventFrame extends JFrame { public MouseEventFrame() {
super(" 마우스올리기내리기연습 "); c.setlayout(new FlowLayout()); JLabel label = new JLabel(" 자기야 "); label.addmouselistener(new MouseAdapter() { public void mouseentered(mouseevent e) { la.settext(" 사랑해 "); public void mouseexited(mouseevent e) { la.settext(" 자기야 "); ); c.add(label); static public void main(string [] args) { new MouseEventFrame(); 2번 public class KeyEventFrame extends JFrame { KeyEventFrame() { super(" 키누르기떼기연습 "); c.setbackground(color.red); c.addkeylistener(new KeyAdapter() { public void keypressed(keyevent e) {
); if(e.getkeychar() == 'R') { getcontentpane().setbackground(color.red); public void keyreleased(keyevent e) { if(e.getkeychar() == 'R') { getcontentpane().setbackground(color.cyan); c.requestfocus(); new KeyEventFrame(); 3번 public class MouseDraggingFrame extends JFrame { public MouseDraggingFrame() { super(" 드래깅동안 YELLOW로변경 "); c.setbackground(color.green); MyMouseListener ml = new MyMouseListener(); c.addmousemotionlistener(ml); c.addmouselistener(ml);
class MyMouseListener extends MouseAdapter implements MouseMotionListener { public void mousedragged(mouseevent e) { Container c = (Container)e.getSource(); c.setbackground(color.yellow); public void mousemoved(mouseevent e) { public void mousereleased(mouseevent e) { Container c = (Container)e.getSource(); c.setbackground(color.green); static public void main(string [] args) { new MouseDraggingFrame(); 4번 public class KeyPlusMinusFrame extends JFrame { public KeyPlusMinusFrame() { super("+,- 키로폰트크기조절 "); c.setlayout(new FlowLayout()); JLabel label = new JLabel("Love Java"); label.setfont(new Font("TimesRoman", Font.PLAIN, 10)); // 10 픽셀크기 label.addkeylistener(new KeyAdapter() { public void keypressed(keyevent e) { if(e.getkeychar() == '+') { Font f = la.getfont(); int size = f.getsize(); la.setfont(new Font("TimesRoman", Font.PLAIN, size+5));
else if(e.getkeychar() == '-') { Font f = la.getfont(); int size = f.getsize(); if(size <= 5) return; la.setfont(new Font("TimesRoman", Font.PLAIN, size-5)); ); c.add(label); label.requestfocus(); static public void main(string [] args) { new KeyPlusMinusFrame(); 5번 public class ClickPracticeFrame extends JFrame { public ClickPracticeFrame() { super(" 클릭연습 "); c.setlayout(null); JLabel label = new JLabel("C"); label.setlocation(50,50); label.setsize(20, 20);
label.addmouselistener(new MouseAdapter() { public void mousepressed(mouseevent e) { Container c = la.getparent(); int xbound = c.getwidth() - la.getwidth(); // 레이블의폭만큼감소 int ybound = c.getheight() - la.getheight(); // 레이블의높이만큼감소 int x = (int)(math.random()*xbound); int y = (int)(math.random()*ybound); la.setlocation(x, y); ); c.add(label); static public void main(string [] args) { new ClickPracticeFrame(); 6번 public class FrameResize extends JFrame { FrameResize() { super(" 키로프레임크기조절 "); Container contentpane = getcontentpane(); // 컨텐트팬알아내기 contentpane.setlayout(new FlowLayout()); // 배치관리자 contentpane.add(new JButton("A")); // 더미버튼추가 contentpane.addkeylistener(new MyKeyListener()); // 키리스너달기 setsize(150, 150); contentpane.requestfocus(); // 컨텐트팬에포커스설정. 키입력가능해짐 class MyKeyListener extends KeyAdapter { // 키리스너
public void keypressed(keyevent e) { int w = getwidth(); // JFrame 윈도우의너비 int h = getheight(); // JFrame 윈도우의높이 switch (e.getkeycode()) { // 입력된키문자 case KeyEvent.VK_F1: setsize(w + 10, h + 10); // JFrame 10픽셀증가 break; case KeyEvent.VK_F2: setsize(w - 10, h - 10); // JFrame 10픽셀축소 break; case KeyEvent.VK_Q : System.exit(0); // 프로그램종료 new FrameResize(); 7번 public class ButtoActionFrame extends JFrame { String text [] = {"+2", "-1", "%4"; JLabel la = new JLabel(); JButton btn [] = new JButton [text.length]; ButtoActionFrame() { super("0으로만들기 "); // 정수레이블생성 int num= (int)(math.random()*60 + 1); // 1에서 60 사이의임의의정수 la.settext(integer.tostring(num)); // 정수를문자열로만들어레이블에출력 la.setfont(new Font("Gothic", Font.ITALIC, 20)); JPanel p = new JPanel(); c.add(p, BorderLayout.CENTER);
p.add(la); JPanel q = new JPanel(); c.add(q, BorderLayout.SOUTH); // 3 개의버튼생성 MyActionListener listener = new MyActionListener(); // Action 리스너객체생성 for(int i=0; i<text.length; i++) { btn[i] = new JButton(text[i]); btn[i].addactionlistener(listener); // 리스너달기 q.add(btn[i]); // 컨텐트팬에버튼달기 class MyActionListener implements ActionListener { public void actionperformed(actionevent e) { ButtoActionFrame.this.setTitle("0 으로만들기 "); // 타이틀바초기화 int n = Integer.parseInt(la.getText()); // 현재정수를알아냄 switch(e.getactioncommand()) { case "+2": n += 2; // 2 증가 btn[0].setenabled(false); // 버튼비활성화시켜다시클릭되지않게함 break; case "-1": n--; // 1 증가 btn[1].setenabled(false); // 버튼비활성화시켜다시클릭되지않게함 break; case "%4": n %= 4; // 4로나눈나머지계산 btn[2].setenabled(false); // 버튼비활성화시켜다시클릭되지않게함 break; la.settext(integer.tostring(n)); // 정수를문자열로만들어레이블에출력 if(n == 0) { ButtoActionFrame.this.setTitle(" 성공 "); for(int i=0; i<text.length; i++) { btn[i].setenabled(true);
else { int num= (int)(math.random()*60 + 1); // 1에서 60 사이의임의의정수 la.settext(integer.tostring(num)); // 정수를문자열로만들어레이블에출력 if(btn[0].isenabled() == false && btn[1].isenabled() == false && btn[2].isenabled() == false) settitle(" 실패 "); new ButtoActionFrame();