명품 JAVA 프로그래밍

명품 JAVA 프로그래밍 10장 실습문제 2

anycoding 2021. 12. 13. 18:04
반응형

문제

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

 

소스 코드

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

public class Main2 extends JFrame{
	public Main2() {
		setTitle("드래깅동안 YELLOW...");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		Panel p = new Panel();
		p.setBackground(Color.green);
		
		p.addMouseMotionListener(new MouseAdapter() {
			public void mouseDragged(MouseEvent e) {
				p.setBackground(Color.yellow);
			}
		});
		p.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
				p.setBackground(Color.green);
			}
		});
		c.add(p);
		
		setSize(300,200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Main2();
		
	}

}

 

반응형