java - 单击按钮时未删除 JLabel
问题描述
我正在为学校的期末考试制作轮盘赌游戏。我正在使用 Java 和 Swing,我正在编写 GUI,而不是使用拖放软件。我很难弄清楚为什么 aJLabel
没有被删除。
在投注屏幕上,我希望有一条消息显示您是赢还是输以及球落在了哪个号码上。我的问题是,当您进行第二次下注时,之前的输赢混乱并没有消失,新的赌注会重叠在上面。我的问题出在straightUpBetMech()
方法上。
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();
}
}
解决方案
我很长时间不做 GUI(可能有 100 种方法比这更好)
您有很多重复的代码,并且您的方法太大(作为您的类)。我不想编写 100% 干净的代码,但我认为至少它比以前更好。
这是一段可以帮助您的代码:
//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));
}
推荐阅读
- qt - QBitArray 中的 data_ptr()->data() 是什么意思?
- python - 是否可以生成一种算法,可以在 Python 中求解具有二进制变量的线性方程组?
- jestjs - 开玩笑,React 测试组件不存在
- tinymce - 加载内容css失败://www.tinymce.com/css/codepen.min.css
- ios - 在 acOS CATALINA 中安装 cocoapod 时出错
- javascript - 如何在没有 Prop Drilling 的情况下触发兄弟组件的功能?
- python - 显示查询集的每个元素,用
django 模板的内部分隔 - python - nohup 无法获得输出
- python - 新手问题 Python:为什么索引值会发生变化?
- r - Rcpp(具有默认空参数的函数):与请求的类型不兼容:[type=NULL; 目标=双]