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();
}
}