• 명품 자바 프로그래밍(개정4판) 제 10장 실습문제

    2020. 12. 7.

    by. Hyeon-Uk

    반응형

    [1번]

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("마우스 올리기 내리기");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		JLabel j=new JLabel("Love Java");
    		j.addMouseListener(new MouseAdapter() {
    			public void mouseExited(MouseEvent e) {
    				// TODO Auto-generated method stub
    				j.setText("사랑해");
    			}
    			public void mouseEntered(MouseEvent e) {
    				// TODO Auto-generated method stub
    				j.setText("Love Java");
    			}
    		});
    		c.add(j);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1001 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [2번]

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    
    import javax.swing.JFrame;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("드래깅동안 YELLOW");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		c.setBackground(Color.GREEN);
    		
    		c.addMouseListener(new mouseListener());
    		c.addMouseMotionListener(new motionListener());
    		setSize(300,300);
    		setVisible(true);
    	}
    	
    	public class motionListener extends MouseMotionAdapter{
    		public void mouseDragged(MouseEvent e) {
    			Container c=(Container)e.getSource();
    			c.setBackground(Color.yellow);
    		}
    	}
    	
    	public class mouseListener extends MouseAdapter{
    		public void mouseReleased(MouseEvent e) {
    			Container c=(Container)e.getSource();
    			c.setBackground(Color.GREEN);
    		}
    	}
    }
    
    public class p1002 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [3번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("Left키로 문자열 교체");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		JLabel j=new JLabel("Love Java");
    		c.add(j);
    		c.setFocusable(true);
    		c.requestFocus();
    		c.addKeyListener(new KeyAdapter() {
    			public void keyPressed(KeyEvent e) {
    				// TODO Auto-generated method stub
    				if(e.getKeyCode()==KeyEvent.VK_LEFT) {
    					String text=j.getText();
    					if(text.equals("Love Java")) {
    						j.setText("avaJ evoL");
    					}
    					else {
    						j.setText("Love Java");
    					}
    				}
    			}
    		});
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1003 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [4번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("Left 키로 문자열 이동");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		JLabel j=new JLabel("Love Java");
    		c.add(j);
    		c.setFocusable(true);
    		c.requestFocus();
    		c.addKeyListener(new KeyAdapter() {
    			public void keyPressed(KeyEvent e) {
    				// TODO Auto-generated method stub
    				if(e.getKeyCode()==KeyEvent.VK_LEFT) {
    					String text=j.getText();
    					j.setText(text.substring(1)+text.charAt(0));
    				}
    			}
    		});
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1004 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [5번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("+,- 키로 폰트 크기 조절");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		JLabel j=new JLabel("Love Java");
    		j.setFont(new Font("Arial",Font.PLAIN,10));
    		c.add(j);
    		c.setFocusable(true);
    		c.requestFocus();
    		c.addKeyListener(new KeyAdapter() {
    			public void keyPressed(KeyEvent e) {
    				// TODO Auto-generated method stub
    				Font temp=j.getFont();
    				int size=temp.getSize();
    				if(e.getKeyCode()==KeyEvent.VK_ADD||e.getKeyCode()==KeyEvent.VK_EQUALS) {
    					j.setFont(new Font("Arial",Font.PLAIN,size+5));					
    				}
    				if(e.getKeyCode()==KeyEvent.VK_MINUS||e.getKeyCode()==KeyEvent.VK_SUBTRACT) {
    					if(size-5>0) {
    						j.setFont(new Font("Arial",Font.PLAIN,size-5));
    					}
    				}
    			}
    		});
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1005 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    반응형

    [6번]

    import java.awt.Container;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("클릭 연습 용 응용프로그램");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(null);
    		
    		JLabel j=new JLabel("c");
    		j.setBounds(100,100,10,10);
    		j.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {
    				int x=(int)(Math.random()*c.getWidth());
    				int y=(int)(Math.random()*c.getHeight());
    				j.setLocation(x,y);
    			}
    		});
    		
    		c.add(j);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1006 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    

    [7번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class MyFrame extends JFrame{
    	public MyFrame() {
    		setTitle("마우스 휠을 굴려 폰트 크기 조절");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		JLabel j=new JLabel("Love Java");
    		j.addMouseWheelListener(new MouseWheelListener() {
    			public void mouseWheelMoved(MouseWheelEvent e) {
    				// TODO Auto-generated method stub
    				int n=e.getWheelRotation();
    				Font f=j.getFont();
    				int size=f.getSize();
    				if(n<0) {
    					if(size-5>0) {
    						j.setFont(new Font("Arial",Font.PLAIN,size-5));
    					}
    				}
    				else {
    					j.setFont(new Font("Arial",Font.PLAIN,size+5));
    				}
    			}
    		});
    		
    		c.add(j);
    		
    		setSize(300,300);
    		setVisible(true);
    	}
    }
    
    public class p1007 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MyFrame();
    	}
    }
    
    
    반응형

    댓글