我正在使用eclipse创建一台老虎机
我试图以某种方式更新“奖金”JTextField,以便在选择随机图像时,它会添加到已经显示在JTextField中的数字,而不是它在分钟时所执行的操作。显示在特定旋转中获胜的数量。
我也在努力设置代码,以便在获得注释时,不添加任何内容。任何帮助,将不胜感激。干杯
我的代码:
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.pink);
btncollect.setForeground(Color.white);
btninsertfunds = new JButton("Insert Funds");
btninsertfunds.setPreferredSize( new Dimension(200,200));
btninsertfunds.addActionListener(this);
btninsertfunds.setBackground (Color.yellow);
btninsertfunds.setForeground(Color.black);
btnquit = new JButton("Quit");
btnquit.setPreferredSize( new Dimension(200,200));
btnquit.addActionListener(this);
btnquit.setBackground(Color.black);
btnquit.setForeground(Color.white);
btnspin = new JButton ("Spin");
btnspin.setPreferredSize( new Dimension(200,200));
btnspin.addActionListener(this);
btnspin.setBackground(Color.red);
btnspin.setForeground(Color.black);
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("Winnings");
txtwinnings.setPreferredSize( new Dimension(200,200));
txtwinnings.setBackground(Color.orange);
txtwinnings.setForeground(Color.black);
txtbank = new JTextField ("Bank");
txtbank.setPreferredSize( new Dimension (200,200));
txtbank.setBackground(Color.green);
txtbank.setForeground(Color.black);
match3 = new JLabel ("Match 3 Symbols = 100");
match2 = new JLabel ("Match 2 Symbols = 50");
onecherry = new JLabel (" One Cherry = 20");
iconpic1 = new ImageIcon("src/resources/app.png");
iconpic2 = new ImageIcon("src/resources/str.png");
iconpic3 = new ImageIcon("src/resources/ban.png");
iconpic4 = new ImageIcon("src/resources/che.png");
iconpic5 = new ImageIcon("src/resources/pin.png");
iconpic6 = new ImageIcon("src/resources/org.png");
iconpic7 = new ImageIcon("src/resources/grp.png");
iconpic8 = new ImageIcon("src/resources/lem.png");
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,800);
}
public void actionPerformed (ActionEvent e){
//TODO Auto-generated method stub
if(e.getSource()==btninsertfunds){
txtbank.setForeground(Color.black);
bank=bank +25 ;
txtbank.setText("Bank = " + bank);
if (bank>=50)
btninsertfunds.setEnabled(true);
if(bank>=400)
btninsertfunds.setEnabled(false);
{
if (bank>=50)
btnspin.setEnabled(true);
if (bank<50)
btnspin.setEnabled(false);
}
}
if (e.getSource()==btnspin){
bank = bank -50 ;
txtbank.setText("Bank = " );
System.out.println("here");
setIcon1();setIcon2();setIcon3();
if(a==b && a==c ){
txtwinnings.setText("winnings = 100");
}
else
if (a==b || a==c || b==c)
{
txtwinnings.setText("winnings = 50");
}
else
if (a== 3 || b== 3 ||c== 3){
txtwinnings.setText("winnings = 20");
}
}
if (e.getSource()==btnquit)
{
System.exit(0);
}
}
private void setIcon1(){
// TODO Auto-generated method stub
a=random.nextInt(8);
switch(a)
{
case 0:
btnpic1.setIcon(iconpic1);
counter = counter+1;
break;
case 1:
btnpic1.setIcon(iconpic2);
counter = counter+1;
break;
case 2:
btnpic1.setIcon(iconpic3);
counter = counter+1;
break;
case 3:
btnpic1.setIcon(iconpic4);
counter = counter+1;
break;
case 4:
btnpic1.setIcon(iconpic5);
counter = counter+1;
break;
case 5:
btnpic1.setIcon(iconpic6);
counter = counter+1;
break;
case 6:
btnpic1.setIcon(iconpic7);
counter = counter+1;
break;
case 7:
btnpic1.setIcon(iconpic8);
counter = counter+1;
}
}
private void setIcon2()
{
b=random.nextInt(8);
switch(b)
{
case 0:
btnpic2.setIcon(iconpic1);
counter = counter+1;
break;
case 1:
btnpic2.setIcon(iconpic2);
counter = counter+1;
break;
case 2:
btnpic2.setIcon(iconpic3);
counter = counter+1;
break;
case 3:
btnpic2.setIcon(iconpic4);
counter = counter+1;
break;
case 4:
btnpic2.setIcon(iconpic5);
counter = counter+1;
break;
case 5:
btnpic2.setIcon(iconpic6);
counter = counter+1;
break;
case 6:
btnpic2.setIcon(iconpic7);
counter = counter+1;
break;
case 7:
btnpic2.setIcon(iconpic8);
counter = counter+1;
}
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;
}
}
}
答案 0 :(得分:1)
为奖金维护一个单独的变量,可能是double
。计算每轮的奖金并将其添加到变量中,然后使用Double.toString
或NumberFormat.getNumberInstance().format(...)
之类的内容将值转换为String
并将其设置为文本字段的文本
例如......
if(a==b && a==c ){
winnings += 100;
}
else if (a==b || a==c || b==c)
{
winnings += 50;
}
else if (a== 3 || b== 3 ||c== 3){
winnings += 20;
}
txtwinnings.setText(NumberFormat.getInstance().format(winnings));