这个程序最终被设计成一个老虎机,它将被整合到一个朋友的游戏中,仍处于预先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();
}
} );
}
}
这里有三个漏洞,我一直在寻找答案而且找不到任何答案,所以非常感谢帮助。
由于某种原因,第九个插槽不会听取放置参数,只是坐在旁边。
单击应该更改图像的按钮后,插槽并不总是出现(可能我需要使用除JLabel之外的其他东西吗?)
背景不会改变颜色。
答案 0 :(得分:5)
该代码有很多问题存在问题,让我列举一些:
....等......
为朋友发布代码"班级项目。
我认为此代码的最佳解决方法是废弃它,而是尝试使用数组从头开始重新编写代码,使用布局管理器,避免使用静态变量。重新开始,您可以快速创建一个漂亮的GUI,并在此过程中学到很多东西。
修改2