상세 컨텐츠

본문 제목

자바 awt를 이용하여 입력값을 랜덤으로 출력해주는 프로그램

관리X 과거글

by 까먹기전에 2015. 1. 9. 12:42

본문

반응형














밥고민하지말자.zip



다운 받으신뒤 압축 풀고


원하는 메뉴를 쓰고 넣어를 누르신뒤에


돌려를 누르시면 랜덤으로 튀어나옵니다!





소스

package dark;


import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Color;

import java.awt.Component;

import java.awt.Dialog;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.Label;

import java.awt.List;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

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, b4;

TextField t1, t2;

List l1;


Myframe(String a) {

super(a);

b1 = new Button("돌려!");

b2 = new Button("넣어!");

b3 = new Button("초기화");

t1 = new TextField(20);

t2 = new TextField(10);

t2.setEnabled(false);

l1 = new List();

b4 = new Button("이건빼자");

}


void addComponent() {

add(t1);

add(b2);

add(b1);

add(t2);

add(b3);

add(l1);

add(b4);

}


void registerEvent() {

Test test = new Test();

b1.addActionListener(test);

b2.addActionListener(test);

this.addWindowListener(test);

b3.addActionListener(test);

b4.addActionListener(test);

}

}


class Test extends WindowAdapter implements ActionListener {


Dialog info;

Random r = new Random();

Button b1, b2, b3, b4 ,b5;// b1=돌려 ,b4=이건빼자

// b2=넣어

TextField t1, t2;// t1-값받는곳

// t2-값넣는곳

int i = 0;

List l1;


@Override

public void windowClosing(WindowEvent e) {

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();

info = new Dialog(f);

t1 = (TextField) cp[0];

b2 = (Button) cp[1];

b1 = (Button) cp[2];

t2 = (TextField) cp[3];

b3 = (Button) cp[4];

l1 = (List) cp[5];

b4 = (Button) cp[6];

Label msg;

Label msg2;

Label msg3;

Button ok = new Button("죄송합니당..");


if (e.getActionCommand() == "넣어!") {

if (t1.getText().trim().equals("")) {

info.setSize(200, 100);

info.setLocation(f.getLocation());

info.setLayout(new BorderLayout());

info.setVisible(true);

msg2 = new Label("뭘 쓰고 버튼 눌리리라고!!");

info.add(msg2, "Center");

info.add(ok, "South");

} else {

b.add(t1.getText());

l1.add(t1.getText().trim());

t1.setText("");

t1.requestFocus();

}


} else if (e.getActionCommand() == "돌려!") {

try {

k = (int) (Math.random() * b.size());

t2.setText(b.get(k).trim());

} catch (IndexOutOfBoundsException zzz) {

info.setSize(200, 100);

info.setLocation(f.getLocation());

info.setLayout(new BorderLayout());

info.setVisible(true);

msg3 = new Label("넣은값이 있으야 돌리지 않것어?? 앙??");

info.add(msg3, "Center");

info.add(ok, "South");

}

}


if (e.getActionCommand() == "이건빼자") {

try {

String a = l1.getSelectedItem();

l1.remove(a);

l1.select(0);

} catch (IllegalArgumentException eeeeeee) {

info.setSize(200, 100);

info.setLocation(f.getLocation());

info.setLayout(new BorderLayout());

info.setVisible(true);

msg = new Label("안선택이거나 값이 없어요!!!");

info.add(msg, "Center");

info.add(ok, "South");


}


}


if (e.getActionCommand() == "초기화") {

l1.removeAll();

b.removeAll(b);

t2.setText("");

t1.setText(" ");

t1.requestFocus();

}


if (e.getSource() == b4 || e.getSource() == b2 || e.getSource() == b1) {

ok.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

info.setVisible(false);

info.dispose();

if (e.getSource() == ok) {

t1.requestFocus();

}

}


});


}


}


}


public class tes {

public static void main(String[] args) {

Myframe f = new Myframe("밥고민하지마세요");

f.setVisible(true);

f.setBounds(600, 600, 500, 300);

f.setLayout(new FlowLayout(FlowLayout.LEFT,0,10));

f.addComponent();

f.registerEvent();

f.t1.requestFocus();

f.setEnabled(true);


}

}

















관련글 더보기