Untitled

 avatar
unknown
plain_text
3 years ago
2.6 kB
3
Indexable
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.JTextArea;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MyFrame extends JFrame implements ActionListener {
	
	Integer counter = new Integer(0);
    Integer diamonds = new Integer(0);
    Integer boom = new Integer(0);
	
	MyFrame () {
		System.out.println("Hi");
		
		this.setLayout(new GridLayout(2,2,10,10));
		
	
		
		JButton button = new JButton("Kliknij tutaj");
		JButton button2 = new JButton("A może tutaj?!");
		
		JTextArea textArea = new JTextArea(5, 20);
		JTextArea textArea2 = new JTextArea(5, 20);
	
		this.add(button);
		this.add(button2);
		
		textArea.setLineWrap(true);
		textArea2.setLineWrap(true);
		
		this.add(textArea);
		this.add(textArea2);
		
		
//		button.addActionListener(this);
		
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				counter += 1;
				boolean isWin = getRandomNumberHandler(0);
				
				if(counter == 20) {
					counter = 0;
					diamonds = 0;
					boom = 0;
				}
				
				if(isWin) {
					diamonds += 1;
					textArea2.setText("Szczęście uśmiechnęło się do Ciebie! Wygrałeś diament !!");
				}else {
					boom += 1;
					textArea2.setText("Masz dzisiaj pecha :( Bomba!");
				}
				
				textArea.setText("Próba" + counter + "Liczba diamentów" + diamonds);
			} });
		
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				counter += 1;
				boolean isWin = getRandomNumberHandler(1);
				
				if(counter == 20) {
					counter = 0;
					diamonds = 0;
					boom = 0;
				}
				
				if(isWin) {
					diamonds += 1;
					textArea2.setText("Szczęście uśmiechnęło się do Ciebie! Wygrałeś diament !!");
				}else {
					boom += 1;
					textArea2.setText("Masz dzisiaj pecha :( Bomba!");
				}
				
				textArea.setText("Próba" + counter + "Liczba diamentów" + diamonds);
			} });
		
		
		
		this.setSize(400,400);
		this.setVisible(true);
		
	
	}
	
	public boolean getRandomNumberHandler(int selectedValue) {
		 int randomNumber = getRandomNumber(0, 2);
		 if(randomNumber == 0 && selectedValue == 0) {
			 return true;
		 }
		 if(randomNumber == 1 && selectedValue == 1) {
			 return true;
		 }
		 return false;
 	}
	
	

 	public void actionPerformed(ActionEvent arg0) {
		System.out.println("Button - actionPerformed");
 	}
 	
 	public int getRandomNumber(int min, int max) {
 	    return (int) ((Math.random() * (max - min)) + min);
 	}
	

;
}
Editor is loading...