the way i have set up my code is to display an error message once the credit drops below 0. at the moment the second the last go puts the credit to 0 it displays the error message i was wondering if someone could tell me how to make the message appear once the last go has been finished.
my second question is related to how i stop the credit going over £5. i have made it so that once the credit goes over 500 it displays and error message but if it is on a amount which isnt a whole number say 420 it will allow the balance to go to 520. i have spent around 4 hours + trying to figure it out. i am not looking for my work doing for me but would just appricate some tips as im getting frustrated that the answer is still aluding me..
MY CODE SO FAR!:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class ElectronicBandit extends JFrame implements ActionListener
{
//list of all the variables in the project
private JButton btnInsertMoney, btnStart, btnBye;
private JTextField txtcredit;
private JLabel lblCredit, lblno1, lblno2, lblno3;
private int credit = 0, counter = 0, spin1;
private Random random;
private Icon icnMelon,icnOrange,icnBar,icnGrapes,icnLemon,icnBell,icnSeven,icnCherry;
private Timer timer1;
public static void main(String[] args)
{
ElectronicBandit ElectronicBandit = new ElectronicBandit();
}
public ElectronicBandit()
{
//definitions of all the variables detailed above (basically put all the buttons the window)
JOptionPane.showMessageDialog(null,"Hello and welcome to Electronic Bandit!!");
setLayout(new FlowLayout());
timer1 = new Timer(300, this);
random = new Random();
lblCredit = new JLabel ("Credit");
lblCredit.setFont(new Font("TimesRoman", Font.BOLD, 16));
txtcredit= new JTextField(3);
txtcredit.setFont(new Font("TimesRoman", Font.BOLD, 16));
txtcredit.setBackground(Color.green);
txtcredit.setForeground(Color.red);
txtcredit.setText(credit + " ");
lblno1 = new JLabel ("");
lblno1.setPreferredSize(new Dimension(150, 150));
lblno1 .setFont(new Font("TimesRoman", Font.BOLD,50));
lblno1.setBackground(Color.white);
lblno2 = new JLabel ("");
lblno2.setPreferredSize(new Dimension(150, 150));
lblno2 .setFont(new Font("TimesRoman", Font.BOLD,50));
lblno2.setBackground(Color.white);
lblno3= new JLabel ("");
lblno3.setPreferredSize(new Dimension(150, 150));
lblno3 .setFont(new Font("TimesRoman", Font.BOLD,50));
lblno3.setBackground(Color.white);
btnInsertMoney = new JButton("Insert £1.00");
btnStart = new JButton("Start");
btnStart.setEnabled(false);
btnBye = new JButton("Bye!!");
icnMelon = new ImageIcon("bin/Melon.jpg");
icnOrange = new ImageIcon("bin/Orange.jpg");
icnBar = new ImageIcon("bin/Bar.jpg");
icnGrapes = new ImageIcon("bin/Grapes.jpg");
icnLemon = new ImageIcon("bin/Lemon.jpg");
icnBell= new ImageIcon("bin/Bell.jpg");
icnSeven = new ImageIcon("bin/Seven.jpg");
icnCherry = new ImageIcon("bin/Cherry.jpg");
add(txtcredit);
add(lblCredit); add(lblno1); add(lblno2); add(lblno3);
add(btnInsertMoney); add(btnStart); add(btnBye);
btnInsertMoney.addActionListener(this);
btnStart.addActionListener(this);
btnBye.addActionListener(this);
setTitle("Simple GUI Demo. . . .");
setSize(1000, 1000);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
// this states that once the button insert money is pressed it increases the balance by 100 (£1)
if (event.getSource()== btnInsertMoney){
txtcredit.setForeground(Color.black);
credit = credit + 100;
txtcredit.setText(credit + " ");
}
// this is the increment money function.
// the first if statement disables the button if the total becomes equal to 500 (£5)
if (credit >= 500)
{
btnInsertMoney.setEnabled(false);
JOptionPane.showMessageDialog(null,"Maximum Pay In Reached, Time to Start Playing!!");
}
// the second statement re-enables the button once the balance goes back below 0
if (credit <= 0)
{
btnInsertMoney.setEnabled(true);
}
// this is the decrement money function.
// the first if statement disables the button if the total becomes equal to 0 or goes below
if (credit <= 0)
{
btnStart.setEnabled(false);
JOptionPane.showMessageDialog(null,"Insert More Money to Continue!!");
}
// the second statement enables the button once the balance goes back above 20
if (credit >= 20)
{
btnStart.setEnabled(true);
}
// this states that once the button decrement is pressed it decreases the balance by 20 (20) and starts the timer
if (event.getSource()== btnStart)
{
counter = 0;
timer1.start();
txtcredit.setForeground(Color.red);
credit = credit - 20;
txtcredit.setText(credit + " ");
}
if (event.getSource()== timer1)
{
setIcon1();
}
// this states that once the button bye is pressed the program is closed
if (event.getSource()== btnBye)
{
System.exit(0);
}
}
private void setIcon1()
{
spin1= random.nextInt(8);
switch (spin1)
{
case 1:
lblno1.setIcon(icnMelon);
counter = counter + 1;
break;
case 2:
lblno1.setIcon(icnOrange);
counter = counter + 1;
break;
case 3:
lblno1.setIcon(icnBar);
counter = counter + 1;
break;
case 4:
lblno1.setIcon(icnGrapes);
counter = counter + 1;
break;
case 5:
lblno1.setIcon(icnLemon);
counter = counter + 1;
break;
case 6:
lblno1.setIcon(icnBell);
counter = counter + 1;
break;
case 7:
lblno1.setIcon(icnSeven);
counter = counter + 1;
break;
case 8:
lblno1.setIcon(icnCherry);
counter = counter + 1;
break;
}
if (counter ==10)
timer1.stop();
}
}
This post has been edited by macosxnerd101: 15 November 2010 - 11:19 AM
Reason for edit:: Please use code tags

New Topic/Question
Reply



MultiQuote








|