1

I'm making a roulette game for my final in school. I am using Java and Swing and I am coding the GUI, not using a drag and drop software. I am having a hard time trying to figure out why a JLabel is not getting removed.

On the betting screen, I would like there to be a message that displays if you won or lost and what number the ball landed on. My problem is that when you place a second bet, the previous win/lose messing doesn't go away and the new one overlaps on it. My problem is in the straightUpBetMech() method.

public void actionPerformed(ActionEvent e) {

    //if they place their bet
    } else if (e.getSource() == placeBet) {

        straightUpBetMech();

    }
}


//the straight up bet GUI
public void straightUpBetGUI() {

    //title line

    //title
    straightUpTitle = new JLabel("Straight Up Bet");
    straightUpTitle.setBounds(50, 25, 350, 50);
    straightUpTitle.setFont(new Font("null", Font.PLAIN, 32));
    mainPanel.add(straightUpTitle);

    //balance display
    balanceDisplay = new JLabel("$100");
    balanceDisplay.setText("Current Balance: $" + balance);
    balanceDisplay.setBounds(50, 70, 350, 50);
    mainPanel.add(balanceDisplay);


    //Bet type rules button
    straightUpBetRules = new JButton("Straight Up Bet Rules");
    straightUpBetRules.setBounds(320, 25, 175, 50);
    straightUpBetRules.addActionListener(new Main());
    mainPanel.add(straightUpBetRules);

    //Bet type rules button
    betSelectionScreen = new JButton("Bet Selection Screen");
    betSelectionScreen.setBounds(320, 100, 175, 50);
    betSelectionScreen.addActionListener(new Main());
    mainPanel.add(betSelectionScreen);

    //Bet amount
        //Bet amount label
        placeBetAmount = new JLabel("Bet Amount  $");
        placeBetAmount.setBounds(50, 100, 350, 50);
        mainPanel.add(placeBetAmount);

        //bet amount text field
        betAmount = new JTextField(13);
        betAmount.setBounds(130, 113, 100, 25);
        mainPanel.add(betAmount);

    //Number to bet on
        //Number label
        numberToBetOn = new JLabel("Number To Bet On");
        numberToBetOn.setBounds(50, 150, 350, 50);
        mainPanel.add(numberToBetOn);

        //Number text field
        betNumber = new JTextField(2);
        betNumber.setBounds(160, 164, 30, 25);
        mainPanel.add(betNumber);

    //Place bet button
    placeBet = new JButton("Place Bet");
    placeBet.setBounds(50, 225, 175, 50);
    placeBet.addActionListener(new Main());
    mainPanel.add(placeBet);

    //sets frame size to betting screen size
    mainFrame.setSize(550, 355);
}

//the bet mechanics for the straight up bet type
public void straightUpBetMech() {

    //sets the betNum to string
    String betNum = betNumber.getText();
    long bet = Integer.parseInt(betAmount.getText());

    //clears the text fields
    betNumber.setText(null);
    betAmount.setText(null);

    //sets the odds for winnings math
    int odds = 35;

    //winnings math, winnings get added to the balance if you bet correctly
     long winnings = (odds * bet) + bet;

    //the wheel
    String[] theWheel = {"00", "27", "10", "25", "29", "12", "8", "19", "31", "18", "6", "21", "33", "16", "4", "23",
            "35", "14", "2", "0", "28", "9", "26", "30", "11", "7", "20", "32", "17", "5", "22", "34", "15", "3", "24", "36", "13", "1"};

    //the random number selector
    Random random1 = new Random();
    int randomNum = random1.nextInt(theWheel.length);

  //winner message
    winner = new JLabel("<html>The Ball Lands on " + theWheel[randomNum] + "<center><br>You Were Correct!<html>");
    winner.setBounds(350, 200, 300, 50);

    //loser message
    loser = new JLabel("<html>The Ball Lands on " + theWheel[randomNum] + "<center><br>You Were Incorrect!<html>");
    loser.setBounds(350, 200, 300, 50);

    //they bet correctly
    if (betNum.equals(theWheel[randomNum])) {   

         //adds winnings to your balance
        balance = balance + winnings;

        //removes stagnant balanceDisplay
        mainPanel.remove(balanceDisplay);

        //adds new, updating balanceDisplay
        balanceDisplay = new JLabel("$100");
        balanceDisplay.setText("Current Balance: $" + balance);
        balanceDisplay.setBounds(50, 70, 350, 50);
        mainPanel.add(balanceDisplay);

        //removes old winner/loser message
        mainPanel.remove(winner);
        mainPanel.remove(loser);

        mainPanel.repaint();
        mainPanel.revalidate();

        //adds new winner message
        mainPanel.add(winner);

        mainPanel.repaint();
        mainPanel.revalidate();

    //they bet incorrectly
    } else {

        //removes their bet from their balance
        balance = balance - bet;

        //removes stagnant balanceDisplay
        mainPanel.remove(balanceDisplay);

        //adds new, updating balanceDisplay
        balanceDisplay = new JLabel("$100");
        balanceDisplay.setText("Current Balance: $" + balance);
        balanceDisplay.setBounds(50, 70, 350, 50);
        mainPanel.add(balanceDisplay);

        //removes old winner/loser message
        mainPanel.remove(winner);
        mainPanel.remove(loser);

        mainPanel.repaint();
        mainPanel.revalidate();

        //adds new loser message
        mainPanel.add(loser);

        mainPanel.repaint();
        mainPanel.revalidate();
    }
}
Matthew
  • 1,905
  • 3
  • 19
  • 26
C.Hardy
  • 13
  • 4
  • 1
    Welcome to Stack Overflow. A) Your current code is too big to debug, could you take the [tour], read [ask] and create a proper [mre] that demonstrates your issue? Some tips to notice are 1) Don't use `null-layout`, see [Why is it frowned upon to use null layout](https://stackoverflow.com/q/6592468/2180785), Swing has to deal with multiple OS, PLAFs, screen sizes and resolutions, pixel perfect apps might seem like the easiest way to make UIs with Swing, but [they're not](https://stackoverflow.com/a/42521097/2180785), avoid the use of `.setBounds(...)` as well... – Frakcool Apr 27 '21 at 18:39
  • 1
    ... instead use [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html), you can even combine them. 2) Depending on your needs you could use [Card Layout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) instead of manually removing the labels. Or an even better and simpler way is to call `setText("");` on your `JLabel`, this will make it look like it "disappears" – Frakcool Apr 27 '21 at 18:41

1 Answers1

0

I dont do GUI for a long time (possible there are 100 ways better than this)

You have a lot of duplicated code and your method is too big (as your class). I didn t want to do a 100% clean code, but I think at least it´s better than it was.

Here is a piece of code that could help you:

//the bet mechanics for the straight up bet type
    public void straightUpBetMech() {

        //sets the betNum to string
        String betNum = betNumber.getText();
        long bet = Integer.parseInt(betAmount.getText());

        //clears the text fields
        betNumber.setText(null);
        betAmount.setText(null);

        //sets the odds for winnings math
        int odds = 35;

        //winnings math, winnings get added to the balance if you bet correctly
        long winnings = (odds * bet) + bet;

        //the random number selector
        int randomNum = getRandomNumber();

        //For first time
        if (balanceDisplay == null) {
            balanceDisplay = new JLabel("$100");
            balanceDisplay.setText("Current Balance: $" + balance);
            balanceDisplay.setBounds(50, 70, 350, 50);
            mainPanel.add(balanceDisplay);
        }

        // create this variable
        if (betStatus != null) {
            mainPanel.remove(betStatus);
            betStatus = null;
        }

        betStatus = new JLabel("oioi");
        betStatus.setBounds(350, 200, 300, 50);
        mainPanel.add(betStatus);


        //they bet correctly
        if (betNum.equals(String.valueOf(randomNum))) {

            //adds winnings to your balance
            balance = balance + winnings;

            //winner message
            betStatus.setText("<html>The Ball Lands on " + randomNum + "<center><br>You Were correct!<html>");

        } else {

            //removes their bet from their balance
            balance = balance - bet;

            //loser message
            betStatus.setText("<html>The Ball Lands on " + randomNum + "<center><br>You Were Incorrect!<html>");

        }
        balanceDisplay.setText("Current Balance: $" + balance);
        mainPanel.add(betStatus);

        mainPanel.repaint();
        mainPanel.revalidate();
    }


    private static int getRandomNumber() {

        // Generate numbers as String from 00 to 36
        String numbers[] = new String[38];
        numbers[0] = "00";
        for (int i = 1; i < numbers.length; i++) {
            numbers[i] = Integer.toString(i - 1);
        }

        // Generate random number
        int min = 0;
        int max = numbers.length - 1;
        return  min + (int) (Math.random() * ((max - min) + 1));

    }
Goldbones
  • 1,407
  • 3
  • 21
  • 55