본문 바로가기
IT/프로그래밍

명품 JAVA Programming 연습문제 10장

by nutrient 2020. 11. 29.
728x90
728x170

명품 JAVA Programming 연습문제 10장

 

1. JLabel 컴포넌트는 Mouse 이벤트를 받을 수 있다. JLabel 컴포넌트에 마우스를 올리면 “Love Java”가, 내리면 “사랑해”가 출력되도록 스윙 응용프로그램을 작성하라.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



class Frame1 extends JFrame

{

        JPanel P;

        JLabel La;



        Frame1()

        {

                this.setTitle("마우스 올리기");

                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                P = new JPanel();

                La = new JLabel("사랑해");

                Mouse Mo = new Mouse();

                La.addMouseListener(Mo);

                P.add(La);

                this.add(P);

                this.setVisible(true);

                this.setSize(300, 300);

        }



        class Mouse implements MouseListener

        {



                public void mouseClicked(MouseEvent e)

                {



                }



                public void mouseEntered(MouseEvent e)

                {

                        JLabel La = (JLabel)e.getSource();

                        La.setText("I Love You");

                }



                public void mouseExited(MouseEvent e)

                {

                        JLabel La = (JLabel)e.getSource();

                        La.setText("사랑해");

                }



                public void mousePressed(MouseEvent e)

                {



                }



                public void mouseReleased(MouseEvent e)

                {



                }



        }

}













public class M1

{



        public static void main(String[] args)

        {

                new Frame1();

        }



}

 

2. 컨텐트팬의 배경색은 초록색으로 하고 마우스를 드래깅하는 동안만 노란색으로 유지하는 스윙응용프로그램을 작성하라.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



class Frame2 extends JFrame

{

        JPanel P;



        Frame2()

        {

                this.setTitle("마우스 올리기");

                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                P = new JPanel();

                P.setBackground(Color.GREEN);

                Mouse Mo = new Mouse();

                P.addMouseListener(Mo);

                P.addMouseMotionListener(Mo);

                this.add(P);

                this.setVisible(true);

                this.setSize(300, 300);

        }



        class Mouse implements MouseListener, MouseMotionListener

        {



                public void mouseClicked(MouseEvent e)

                {



                }



                public void mouseEntered(MouseEvent e)

                {



                }



                public void mouseExited(MouseEvent e)

                {



                }



                public void mousePressed(MouseEvent e)

                {

                        JPanel P = (JPanel)e.getSource();

                        P.setBackground(Color.GREEN);

                }



                public void mouseReleased(MouseEvent e)

                {

                        JPanel P = (JPanel)e.getSource();

                        P.setBackground(Color.GREEN);

                }



                public void mouseDragged(MouseEvent e)

                {

                        JPanel P = (JPanel)e.getSource();

                        P.setBackground(Color.YELLOW);

                }



                public void mouseMoved(MouseEvent e)

                {



                }



        }

}



public class M2

{

        public static void main(String[] args)

        {

                new Frame2();

        }



}

 

3.JLabel을 활용하여 “ Love java”를 출력하고 왼쪽 화살표 키 (Left키)를 입력할 때 마다 “avaJ evoL”와 “Love java”를 번갈아 출력하는 스윙 프로그램을 작성하라. StringBuffer 클래스의 reverse() 매소드를 이용하여 구현하는 것과 JLabel에 포커스를 설정하는 것을 잊지 말아야 한다.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



class Frame3 extends JFrame

{

        JPanel P;

        JLabel La;

        StringBuffer SB = new StringBuffer("Love Java");

        Frame3()

        {

                this.setTitle("문자열 바꾸기");

                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                P = new JPanel();

                La = new JLabel("Love java");

                Key K = new Key();

                La.addKeyListener(K);

                P.add(La);

                this.add(P);

                this.setVisible(true);

                this.setSize(300, 300);

                La.requestFocus();

        }



        class Key implements KeyListener

        {



                public void keyPressed(KeyEvent e)

                {

                        if(e.getKeyCode() == KeyEvent.VK_LEFT)

                        {

                                SB.reverse();

                                La.setText(SB.toString());

                        }

                }



                public void keyReleased(KeyEvent e)

                {



                }



                public void keyTyped(KeyEvent e)

                {



                }

        }

}



public class M3

{







        public static void main(String[] args)

        {

                new Frame3();

        }



}

 

4.JLabel을 활용하여 “Love Java”를 출력하고 왼쪽 화살표 키(Left 키)를 입력할 때 마다 “ove JavaL”, “ve JavaLo”, “e JavaLov”등과 같이 계속 한 문자씩 왼쪽으로 이동하는 스윙 프로그램을 작성하라. 문자열의 이동은 String 클래스의 sbustring()메소드를 이용하여 구현하라. String text = “ Love Java”인 경우, text.substring(0, 1)은 “L”을 리턴하고, text.sbustring(1)은 “ove Java”를 리턴한다. JLabel에 포커스 설정하는 것을 잊지 말아야 한다.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



class Frame4 extends JFrame

{

        JPanel P;

        JLabel La;

        String SB = "Love Java"



        Frame4()

        {

                this.setTitle("문자열 바꾸기");

                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                P = new JPanel();

                La = new JLabel(SB);

                Key K = new Key();

                La.addKeyListener(K);

                P.add(La);

                this.add(P);

                this.setVisible(true);

                this.setSize(300, 300);

                La.requestFocus();

        }



        class Key implements KeyListener

        {



                public void keyPressed(KeyEvent e)

                {



                        if(e.getKeyCode() == KeyEvent.VK_LEFT)

                        {

                                String StrStart, StrEnd;

                                StrStart = SB.substring(1);

                                StrEnd = SB.substring(0,1);

                                SB = StrStart + StrEnd;

                                La.setText(SB);

                        }

                }



                public void keyReleased(KeyEvent e)

                {



                }



                public void keyTyped(KeyEvent e)

                {



                }

        }

}



public class M4

{



        public static void main(String[] args)

        {

                new Frame4();

        }



}

 

5. JLabel 컴포넌트는 key 이벤트를 받을 수 있다. Jlabel 컴포넌트를 이용하여 “Love java”를 출력하고 + 키를 치면 폰트 크기를 5픽셀씩 키우고, - 키를 치면 폰트 크기를 5픽셀씩 줄이는 스윙응용프로그램을 작성하라. 5픽섹 이하로 작아지지 않도록 하라.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



class Frame5 extends JFrame

        {

        JPanel P;

        JLabel La;

        String SB = "Love Java";



        Frame5()

        {

                this.setTitle("문자열 바꾸기");

                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                P = new JPanel();

                La = new JLabel(SB);

                La.setFont(new Font("Arial", Font.PLAIN, 10));

                Key K = new Key();

                La.addKeyListener(K);

                P.add(La);

                this.add(P);

                this.setVisible(true);

                this.setSize(300, 300);

                La.requestFocus();

        }



        class Key implements KeyListener

        {



                public void keyPressed(KeyEvent e)

                {



                  if(e.getKeyChar() == '+')

                  {

                   La.setFont(new Font("Arial", Font.PLAIN, La.getFont().getSize()+5));

                  }



                  if(e.getKeyChar() == '-' && La.getFont().getSize() > 5)

                  {

                   La.setFont(new Font("Arial", Font.PLAIN, La.getFont().getSize()-5));

                  }

                }



                public void keyReleased(KeyEvent e)

                {



                }



                public void keyTyped(KeyEvent e)

                {



                }

        }

}



public class M5

{



        public static void main(String[] args)

        {

                new Frame5();

        }



}

 

6. 클릭 연습용 스윙 응용프로그램을 작성하라. JLabel을 이용하여 문자열이 “C”인 레이블을 하나 만들고 초기 위치를 (100,100)으로 하라. 문자열을 클릭할 때 마다 레이블은 프레임 내의 랜덤한 위치로 움직인다.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



class Frame6 extends JFrame

{

        JPanel P;

        JLabel La;



        Frame6()

        {

                this.setTitle("마우스 올리기");

                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                P = new JPanel();

                P.setLayout(null);

                La = new JLabel("C");

                La.setSize(20, 20);

                La.setLocation(100, 100);

                Mouse Mo = new Mouse();

                La.addMouseListener(Mo);

                P.add(La);

                this.add(P);

                this.setVisible(true);

                this.setSize(350, 350);

        }



        class Mouse implements MouseListener

        {



                public void mouseClicked(MouseEvent e)

                {

                        JLabel La = (JLabel)e.getSource();

                        int X = (int)(Math.random()*300);

                        int Y = (int)(Math.random()*300);

                        La.setLocation(X, Y);

                }



                public void mouseEntered(MouseEvent e)

                {



                }



                public void mouseExited(MouseEvent e)

                {



                }



                public void mousePressed(MouseEvent e)

                {



                }



                public void mouseReleased(MouseEvent e)

                {



                }



        }

}





public class M6

{



        public static void main(String[] args)

        {

                new Frame6();

        }



}


7. Jlabel을 활용하여 “Love Java”를 출력하고, “Love Java” 글자 위에 마우스를 올려 마우스 휠을 위로 굴리면 글자가 작아지고, 아래로 굴리면 글자가 커지도록 프로그램을 작성하라. 폰트 크기는 한번에 5픽셀씩 작아지거나 커지도록 하고, 5픽셀 이하로 작아지지 않도록 하라.


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



class Frame7 extends JFrame

{

        JPanel P;

        JLabel La;



        Frame7()

        {

                this.setTitle("휠 굴리기");

                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



                P = new JPanel();

                La = new JLabel("Love java");

                La.setFont(new Font("Arial", Font.PLAIN, 10));



                Mouse Mo = new Mouse();

                La.addMouseWheelListener(Mo);



                P.add(La);

                this.add(P);

                this.setVisible(true);

                this.setSize(300, 300);

        }











        class Mouse implements MouseWheelListener

        {

                public void mouseWheelMoved(MouseWheelEvent e)

                {

                        int n = e.getWheelRotation();



                        if( n > 0 && La.getFont().getSize() >5)

                        {

        La.setFont(new Font("Arial", Font.PLAIN, La.getFont().getSize()-5));

                        }



                        else

                        {

        La.setFont(new Font("Arial", Font.PLAIN, La.getFont().getSize()+5));

                        }



                }



        }

}



public class M7

{



        public static void main(String[] args)

        {

                new Frame7();

        }



}

 

 
 
 
 
728x90
그리드형

댓글