지난 시간 복습
Thread : 프로세스 속의 작은 프로세스
여러 개의 활동을 한번에 할 수 있게 한다.
Thread t = new Thread(); // thread 생성;
t.start(); // thread 시작;
thread를 생성하고 동작시킴.
thread를 생성하는 방법
1. thread 클래스를 상속하는 방법
2. runnable 인터페이스를 구현하는 방법
클래스는 단일상속, 인터페이스는 다중상속이기 때문에 이미 상속받고 있는 클래스가 있다면 2, 아니면 1의 방법을 사용하는 등으로 응용할 수 있다.
thread는 무작위 순서로 진행이 된다.
한 번에 하나의 스레드 만이 공유 데이터에 접근할 수 있도록 제어하는 것이 필요.
radiobutton 과 checkbox의 차이점 : radiobutton 은 다중선택이 불가능하다는 것인데, 아직은 동시선택이 되므로 group 을 이용해서 바꿔주어야 한다.
radiobutton 자체가 다중선택이 아닌 다지선다 중 1 택의 형태를 띄는 성격을 가진다. (는 의미)
buttongroup 을 통해 묶어주니 동시선택이 안된다.
package com.tech.gt001;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
public class RadioButtonEx2 extends JFrame implements ActionListener{
private JRadioButton small,medium,large;
private JLabel text;
private JPanel topPanel,sizePanel,resultPanel,btnPanel,cPanel;
private JButton btn1,btn2;
private ButtonGroup size;
public RadioButtonEx2() {
setTitle("커피주문");
setSize(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
topPanel=new JPanel();
JLabel label=new JLabel("어떤 커피를 주문하시겠습니까?");
topPanel.add(label);
add(topPanel,BorderLayout.NORTH);
sizePanel=new JPanel();
small=new JRadioButton("smallsize");
medium=new JRadioButton("mediumsize");
large=new JRadioButton("largesize");
// 보더적용
Border border=BorderFactory.createBevelBorder(NORMAL);
sizePanel.setBorder(border);
sizePanel.setSize(300,200);
size=new ButtonGroup();
size.add(small);
size.add(medium);
size.add(large);
// 라디오 버튼에 수신기 부착
small.addActionListener(this);
medium.addActionListener(this);
large.addActionListener(this);
// 버튼 구성
btn1=new JButton("계산");
btn2=new JButton("취소");
btnPanel=new JPanel();
btnPanel.add(btn1);
btnPanel.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
cPanel=new JPanel();
cPanel.add(btnPanel,BorderLayout.NORTH);
cPanel.add(sizePanel,BorderLayout.CENTER);
sizePanel.add(small);
sizePanel.add(medium);
sizePanel.add(large);
add(cPanel,BorderLayout.CENTER);
resultPanel=new JPanel();
text=new JLabel("크기를 선택하지 않았습니다.");
text.setForeground(Color.red);
resultPanel.add(text);
add(resultPanel,BorderLayout.SOUTH);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("신호신호");
}
public static void main(String[] args) {
new RadioButtonEx2();
}
}
결과
clearSelection() method 를 사용한다.
며칠 후에 kiosk 개념의 semi-project 를 할 예정.
저번 계산기 미션
다른 수강생의 코드
package com.tech.mission;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorEx {
JFrame f = new JFrame();
public CalculatorEx() {
f.setTitle("계산기");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(620, 470);
JPanel p1 = new JPanel();
JPanel p = new JPanel();
f.setLayout(null);
p1.setLayout(new GridLayout(1, 1));
p.setLayout(new GridLayout(5, 5));
p1.setSize(600, 50);
p.setBounds(0, 50, 610, 380);
p1.setBounds(0, 0, 610, 50);
JTextField t = new JTextField(10);
JButton b1 = new JButton("backspace");
JButton b2 = new JButton("");
JButton b2_1 = new JButton("");
JButton b3 = new JButton("CE");
JButton b4 = new JButton("C");
JButton b5 = new JButton("7");
JButton b6 = new JButton("8");
JButton b7 = new JButton("9");
JButton b8 = new JButton("sqrt");
JButton b9 = new JButton("/");
JButton b10 = new JButton("4");
JButton b11 = new JButton("5");
JButton b12 = new JButton("6");
JButton b13 = new JButton("X");
JButton b14 = new JButton("%");
JButton b15 = new JButton("1");
JButton b16 = new JButton("2");
JButton b17 = new JButton("3");
JButton b18 = new JButton("-");
JButton b19 = new JButton("1/x");
JButton b20 = new JButton("0");
JButton b21 = new JButton("+/-");
JButton b22 = new JButton(".");
JButton b23 = new JButton("+/");
JButton b24 = new JButton("=");
p1.add(t);
p.add(b1);
p.add(b2);
p.add(b2_1);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
p.add(b17);
p.add(b18);
p.add(b19);
p.add(b20);
p.add(b21);
p.add(b22);
p.add(b23);
p.add(b24);
f.add(p1);
f.add(p);
f.setVisible(true);
}
public static void main(String[] args) {
new CalculatorEx();
}
}
결과
강사님의 코드
package com.tech.mission;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorEx3 extends JFrame{
// 배열과 반복문 사용
private JPanel panel;
private JTextField tField;
private JButton[] buttons;
private String[] labels= {
"Backspace", "", "", "CE", "C",
"7", "8", "9", "/", "sqrt",
"4", "5", "6", "x", "%",
"1", "2", "3", "-","1/x",
"0", "+/-", ".", "+", "="
};
public CalculatorEx3() {
setTitle("계산기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 250);
tField=new JTextField(35);
tField.setEnabled(false);;
panel=new JPanel();
tField.setText("0.");
panel.setLayout(new GridLayout(0,5,3,3));
buttons=new JButton[25];
int index=0;
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
buttons[index]=new JButton(labels[index]);
if(col>=3) {
buttons[index].setForeground(Color.red);
}else {
buttons[index].setForeground(Color.blue);
}
buttons[index].setBackground(Color.cyan);
panel.add(buttons[index]);
index++;
}
}
// 판넬을 프레임에 부착
add(tField,"North");
add(panel,"Center");
setVisible(true);
}
public static void main(String[] args) {
new CalculatorEx3();
}
}
결과
카운트다운
package com.tech.gt003;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CountDownEx extends JFrame{
// 내부 클래스를 활용한 thread 사용
private JLabel label;
class MyThread extends Thread {
@Override
public void run() {
for (int i = 10; i >= 0; i--) {
// sleep 기능 활용
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
label.setText(i+"");
}
}
}
public CountDownEx() {
setTitle("카운트다운");
setSize(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
label=new JLabel("Start");
label.setFont(new Font("Impact", Font.BOLD, 100));
add(label);
// thread 동작
new MyThread().start();
setVisible(true);
}
public static void main(String[] args) {
new CountDownEx();
}
}
간단한 자동차 경주
package com.tech.gt003;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CarRace extends JFrame{
class MyCarThread extends Thread{
private JLabel label;
private int x,y; // 좌표값 사용
public MyCarThread(String fname, int x, int y) {
this.x=x;
this.y=y;
label=new JLabel();
label.setIcon(new ImageIcon(fname));
label.setBounds(x,y,50,50);
add(label);
}
@Override
public void run() {
System.out.println("신호"+x+":"+y);
// 좌표값 변경
for (int i = 0; i < 200; i++) {
x+=10*(Math.random());
label.setBounds(x,y,50,50);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}
public CarRace() {
setTitle("간단한 자동차 경주");
setSize(600,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
new MyCarThread("car.png",100,0).start();
new MyCarThread("carblue.png",100,50).start();
new MyCarThread("caryell.png",100,100).start();
setVisible(true);
}
public static void main(String[] args) {
new CarRace();
}
}
결과
ComboBox
package com.tech.gt003;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ComboBoxEx extends JFrame implements ActionListener{
private JComboBox animalList;
private JLabel label;
public ComboBoxEx() {
setTitle("ComboBox");
setSize(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
String[] animals={"lion","monkey","tiger"};
animalList=new JComboBox(animals);
animalList.addActionListener(this);
label=new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
changePicture(animals[animalList.getSelectedIndex()]);
add(animalList,"North");
add(label,"South");
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("신호");
System.out.println("index:"+animalList.getSelectedIndex());
JComboBox jb=(JComboBox) e.getSource();
System.out.println("jb:"+jb.getSelectedItem());
String name=(String)jb.getSelectedItem();
changePicture(name);
}
private void changePicture(String name) {
ImageIcon icon=new ImageIcon(name+".png");
label.setIcon(icon);
}
public static void main(String[] args) {
new ComboBoxEx();
}
}
결과