老虎机Java游戏

老虎机Java游戏,java,swing,jframe,jpanel,jlabel,Java,Swing,Jframe,Jpanel,Jlabel,我正在为学校制作这个java吃角子老虎机,并对它进行了编码。我正在使用JFrames,但无法在屏幕上显示图像。插槽[]jLabel似乎有问题。插槽是容纳三个不同卷盘的阵列。我不知道这个问题是否与网格布局有关。我很困惑 package slots; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.FlowLayout; import java.awt.Gr

我正在为学校制作这个java吃角子老虎机,并对它进行了编码。我正在使用JFrames,但无法在屏幕上显示图像。插槽[]jLabel似乎有问题。插槽是容纳三个不同卷盘的阵列。我不知道这个问题是否与网格布局有关。我很困惑

package slots;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class SlotMachineProgram extends JFrame {
  private int total = 2000; //this is the max bet you can have 
  private int bet = 100; //default bet amount 
  private int maxWin; //this variable stores the max wins 

  private String[] symbols = {
    "Cherry",
    "Bear",
    "Diamond",
    "Penguin",
    "Star",
    "Shoe",
    "Banana",
    "Orange",
    "Apple"
  }; //symbols that are available for the slot machine 

  //amount winning
  int[] gotTwo = {
    30,
    16,
    15,
    12,
    11,
    10,
    9,
    7,
    5
  }; //Prize moneys for two matches 
  int[] gotThree = {
    60,
    32,
    30,
    24,
    22,
    20,
    18,
    14,
    10
  }; //prize money for three matches 

  public JPanel slotPanel = new JPanel(new GridLayout(1, 3)); //creates a grid that holds the three slot options 
  public JLabel[] slot = new JLabel[3]; //create the three slots 

  //LABELS
  private JLabel amount = new JLabel("Total: $" + total); //this is the label for the total amount 
  private JLabel betLabel = new JLabel("Bet: $" + bet); //this is the label for the bet amount 

  //BUTTONS 
  private JButton add50Button = new JButton("+50"); //creates a button called add50Button that adds 50 dollars to the bet
  private JButton spin = new JButton("Spin"); //creates a button that spins the slot machine 
  private JButton maxBetButton = new JButton("Max"); //creates a button that makes the max amount the bet 
  private JButton subtract50Button = new JButton("-50"); //creates a button that subtracts 50 from bet 
  private JButton getBet = new JButton("Enter Bet"); //a button that pops up a new window that allows the user to type in a bet

  public SlotMachineProgram() { //constructor
    maxWin = total; //set win to total 

    //Coloring Buttons
    spin.setBackground(Color.GREEN); //sets color of button to be green 
    spin.setOpaque(true); //Sets Button Opaque so it can be seen 
    maxBetButton.setBackground(Color.CYAN); //sets color of button to be green 
    maxBetButton.setOpaque(true); //Sets Button Opaque so it can be seen 
    add50Button.setBackground(Color.YELLOW); //sets color of button to be green 
    add50Button.setOpaque(true); //Sets Button Opaque so it can be seen 
    subtract50Button.setBackground(Color.MAGENTA); //sets color of button to be green 
    subtract50Button.setOpaque(true); //Sets Button Opaque so it can be seen 
    getBet.setBackground(Color.ORANGE); //sets color of button to be green 
    getBet.setOpaque(true); //Sets Button Opaque so it can be seen 

    //money panel
    JPanel moneyPanel = new JPanel(new GridLayout(2, 1)); //creates a grid called money panel that is 2x1 that stores total and bet
    moneyPanel.setBorder(new TitledBorder("Money")); //creates the title of the panel 
    moneyPanel.add(amount); //adds the amount to the panel 
    moneyPanel.add(betLabel); //adds the BetLabel to the panel 

    //button panel 
    JPanel buttonsPanel = new JPanel(new GridLayout(2, 1)); //creates a new panel called buttonsPanel that has all of the buttons 
    JPanel buttonTopPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 2)); //this orients the buttons to the middle in a row
    buttonsPanel.setBorder(new TitledBorder("             Buttons")); //sets the title of the buttons panel to buttons 

    buttonTopPanel.add(add50Button); //add the add50button to the button panel
    buttonTopPanel.add(spin); //adds a spin button
    buttonTopPanel.add(maxBetButton); //adds a max bet button 

    JPanel BottomPanel = new JPanel(); //creates a panel for the bottom of the screen 
    BottomPanel.add(subtract50Button); //adds the subtract 50 button
    BottomPanel.add(getBet); //adds the get bet button

    buttonsPanel.add(buttonTopPanel, BorderLayout.NORTH); //adds a border on the north edge
    buttonsPanel.add(BottomPanel, BorderLayout.SOUTH); //adds a border on the south edge

    // TODO
    //set slots to their default image
    slot[0] = new JLabel(new ImageIcon("/images/left.png"));
    slot[1] = new JLabel(new ImageIcon("/images/middle.png"));
    slot[2] = new JLabel(new ImageIcon("/images/right.png"));


    //add Action listeners to bet buttons
    add50Button.addActionListener(new ActionListener() { //action listener for the add 50 button 
      @Override public void actionPerformed(ActionEvent e) { //makes an action performed method
        bet += 50; //increase bet by 50 if the button add50 is clicked
        if (bet > total) bet = total; //else skip
        betLabel.setText("Bet: $" + bet); //show the bet next to the bet label
      }
    });

    maxBetButton.addActionListener(new ActionListener() { //creates an action listener for the maxbetbutton
      @Override public void actionPerformed(ActionEvent e) { //creates a method for the maxbetbutton's action listener
        bet = total; //make bet the maxBet
        betLabel.setText("Bet: $" + bet); //show bet next to the bet label
      }
    });

    subtract50Button.addActionListener(new ActionListener() { //creates an actionlistener for the subtract 50 button
      @Override public void actionPerformed(ActionEvent e) { //creates an actionperformed method 
        bet -= 50; //subtracts the value of the bet by 50
        if (bet < 0) bet = 0; //is the user tries to have a negative bet, set bet to 0
        betLabel.setText("Bet: $" + bet); //display the value of the bet next to a bet label
      }
    });

    getBet.addActionListener(new ActionListener() { //create an actionlistener for the getbet button that allows the user to type in a custom amount 
      @Override public void actionPerformed(ActionEvent e) { // create an actionperformed method 
        String input; //creates a variables called input of type string. This is what is displayed on the screen
        int betInput; //creates a variables called betInput of type int 
        //"How much do you want to bet" is the text that appears in the bet amount window.
        //"Enter your bet amount" is the header on the bet window
        input = JOptionPane.showInputDialog(null,
          "How Much Do You Want To Bet? ", "Enter Your Bet Amount",
          JOptionPane.QUESTION_MESSAGE);
        betInput = Integer.parseInt(input); //set the user input to the betInput
        if (betInput > total) { //if the inputed value is more than the total amount of money they user has left
          bet = total; //set the bet to equal the amount of money the user has
        } else { //if the inputed bet amount is less than the total
          bet = betInput; //set the bet input the value of the bet
        }
        betLabel.setText("Bet: $" + bet); //updates the bet label to show the bet value next to its label
      }
    });

    spin.addActionListener(new ActionListener() { //creates an actionlistener for the spin button
      @Override
      public void actionPerformed(ActionEvent e) { //creates a method called actionperformed 
        String[] rand = new String[3]; //creates an array of 3 strings 
        int addTotal; //creates a variable called addTotal
        //generate 3 new slots
        for (int i = 0; i < 3; i++) { //loops through the three panes
          int choice; //creates a varaibe called choice 
          double random = Math.random(); //creates a random variable and assigns it a random value
          random *= 8; //times the value by 8
          choice = (int) random; //assigns the value of choice to be random with a cast of int
          rand[i] = symbols[choice]; //assigns  the value rand at index i to be symbols at index choice
          slot[i].setIcon(new ImageIcon("/images/" + symbols[choice] + ".png")); //shows the image for the slot in the panel
          // TODO
        }


        //check for matches
        //three matches
        if (rand[0] == rand[1] && rand[1] == rand[2]) { //if there were three matches
          int index = 0; //creates a variable index of type int and assigns it to the value 0 and 
          for (int i = 0; i < symbols.length; i++) //a loop that runs the length of symbols 
            if (rand[0] == symbols[i]) { //if rand[0] is equal to symbols[i]
              index = i; //assign index to equal i
            }
          addTotal = bet * gotThree[index]; //increase addTotal by bet times gotThree[index], which stores the bonuses for triple pairs
          total += addTotal; //increment total by addTotal
          //pop-up menu saying the bet amount, the amount won and the updated total
          JOptionPane.showMessageDialog(null, "Your Bet: $" + bet + "\nYou Win: $" + addTotal + "\nMoney Left: $" + total,
            "Three Matches", JOptionPane.INFORMATION_MESSAGE);

          amount.setText("Total: $" + total); //set the label Total: to equal total 
          if (total > maxWin) { //if the new total is greater than maxWin
            maxWin = total; //maxWin is now assigned to total
          }
        } else if (rand[0] == rand[1] || rand[1] == rand[2] || rand[2] == rand[0]) { //checks if there is a match between 2 tiles
          int match, index = 0; //creates integer variables match and index, assigning it to 0
          if (rand[0] == rand[1] || rand[0] == rand[2]) { //if the first box has a match
            match = 0; //match is assigned the value of 0
          } else { //if the second of third box has the match
            match = 1; //assign match to the value of 1
          }

          //find the index
          for (int i = 0; i < symbols.length; i++) { //A loop that iterates the amount of times of the length of symbols
            if (rand[0] == symbols[i]) { //if rand at index 0 is equal to symbols at index i
              index = i; //set index to equal i
            }
          }
          addTotal = bet * gotTwo[index]; //increment addTotal by bet*gotTwo[index]
          //Creates a pop-up window that displays the bet amount and the winnings
          JOptionPane.showMessageDialog(null, "Your Bet: $" + bet + "\nYou Win: $" + addTotal,
            "Two Matches", JOptionPane.INFORMATION_MESSAGE);
          total += addTotal; //increments total by addTotal
          amount.setText("Total: $" + total); //updates the text next to total
          if (total > maxWin) { //if the total is greater than maxWin
            maxWin = total; //set maxWin to total
          }


        } else { //run this is there are no matches
          total -= bet; //decrement  total by bet
          //Creates a pop-up window that shows the lost amount (bet) and the money left
          JOptionPane.showMessageDialog(null, "You Lost: $" + bet + "\nMoney Left: $" + total, "No Matches", JOptionPane.INFORMATION_MESSAGE);
          amount.setText("Total: $" + total); //updates the total amount that will be seen on the screen
          if (total > maxWin) { //if the total is greater than maxWin
            maxWin = total; //sets maxWin to equal total
          }
          if (bet > total) { //if bet is greater than the amount of money left (total) than 
            bet = total; //set the bet to total
            betLabel.setText("Bet: $" + bet); //create the betLabel and update the value of it 
          }
          if (total == 0) { //if the total is the 
            bet = 0; //sets the bet value to equal 0
            betLabel.setText("Bet: $" + bet); //updates the betLabel to the new bet value 
            //Creates a pop-up message saying "You Lost :( and it also displays the maximum amount that the user won
            JOptionPane.showMessageDialog(null, "You Lost :( \nThe Maximum That You Won Was $" + maxWin,
              "\nGame Over", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0); //exit the program 
          }
        }
      }
    });

    JPanel panelOnBottom = new JPanel(new BorderLayout()); //creates a panel on the bottom 
    panelOnBottom.add(moneyPanel, BorderLayout.WEST); //adds the money panel to the west/left border of the window
    panelOnBottom.add(buttonsPanel, BorderLayout.EAST); //adds the buttons panel to the east/right border of the window

    // TODO
    JPanel all = new JPanel(new GridLayout(2, 1)); //creates a panel called all with a grid layout of 2x1
    slotPanel.setVisible(true);
    slotPanel.setOpaque(true);
    all.add(slotPanel); //adds slotPanel to the 'all' panel
    all.add(panelOnBottom); //adds the panelOnBottom to the 'all' panel
    add(all); //add all to the window

  }

  public static void main(String[] args) { //the main class
    JFrame frame = new SlotMachineProgram(); //creates a new frame of type SlotMachineProgram
    frame.setTitle("Gambling Time!"); //creates the title of the frame to be Gambling Time!
    frame.setLocationRelativeTo(null); //sets the location of the window to null, meaning it opens in the middle 
    frame.setSize(400, 300); //sets the size of the window to be 400 by 300
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //sets the default close method to exit
    frame.setResizable(false); //makes the window not resizable
    frame.setVisible(true); //makes the window visible


  }
}
包槽;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.FlowLayout;
导入java.awt.Graphics;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.border.TitledBorder;
公共类SlotMachine程序扩展JFrame{
private int total=2000;//这是您可以进行的最大赌注
private int bet=100;//默认下注金额
private int maxWin;//此变量存储最大wins
专用字符串[]符号={
“樱桃”,
“熊”,
“钻石”,
“企鹅”,
“明星”,
“鞋”,
“香蕉”,
“橙色”,
“苹果”
};//老虎机可用的符号
//中奖金额
int[]gotwo={
30,
16,
15,
12,
11,
10,
9,
7.
5.
};//两场比赛的奖金
int[]gotThree={
60,
32,
30,
24,
22,
20,
18,
14,
10
};//三场比赛的奖金
public JPanel slotPanel=new JPanel(new GridLayout(1,3));//创建一个包含三个插槽选项的网格
public JLabel[]slot=new JLabel[3];//创建三个插槽
//标签
private JLabel amount=new JLabel(“总计:$”+总计);//这是总金额的标签
private JLabel betLabel=new JLabel(“下注:$”+下注);//这是下注金额的标签
//钮扣
private JButton add50Button=new JButton(“+50”);//创建一个名为add50Button的按钮,将赌注增加50美元
private JButton spin=new JButton(“spin”);//创建旋转老虎机的按钮
private JButton maxBetButton=new JButton(“Max”);//创建一个按钮,使最大金额成为赌注
private JButton subtract50 button=new JButton(“-50”);//创建一个从下注中减去50的按钮
private JButton getBet=new JButton(“Enter Bet”);//弹出一个新窗口的按钮,允许用户键入赌注
公共SlotMachineProgram(){//构造函数
maxWin=total;//将win设置为total
//彩色按钮
spin.setBackground(Color.GREEN);//将按钮的颜色设置为绿色
spin.setOpaque(true);//设置按钮不透明,以便可以看到它
maxBetButton.setBackground(Color.CYAN);//将按钮的颜色设置为绿色
maxBetButton.setOpaque(true);//将按钮设置为不透明,以便可以看到
add50Button.setBackground(Color.YELLOW);//将按钮的颜色设置为绿色
add50Button.setOpaque(true);//设置按钮不透明,以便可以看到
subtract50Button.setBackground(Color.MAGENTA);//将按钮的颜色设置为绿色
subtract50Button.setOpaque(true);//将按钮设置为不透明,以便可以看到
getBet.setBackground(Color.ORANGE);//将按钮的颜色设置为绿色
getBet.setOpaque(true);//设置按钮不透明,以便可以看到它
//货币小组
JPanel-moneyPanel=new-JPanel(new-GridLayout(2,1));//创建一个名为money-panel的网格,该网格为2x1,用于存储总计和赌注
moneyPanel.setboorder(新标题边框(“Money”);//创建面板的标题
moneyPanel.add(amount);//将金额添加到面板中
moneyPanel.add(betLabel);//将betLabel添加到面板
//按钮面板
JPanel buttonpanel=newjpanel(newgridlayout(2,1));//创建一个名为buttonpanel的新面板,该面板包含所有按钮
JPanel buttonoppanel=new JPanel(new FlowLayout(FlowLayout.CENTER,5,2));//这会将按钮定向到一行的中间
buttonsPanel.setOrder(新标题边框(“按钮”);//将按钮面板的标题设置为按钮
buttonTopPanel.add(add50Button);//将add50Button添加到按钮面板
添加(旋转);//添加旋转按钮
添加(maxBetButton);//添加最大下注按钮
JPanel BottomPanel=new JPanel();//为屏幕底部创建一个面板
BottomPanel.add(减法50按钮);//添加减法50按钮
BottomPanel.add(getBet);//添加获取赌注按钮
buttonPanel.add(buttonTopPanel,BorderLayout.NORTH);//在北边添加边框
buttonsPanel.add(底部面板,BorderLayout.SOUTH);//在南边添加边框
//待办事项
//将插槽设置为其默认图像
slot[0]=newjlabel(newimageicon(“/images/left.png”);
slot[1]=newjlabel(newimageicon(“/images/middle.png”);
slot[2]=newjlabel(newimageicon(“/images/right.png”);
//将动作侦听器添加到下注按钮
add50Button.addActionListener(新建ActionListener(){//add 50按钮的操作侦听器
@重写public void actionPerformed(ActionEvent e){//生成一个action-performed方法
bet+=50;//如果单击按钮add50,则将下注增加50
如果(bet>total)bet=total;//否则跳过
betLabel.setText(“Bet:$”+Bet);//在下注标签旁边显示下注
}
});
addActionListener(新建ActionListener(){//为maxBetButton创建一个操作侦听器
@Override public void actionPerformed(ActionEvent e){//为maxbetbutton的操作侦听器创建一个方法
bet=total;//最大赌注
betLabel.setText(“下注:$”+下注);//在下注标签旁边显示下注
}
});
Subtract50按钮。addActionListener(新建ActionListener(){//为Subtract50按钮创建ActionListener
@重写public void actionPerformed(ActionEvent e){//创建actionPerformed方法
bet-=50;//将下注值减去
//set slots to their default image
slot[0] = new JLabel(new ImageIcon("/images/left.png"));
slot[1] = new JLabel(new ImageIcon("/images/middle.png"));
slot[2] = new JLabel(new ImageIcon("/images/right.png"));

slotPanel.add(slot[0]);
slotPanel.add(slot[1]);
slotPanel.add(slot[2]);
private String[] symbols = { 
        "Cherry", "Bear", 
        "Diamond", "Penguin", 
        "Star", "Shoe", 
        "Banana", "Orange", 
        "Apple" }; // symbols that are available for the slot machine