1번
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
class Chapter10 extends JFrame{
Chapter10(){
setTitle("마우스 올리기 내리기");
setDefaultCloseOperation(3);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel label = new JLabel("Love Java");
label.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e){
JLabel temp = (JLabel) e.getSource();
temp.setText("사랑해");
}
public void mouseExited(MouseEvent e){
JLabel temp = (JLabel) e.getSource();
temp.setText("Love Java");
}
});
c.add(label);
setSize(300, 200);
setVisible(true);
}
public static void main(String args[]){
new Chapter10();
}
}
2번
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
class Chapter10 extends JFrame{
Chapter10(){
setTitle("드래깅동안 Yellow");
setDefaultCloseOperation(3);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.setBackground(Color.GREEN);
c.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e){
Container temp = (Container) e.getSource();
temp.setBackground(Color.YELLOW);
}
public void mouseReleased(MouseEvent e){
Container temp = (Container) e.getSource();
temp.setBackground(Color.GREEN);
}
});
setSize(300, 200);
setVisible(true);
}
public static void main(String args[]){
new Chapter10();
}
}
3번
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
class Chapter10 extends JFrame{
Chapter10(){
setTitle("Left 키로 문자열 교체");
setDefaultCloseOperation(3);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel label = new JLabel("Love Java");
c.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT){
String text = label.getText();
if(text.equals("Love Java")){
label.setText("avaJ evoL");
}else{
label.setText("Love Java");
}
}
}
});
c.add(label);
c.setFocusable(true);
c.requestFocus();
setSize(300, 200);
setVisible(true);
}
public static void main(String args[]){
new Chapter10();
}
}
4번
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
class Chapter10 extends JFrame{
Chapter10(){
setTitle("Left 키로 문자열 교체");
setDefaultCloseOperation(3);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel label = new JLabel("Love Java");
c.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT){
String text = label.getText();
label.setText(text.substring(1) + text.substring(0, 1));
}
}
});
c.add(label);
c.setFocusable(true);
c.requestFocus();
setSize(300, 200);
setVisible(true);
}
public static void main(String args[]){
new Chapter10();
}
}
5번
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
class Chapter10 extends JFrame{
int size = 10;
Chapter10(){
setTitle("+,-로 폰트 크기 조절");
setDefaultCloseOperation(3);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel label = new JLabel("Love Java");
label.setFont(new Font("Arial", Font.PLAIN, size));
c.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ADD || e.getKeyCode()==KeyEvent.VK_EQUALS){
size += 5;
label.setFont(new Font("Arial", Font.PLAIN, size));
}
else if(e.getKeyCode()==KeyEvent.VK_MINUS) {
size -= 5;
label.setFont(new Font("Arial", Font.PLAIN, size));
}
}
});
c.add(label);
c.setFocusable(true);
c.requestFocus();
setSize(300, 200);
setVisible(true);
}
public static void main(String args[]){
new Chapter10();
}
}
6번
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
class Chapter10 extends JFrame{
Chapter10(){
setTitle("클릭 연습용 프로그램");
setDefaultCloseOperation(3);
Container c = getContentPane();
c.setLayout(null);
JLabel label = new JLabel("c");
label.setSize(10, 10);
label.setLocation(100, 100);
label.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int x = (int)(Math.random() * 400);
int y = (int)(Math.random() * 500);
label.setLocation(x, y);
}
});
c.add(label);
setSize(500, 600);
setVisible(true);
}
public static void main(String args[]){
new Chapter10();
}
}
7번
import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;
class Chapter10 extends JFrame{
int size = 10;
Chapter10(){
setTitle("마우스 휠을 굴려 폰트 크기 조절");
setDefaultCloseOperation(3);
Container c = getContentPane();
c.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel label = new JLabel("Love Java");
label.setFont(new Font("Arial", Font.PLAIN, size));
label.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
int n = e.getWheelRotation();
if(n < 0){
if(size - 5 > 0){
size -= 5;
label.setFont(new Font("Arial", Font.PLAIN, size ));
}
}
else{
size += 5;
label.setFont(new Font("Arial", Font.PLAIN, size + 5));
}
}
});
c.add(label);
setSize(500, 600);
setVisible(true);
}
public static void main(String args[]){
new Chapter10();
}
}
'대학교 수업 > Java 프로그래밍' 카테고리의 다른 글
[Java] 명품 자바 프로그래밍 13장 실습문제 (1~8) (0) | 2022.12.05 |
---|---|
[Java] 명품 자바 프로그래밍 12장 실습문제 (0) | 2022.11.22 |
[Java] 명품 자바 프로그래밍 10장 Open Challenge (0) | 2022.10.11 |
[Java] 명품 자바 프로그래밍 9장 Open Challenge (0) | 2022.10.11 |
[JAVA] 명품 자바 프로그래밍 11장 실습문제 (0) | 2022.10.05 |