명품자바 programming 실습문제 10장
2020. 12. 4. 14:16ㆍProgramming Language/JAVA
2.
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class Ex2 extends JFrame {
public Ex2() {
super("드래깅 동안 Yellow");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.setBackground(Color.GREEN);
c.addMouseListener(new MyMouse());
c.addMouseMotionListener(new MyMouse());
setSize(300,300);
setVisible(true);
}
class MyMouse extends MouseAdapter {
public void mouseDragged(MouseEvent e) {
Component co = (Component)e.getSource();
co.setBackground(Color.YELLOW);
}
public void mouseReleased(MouseEvent e) {
Component co = (Component)e.getSource();
co.setBackground(Color.GREEN);
}
}
public static void main(String[] args) {
new Ex2();
}
}
3.
import java.awt.Component;
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;
public class Ex3 extends JFrame {
JLabel label = null;
public Ex3() {
super("Left 키로 문자열 교체");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
label = new JLabel("Love Java");
c.add(label);
c.addKeyListener(new MyKey());
c.setFocusable(true);
c.requestFocus();
setSize(400,200);
setVisible(true);
}
class MyKey extends KeyAdapter{
public void keyPressed(KeyEvent e) {
Component c = (Component)e.getSource();
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
StringBuffer sb = new StringBuffer(label.getText());
sb = sb.reverse();
label.setText(sb.toString());
c.setFocusable(true);
c.requestFocus();
}
}
}
public static void main(String[] args) {
new Ex3();
}
}
4.
import java.awt.Component;
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;
public class Ex3 extends JFrame {
JLabel label = null;
public Ex3() {
super("Left 키로 문자열 교체");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
label = new JLabel("Love Java");
c.add(label);
c.addKeyListener(new MyKey());
c.setFocusable(true);
c.requestFocus();
setSize(400,200);
setVisible(true);
}
class MyKey extends KeyAdapter{
public void keyPressed(KeyEvent e) {
Component c = (Component)e.getSource();
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
StringBuffer sb = new StringBuffer(label.getText());
char temp;
temp = sb.charAt(0);
sb.append(temp);
sb.delete(0, 1);
label.setText(sb.toString());
c.setFocusable(true);
c.requestFocus();
}
}
}
public static void main(String[] args) {
new Ex3();
}
}
5. +누르면 안되는 이유 아직 해결 x
import java.awt.Component;
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;
public class Ex5 extends JFrame {
JLabel label = null;
public Ex5() {
super("+,- 키로 폰트 크기 조절");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
label = new JLabel("Love Java");
c.add(label);
c.addKeyListener(new MyKey());
c.setFocusable(true);
c.requestFocus();
setSize(400,200);
setVisible(true);
}
public class MyKey extends KeyAdapter {
public void keyPressed(KeyEvent e) {
Component c = (Component)e.getSource();
Font f = label.getFont();
int size = f.getSize();
if (e.getKeyCode() == KeyEvent.VK_PLUS) {
size += 5;
}
if (e.getKeyCode() == KeyEvent.VK_MINUS){
size -= 5;
if (size < 5) size += 5;
}
label.setFont(new Font("Arial", Font.PLAIN, (int)size));
c.setFocusable(true);
c.requestFocus();
}
}
public static void main(String[] args) {
new Ex5();
}
}
반응형
'Programming Language > JAVA' 카테고리의 다른 글
명품자바 programming 13장 실습문제 (0) | 2020.12.08 |
---|---|
명품자바 programming 11장 실습문제 (0) | 2020.12.04 |
명품자바 programming 실습문제 9장 (0) | 2020.12.04 |
\n과 \r (Escape String) (0) | 2020.12.02 |