1 Replies - 310 Views - Last Post: 18 December 2013 - 06:29 AM Rate Topic: -----

#1 ptpg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-December 13

change text in a text field through the click of a button

Posted 18 December 2013 - 05:54 AM

Firstly i am wanting the spin button to be enabled if the bank is >=20. Secondly i am wanting the amount shown in the bank text field to be minused by 20 everytime the spin button is clicked but am having trouble with these. Any help would be appreciated. Thank you;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;


public class ElecBandit extends JFrame implements ActionListener
{

	private JPanel paper;
	
	private JButton btnspin,btninsertfunds,btncollect,btnquit,btnpic1,btnpic2,btnpic3;
	
	private Random random;
	
	private Timer timer1;
	
	private int a =0, b=0, c=0, counter,number,collect,winnings,bank;
	 
	private Icon iconpic1, iconpic2, iconpic3, iconpic4, iconpic5, iconpic6, iconpic7, iconpic8; 
	
	private JTextField txtbank, txtwinnings;
	
	private JLabel match3, match2, onecherry;



	public static void main(String[] args) 
	{

		ElecBandit elecbandit = new ElecBandit();
		elecbandit.setVisible(true);
		}

	public ElecBandit(){
	
setLayout (new FlowLayout());
timer1 = new Timer(900,this);
random = new Random();
}
{
	
btncollect = new JButton("Collect");
btncollect.setPreferredSize( new Dimension(200,200));
btncollect.addActionListener(this);
btncollect.setBackground(Color.cyan);
btncollect.setForeground(Color.white);

btninsertfunds = new JButton("Insert Funds");
btninsertfunds.setPreferredSize( new Dimension(200,200));
btninsertfunds.addActionListener(this);
btninsertfunds.setBackground(Color.orange);
btninsertfunds.setForeground(Color.white);

btnquit = new JButton("Quit");
btnquit.setPreferredSize( new Dimension(200,200));
btnquit.addActionListener(this);
btnquit.setBackground(Color.red);
btnquit.setForeground(Color.black);

btnspin = new JButton("Spin");
btnspin.setPreferredSize( new Dimension(200,200));
btnspin.addActionListener(this);
btnspin.setBackground(Color.black);
btnspin.setForeground(Color.red);

btnpic1 = new JButton("");
btnpic1.setPreferredSize( new Dimension(200,200));
btnpic1.setBackground(Color.white);

btnpic2 = new JButton("");
btnpic2.setPreferredSize( new Dimension(200,200));
btnpic2.setBackground(Color.white);

btnpic3 = new JButton("");
btnpic3.setPreferredSize( new Dimension(200,200));
btnpic3.setBackground(Color.white);

txtwinnings = new JTextField("");
txtwinnings.setPreferredSize( new Dimension(200,200));
txtwinnings.setBackground(Color.magenta);

txtbank = new JTextField("Bank");
txtbank.setPreferredSize( new Dimension(200,200));
txtbank.setBackground(Color.yellow);

match3 = new JLabel ("Match 3 Symbols = 100");
match2 = new JLabel ("Match 2 Symbols = 50");
onecherry = new JLabel ("One Cherry = 20");


iconpic1 = new ImageIcon("bin/app.gif");

iconpic2 = new ImageIcon("bin/nte.gif");

iconpic3 = new ImageIcon("bin/ban.gif");

iconpic4 = new ImageIcon("bin/che.gif");

iconpic5 = new ImageIcon("bin/pin.gif");

iconpic6 = new ImageIcon("bin/org.gif");

iconpic7 = new ImageIcon("bin/7.gif");

iconpic8 = new ImageIcon("bin/lem.gif");



add(btninsertfunds);

add(btncollect);

btncollect.setEnabled(false);

add(txtwinnings);

add(txtbank);

add(btnspin);

btnspin.setEnabled(false);

add(btnquit);

add(btnpic1);

add(btnpic2);

add(btnpic3);

add(match3);

add(match2);

add(onecherry);


btninsertfunds.addActionListener(this);

btncollect.addActionListener(this);

btnquit.addActionListener(this);

btnspin.addActionListener(this);



setTitle("One Arm Bandit");
setSize(700,700);
}



public void actionPerformed (ActionEvent e){
	//TODO Auto-generated method stub

	if(e.getSource()==btninsertfunds){
		bank=bank + 100;
	txtbank.setText("bank = " + bank);
		
		if (bank>=20)
		
			btninsertfunds.setEnabled(true);
			
{
	
	if (bank<20)
		btnspin.setEnabled(false);
}
if(e.getSource()==btnspin){
	counter=0;
	timer1.start();
	setIcon1();
	setIcon2();
	setIcon3();
	
	
	
	if (bank<=0)
	btnspin.setEnabled(false);
}

}

	
	if (e.getSource()==btnquit)
	{
		System.exit(0);
	}
		
	}
		
	

	
private void setIcon1()
{
	a=random.nextInt(8);
	switch(a)
	{
	
	case 0:
		btnpic1.setIcon(iconpic1);
		counter = counter++;
		
		break;
		
	case 1:
		btnpic1.setIcon(iconpic2);
		counter = counter++;
		
		break;
		
	case 2:
		btnpic1.setIcon(iconpic3);
		counter = counter++;
		
		break;
		
	case 3:
		btnpic1.setIcon(iconpic4);
		counter = counter++;
		
		break;
		
	case 4:
		btnpic1.setIcon(iconpic5);
		counter = counter++;
		
		break;
		
	case 5:
		btnpic1.setIcon(iconpic6);
		counter = counter++;
		
		break;
		
	case 6:
		btnpic1.setIcon(iconpic7);
		counter = counter++;
		
		break;
		
	case 7:
		btnpic1.setIcon(iconpic8);
		counter = counter++;
		
	}
	
		
	}


private void setIcon2()
{
	b=random.nextInt(8);
	switch(B)/>
	{
	
	case 0:
		btnpic2.setIcon(iconpic1);
		counter = counter++;
		
		break;
		
	case 1:
		btnpic2.setIcon(iconpic2);
		counter = counter++;
		
		break;
		
	case 2:
		btnpic2.setIcon(iconpic3);
		counter = counter++;
		
		break;
		
	case 3:
		btnpic2.setIcon(iconpic4);
		counter = counter++;
		
		break;
		
	case 4:
		btnpic2.setIcon(iconpic5);
		counter = counter++;
		
		break;
		
	case 5:
		btnpic2.setIcon(iconpic6);
		counter = counter++;
		
		break;
		
	case 6:
		btnpic2.setIcon(iconpic7);
		counter = counter++;
		
		break;
		
	case 7:
		btnpic2.setIcon(iconpic8);
		counter = counter++;
		
	}
	
	if(counter ==10);
	timer1.stop();
	
}

private void setIcon3()
{
	c=random.nextInt(8);
	switch(c)
	{
	
	case 0:
		btnpic3.setIcon(iconpic1);
		counter = counter+1;
		
		break;
		
	case 1:
		btnpic3.setIcon(iconpic2);
		counter = counter+1;
		
		break;
		
	case 2:
		btnpic3.setIcon(iconpic3);
		counter = counter+1;
		
		break;
		
	case 3:
		btnpic3.setIcon(iconpic4);
		counter = counter+1;
		
		break;
		
	case 4:
		btnpic3.setIcon(iconpic5);
		counter = counter+1;
		
		break;
		
	case 5:
		btnpic3.setIcon(iconpic6);
		counter = counter+1;
		
		break;
		
	case 6:
		btnpic3.setIcon(iconpic7);
		counter = counter+1;
		
		break;
		
	case 7:
		btnpic3.setIcon(iconpic8);
		counter = counter+1;
		
	}
	
	
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: change text in a text field through the click of a button

#2 fishzilla  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 53
  • Joined: 08-November 13

Re: change text in a text field through the click of a button

Posted 18 December 2013 - 06:29 AM

You could do something like..

if (button == clicked)
textfield.setText(value - 20)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1