import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.sun.xml.internal.ws.api.server.Container;
public class Tossakan extends JFrame implements ActionListener{
int index = 0;
ImageIcon pic1 = new ImageIcon("beber.jpg");
ImageIcon pic2 = new ImageIcon("kaka.jpg");
ImageIcon q[] = {pic1,pic2};
String answer[] = {"Beber","Gaga"};
JTextField box = new JTextField(20);
java.awt.Container con = getContentPane();
JLabel showPic;
Tossakan(){
setLayout(null);
setBounds(0, 0, 600, 500);
setVisible(true);
Random ran = new Random();
index = ran.nextInt(2);
showPic = new JLabel(q[index]);
con.add(showPic);
con.add(box);
showPic.setBounds(10,100,256,256);
box.setBounds(300,100,300,250);
box.addActionListener(this);
}
public static void main(String[] args) {
new Tossakan();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==box){
if(box.getText().toLowerCase().equals(answer[index].toLowerCase())){
JOptionPane.showMessageDialog(null, "ถูกต้องนะคร๊าาาบบบบ");
con.remove(showPic);
repaint();
Random ran = new Random();
index = ran.nextInt(2);
showPic = new JLabel(q[index]);
con.add(showPic);
showPic.setBounds(10,100,256,256);
}
else{
box.setText("");
}
}
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น