상세 컨텐츠

본문 제목

자바 awt를 이용해 음식 메뉴 랜덤선택하기

관리X 과거글

by 까먹기전에 2015. 1. 7. 19:21

본문

반응형










다운 !


선택장애를위한밥고민하지마.exe




사용법
원하는 음식을 넣고    넣어!
다 넣으시면 돌려! 를 누르시면 한개가 나옵니다.







import java.awt.Button;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.List;
import java.awt.TextField;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;

class Myframe extends Frame {
Button b1, b2,b3;
TextField t1, t2;
List l1;

Myframe(String a) {
super(a);
b1 = new Button("돌려!");
b2 = new Button("넣어!");
b3 = new Button("초기화");
t1 = new TextField(15);
t2 = new TextField(10);
l1 = new List();
}

void addComponent() {
add(t1);
add(b2);
add(b1);
add(t2);
add(b3);
add(l1);

}

void registerEvent() {
Test test = new Test();
b1.addActionListener(test);
b2.addActionListener(test);
this.addWindowListener(test);
b3.addActionListener(test);
}
}

class Test extends WindowAdapter implements ActionListener {
Random r = new Random();
Button b1, b2,b3;// b1=돌려
// b2=넣어
TextField t1, t2;// t1-값받는곳
// t2-값넣는곳
static int i = 0;
List l1;
@Override
public void windowClosing(WindowEvent e) {
Window z = e.getWindow();
System.exit(0);
}
 ArrayList<String> b = new ArrayList<String>();
int k;
public void actionPerformed(ActionEvent e) {
Button b1 = (Button) e.getSource();
Frame f = (Frame) b1.getParent();
Component[] cp = f.getComponents();
t1 = (TextField) cp[0];
b2 = (Button) cp[1];
b1 = (Button) cp[2];
t2 = (TextField) cp[3];
b3 = (Button) cp[4];
l1 = (List) cp[5];
try {
if (e.getActionCommand() == "넣어!"){
b.add(t1.getText());
l1.add(t1.getText());
t1.setText("");
}
else if (e.getActionCommand() == "돌려!") {
k=(int)(Math.random() * b.size());
t2.setText(b.get(k));
}
} catch (Exception ee) {
t2.setText("아무값도 없써용!!");
}
if(e.getActionCommand() == "초기화"){
l1.removeAll();
b.removeAll(b);
t2.setText("");
}
}
}

class Table {
public static void main(String[] args) {
Myframe f = new Myframe("밥고민하지마세요");
f.setVisible(true);
f.setBounds(600, 600, 500, 300);
f.setLayout(new FlowLayout());
f.addComponent();
f.registerEvent();

}
}









관련글 더보기