관리 메뉴

IT창고

자바(JAVA) 실습 - 단어장 본문

프로그래밍/JAVA

자바(JAVA) 실습 - 단어장

방구석여포 2017. 9. 23. 18:35


조건

1. 직렬화, 쓰레드 기능을 활용하여 만들기

2. 단어입력 기능구현

3. 영어단어를 보고 한글을 입력해서 정답체크기능 구현

4. GUI형식으로 만들기


소스일부분

------메뉴------

public class Menu extends Frame {

Control con;

public Menu(Control con) {

this.con = con;

setSize(600, 200);

setResizable(false);

setLocation(200, 200);

setLayout(null);


init();

addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

Button vocaListBt;

Button vocaGameBt;

private void init(){

vocaListBt = new Button("단어장 수정");

vocaListBt.setSize(200, 200);

vocaListBt.setLocation(0, 0);

add(vocaListBt);

vocaGameBt = new Button("단어게임");

vocaGameBt.setSize(200, 200);

vocaGameBt.setLocation(200, 0);

add(vocaGameBt);

vocaListBt.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

con.showVoca();

}

});

vocaGameBt.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

con.showQuiz();

}

});

}


}

-------쓰레드---------

public class CountThread extends Thread {

int time;

boolean isDone = false;

Label timeLb;

public void setLabel( Label timeLb){

this.timeLb = timeLb; 

}

public void setIsDone(boolean isDone){

this.isDone = isDone;

}

@Override

public void run() {

while(true){

if(timeLb == null){

continue;

}

 

timeLb.setText("진행시간: "+time +"초");

 

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

time++;

 

if(isDone){

break;

}

}

}

}

--------단어게임----------

public class Quiz extends Frame {

Control con;


public Quiz(Control con) {

this.con = con;

setSize(500, 500);

setResizable(false);

setLocation(300, 100);

setLayout(null);


init();


addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}


int idx = 0;

int score = 0;

String answer = "";

Label engLb;

Label scoreLb;

Label timeLb;

TextField korTf;

CountThread th;


private void init() {

th = new CountThread();

Button homeBt = new Button("메뉴");

homeBt.setSize(50, 20);

homeBt.setLocation(5, 30);

add(homeBt);


homeBt.addActionListener(new ActionListener() {


@Override

public void actionPerformed(ActionEvent e) {

con.showMenu();

}

});


timeLb = new Label("진행시간");

timeLb.setSize(100, 50);

timeLb.setLocation(150, 50);

add(timeLb);

th.setLabel(timeLb);

th.start();

engLb = new Label("영어");

engLb.setSize(100, 50);

engLb.setLocation(100, 200);

add(engLb);


scoreLb = new Label("점수");

scoreLb.setSize(50, 50);

scoreLb.setLocation(400, 50);

add(scoreLb);


korTf = new TextField();

korTf.setSize(200, 30);

korTf.setLocation(200, 200);

add(korTf);


Button confirmBt = new Button("확인");

confirmBt.setSize(50, 20);

confirmBt.setLocation(300, 300);

add(confirmBt);


confirmBt.addActionListener(new ActionListener() {


@Override

public void actionPerformed(ActionEvent e) {

String myAnswer = korTf.getText();


if (answer.equals(myAnswer)) {

score += 10;

} else {

score -= 10;

}

korTf.setText("");

idx++;

if (idx == Storage.vocaArr.size()) {

th.setIsDone(true);

engLb.setText("문제 없다");

} else {

showQuestion();

}


}

});


reset();

showQuestion();


}


private void showQuestion() {

VocaData curVoca = Storage.vocaArr.get(ranIdxArr.get(idx));

engLb.setText(curVoca.eng);

answer = curVoca.kor;

scoreLb.setText("점수: " + score);

}


ArrayList<Integer> ranIdxArr = new ArrayList<>();


private void setRan() {

ranIdxArr.clear();

ArrayList<Integer> temp = new ArrayList<>();

for (int i = 0; i < Storage.vocaArr.size(); i++) {

temp.add(i);

}

Random rd = new Random();

while (true) {

if (temp.size() == 0) {

break;

}

int ranNum = rd.nextInt(temp.size());

ranIdxArr.add(temp.get(ranNum));

temp.remove(ranNum);

}

}


public void reset() {

setRan();

idx = 0;

score = 0;

showQuestion();

}


}

결과화면


Comments