Java 多个bug,没有理由

Java 多个bug,没有理由,java,Java,这个程序最终被设计成一个类似吃角子老虎机的东西,它将被集成到朋友的游戏中,仍然处于阿尔法之前的阶段,并且将持续很长时间,甚至可能是永远。(他只是把它用于一个班级项目) 这里有三个错误,我一直在寻找答案,但没有找到任何,所以非常感谢您的帮助 由于某种原因,第九个槽不去听放置参数,只是坐在一边 在您单击要更改图像的按钮后,插槽并不总是出现(可能我需要使用JLabels以外的其他工具来完成此操作?) 背景颜色不变 该代码有很多问题,让我列举一些: 尝试使用容器在非空布局上设置组件的位置 尝试使用设置

这个程序最终被设计成一个类似吃角子老虎机的东西,它将被集成到朋友的游戏中,仍然处于阿尔法之前的阶段,并且将持续很长时间,甚至可能是永远。(他只是把它用于一个班级项目)

这里有三个错误,我一直在寻找答案,但没有找到任何,所以非常感谢您的帮助

  • 由于某种原因,第九个槽不去听放置参数,只是坐在一边

  • 在您单击要更改图像的按钮后,插槽并不总是出现(可能我需要使用JLabels以外的其他工具来完成此操作?)

  • 背景颜色不变


  • 该代码有很多问题,让我列举一些:

    • 尝试使用容器在非空布局上设置组件的位置
    • 尝试使用设置位置,而不使用布局管理器。这就是他们的目的,使创建复杂的GUI变得容易,而不必担心精确的定位
    • 不使用数组
    • 过度使用静电。所有字段都不应是静态的编辑:除了背景色(很可能是一个常量)之外,还有一个名为background的静态最终变量
    • 使用九个随机对象。一个可以很好地工作,并且不会那么混乱
    • 。。。。等等

    • “朋友”类项目的发布代码


    我认为这段代码的最佳修复方法是将其丢弃,并尝试使用数组、布局管理器、避免静态变量从头开始重新编写代码。从头开始,您可以快速创建一个漂亮的GUI,并在这个过程中学到很多东西


    编辑2

    • 考虑使用一个使用JPanel的GridLayout来保存您的3 x 3 JLabel网格
    • 考虑对整个GUI使用BorderLayout,将GridLayout JPanel放置在BorderLayout.CENTER位置
    • “播放”按钮可以放在位于主GUI BorderLayout.EAST位置的JPanel中
    • 打赌窗口可以位于位于BorderLayout.SOUTH位置的主GUI中的JPanel中
    • 同样,数组的使用将简化和压缩代码,使调试和增强变得更加容易
    • 布局管理器的使用也是如此,因为它们将使您更容易调试、增强和修改GUI

    如果可能,我建议使用阵列。这段代码很难理解。Stackoverflow不是一个调试器。您是否在调试器下一步一步地尝试了这段代码,以了解到底发生了什么?正确的标题应该是“多个错误,至少有一个原因”,正如您编写的程序不正确一样。@ROBBYNATOS您可以使用随机数生成器生成任意数量的数字。只需多次调用
    nextInt()
    ,每次都会得到不同的号码。-这不是我的“朋友代码”,这是我给朋友的代码-请您解释一下如何在不过度使用静电的情况下执行此操作-我喜欢精确的定位,不介意编写额外的代码,此外,这不是一个错误,只是一个编码偏好-哦,谢谢你指出容器没有设置为nulllayout@ROBBYNATOS对于你朋友的课程项目?是的,它开始时是一个课程项目,但他想扩展它,我有这个代码,我可以修复并实现
    “此外,这不是一个错误,只是一个编码偏好“
    也许吧,但有好代码,也有坏代码。太多的代码就是糟糕的代码。@ROBBYNATOS:对这段代码最好的修复方法是将其丢弃,并尝试使用数组、布局管理器从头开始重新编写代码,避免使用静态变量。从头开始,您可以快速创建一个漂亮的GUI,并在这个过程中学到很多东西。
    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();
    
                }
            } );
        }   
    
    }