该程序最终被设计为制作一个类似于老虎机的东西,它将被集成到朋友的游戏中,仍处于前 alpha 阶段,并且将持续很长时间或者可能永远存在。(他只将它用于课堂项目ATM)
package SlotMachine;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.Color;
public class SlotGui{
static Random one = new Random();
static Random two = new Random();
static Random three = new Random();
static Random four = new Random();
static Random five = new Random();
static Random six = new Random();
static Random seven = new Random();
static Random eight = new Random();
static Random nine = new Random();
static int st = one.nextInt(10);
static int nd = two.nextInt(10);
static int trd = three.nextInt(10);
static int frth = four.nextInt(10);
static int fth = five.nextInt(10);
static int sxth = six.nextInt(10);
static int svth = seven.nextInt(10);
static int eth = eight.nextInt(10);
static int nth = nine.nextInt(10);
static int coins = 15;
static JTextField money = new JTextField(Integer.toString(coins));
static JLabel blueLabel = new JLabel();
static JLabel slotOne = new JLabel();
static JLabel slotTwo = new JLabel();
static JLabel slotThree = new JLabel();
static JLabel slotFour = new JLabel();
static JLabel slotFive = new JLabel();
static JLabel slotSix = new JLabel();
static JLabel slotSeven = new JLabel();
static JLabel slotEight = new JLabel();
static JLabel slotNine = new JLabel();
static Icon lever = new ImageIcon("lever.jpg");
static Icon a = new ImageIcon("0.jpg");
static Icon b = new ImageIcon("1.jpg");
static Icon c = new ImageIcon("2.jpg");
static Icon d = new ImageIcon("3.jpg");
static Icon ee = new ImageIcon("4.jpg");
static Icon f = new ImageIcon("5.jpg");
static Icon g = new ImageIcon("6.jpg");
static Icon h = new ImageIcon("7.jpg");
static Icon i = new ImageIcon("8.jpg");
static Icon j = new ImageIcon("9.jpg");
static JButton startLever = new JButton(lever);
static Color backGround = new Color (0,0,0);
public static void slotVisualSet(JLabel slot,int Xval, int Yval, int h, int w){
slot.setOpaque(true);
slot.setLocation(Xval,Yval);
slot.setSize(h,w);
slot.setVisible(true);
}
public static void slotLogic(JLabel slotLab,int slotNum){
if (slotNum == 0){
slotLab.setIcon(a);
} else if (slotNum == 1){
slotLab.setIcon(b);
} else if (slotNum == 2){
slotLab.setIcon(c);
} else if (slotNum == 3){
slotLab.setIcon(d);
} else if (slotNum == 4){
slotLab.setIcon(ee);
} else if (slotNum == 5){
slotLab.setIcon(f);
} else if (slotNum == 6){
slotLab.setIcon(g);
} else if (slotNum == 7){
slotLab.setIcon(h);
} else if (slotNum == 8){
slotLab.setIcon(i);
} else if (slotNum == 9){
slotLab.setIcon(j);
}
}
public static void makeWindow(){
//creating the window
JFrame windo = new JFrame ();
windo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
windo.setExtendedState(JFrame.MAXIMIZED_BOTH);
windo.setVisible(true);
//creating the components of the window
money.setOpaque(true);
money.setLocation(500,900);
money.setSize(60,20);
blueLabel.setOpaque(true);
blueLabel.setBackground(backGround);
blueLabel.setPreferredSize(new Dimension(1000, 1000));
blueLabel.setVisible(true);
blueLabel.setLayout(null);
//setting the coordinates and sizes of the slots
slotVisualSet(slotOne,100,100,225,225);
slotVisualSet(slotTwo,350,100,225,225);
slotVisualSet(slotThree,600,100,225,225);
slotVisualSet(slotFour,100,350,225,225);
slotVisualSet(slotFive,350,350,225,225);
slotVisualSet(slotSix,600,350,225,225);
slotVisualSet(slotSeven,100,600,225,225);
slotVisualSet(slotEight,350,600,225,225);
slotVisualSet(slotNine,600,600,225,225);
startLever.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int st = one.nextInt(10);
int nd = two.nextInt(10);
int trd = three.nextInt(10);
int frth = four.nextInt(10);
int fth = five.nextInt(10);
int sxth = six.nextInt(10);
int svth = seven.nextInt(10);
int eth = eight.nextInt(10);
int nth = nine.nextInt(10);
coins = coins - 5;
money.setText(Integer.toString(coins));
// making the slots change pictures when the lever is pulled
slotLogic(slotOne,st);
slotLogic(slotTwo,nd);
slotLogic(slotThree,trd);
slotLogic(slotFour,frth);
slotLogic(slotFive,fth);
slotLogic(slotSix,sxth);
slotLogic(slotSeven,svth);
slotLogic(slotEight,eth);
slotLogic(slotNine,nth);
if ((st == nd) && (nd == trd)){
coins = coins + 30;
}else if((frth == fth) && (fth == sxth)){
coins = coins + 30;
}else if ((svth == eth) && (eth == nth)){
coins = coins + 30;
} else if ((st == fth) && (fth == nth)){
coins = coins + 100;
}else if ((svth == fth) && (fth == trd)){
coins = coins + 100;
}
}
});
startLever.setSize(183,275);
startLever.setLocation(1000,300);
windo.add(startLever);
windo.add(money);
windo.add(blueLabel);
windo.add(slotOne);
windo.add(slotTwo);
windo.add(slotThree);
windo.add(slotFour);
windo.add(slotFive);
windo.add(slotSix);
windo.add(slotSeven);
windo.add(slotEight);
windo.add(slotNine);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeWindow();
}
} );
}
}
这里有三个错误,我一直在寻找答案,但一直找不到,所以非常感谢帮助。
第 9 个插槽由于某种原因没有去听放置参数,只是坐在一边。
单击应该更改图像的按钮后,插槽并不总是出现(也许我需要为此使用 JLabels 以外的东西?)
背景不会改变颜色。