[英]Why does my Java program open two instances / windows?
I am trying to make a simple BlackJack game/project for school.我正在尝试为学校制作一个简单的二十一点游戏/项目。 So far when I run it - everything goes well, but two instances / JFrame windows are opened instead of just one.到目前为止,当我运行它时 - 一切顺利,但打开了两个实例/JFrame 窗口而不是一个。 Here is the code for the gameloop which is in a class called (BlackJackProject).这是游戏循环的代码,它位于一个名为 (BlackJackProject) 的类中。 Below this code i have also put the code for the JFrame form/file which is opening two windows.在此代码下方,我还放置了打开两个窗口的 JFrame 表单/文件的代码。
public class BlackJackProject {
public static int final_total_cards = 0;
public static int total_cards = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int turns = 3;
boolean isGameOver = false;
while(isGameOver = true){
turns --;
if (turns > 0){
Hit_Or_Stand.main(null);
}
else{
break;
}
}
}
}
Here is the code for the file which is opening two windows这是打开两个窗口的文件的代码
public class Hit_Or_Stand extends javax.swing.JFrame {
int turns = 3;
String name = null;
int balance = 5000;
int bet_amount = 0;
String name_and_choice_question = JOptionPane.showInputDialog(null, "What is your name?");
String ask_bet_amount = JOptionPane.showInputDialog(null, "Your balance is: " + balance + "Enter your bet amount");
public Hit_Or_Stand() {
initComponents();
lblOption.setText(name_and_choice_question + ", what would you like to do?");
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
lblOption = new javax.swing.JLabel();
btn_Hit = new javax.swing.JButton();
btn_Stand = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lblOption.setText("da");
btn_Hit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
btn_Hit.setText("Hit");
btn_Hit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_HitActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(btn_Hit, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGap(175, 175, 175)
.addComponent(lblOption)))
.addContainerGap(299, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(lblOption)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 308, Short.MAX_VALUE)
.addComponent(btn_Hit, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
);
btn_Stand.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
btn_Stand.setText("Stand");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_Stand, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 81, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btn_Stand, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 101, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btn_HitActionPerformed(java.awt.event.ActionEvent evt) {
if (final_total_cards < 21){
int card_draw = (int)(Math.random() * 6 + 1);
total_cards = total_cards + card_draw;
final_total_cards = total_cards + card_draw;
JOptionPane.showMessageDialog(null, "Your total card value is: " + final_total_cards);
}else if (final_total_cards == 21){
JOptionPane.showMessageDialog(null, "Your total card value is: " + final_total_cards);
JOptionPane.showMessageDialog(null, "You win!");
balance = balance + Integer.parseInt(ask_bet_amount);
JOptionPane.showMessageDialog(null, "Your final balance is : " + balance);
this.dispose();
}
else if (final_total_cards > 21){
JOptionPane.showMessageDialog(null, "Your total card value is: " + final_total_cards);
JOptionPane.showMessageDialog(null, "You lose!");
balance = balance - Integer.parseInt(ask_bet_amount);
JOptionPane.showMessageDialog(null, "Your final balance is: " + balance);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Hit_Or_Stand.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Hit_Or_Stand.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Hit_Or_Stand.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Hit_Or_Stand.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Hit_Or_Stand().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_Hit;
private javax.swing.JButton btn_Stand;
private javax.swing.JPanel jPanel1;
public static javax.swing.JLabel lblOption;
// End of variables declaration
} }
The problem is this block:问题是这个块:
while(isGameOver = true){
turns --;
if (turns > 0){
Hit_Or_Stand.main(null);
}
else{
break;
}
}
A couple of issues:几个问题:
while (isGameOver = true)
You're not comparing but assigning isGameOver
to true
.您不是在比较,而是将isGameOver
分配给true
。 This should be ==
but in that case you'd never enter the loop because you instantiated the variable to false
and never change the value.这应该是==
但在那种情况下,您永远不会进入循环,因为您将变量实例化为false
并且永远不会更改该值。
turns --;
if (turns > 0){
Hit_Or_Stand.main(null);
}
else{
break;
}
You start with turns = 3
and decrease it (new value 2
).您从turns = 3
开始并减少它(新值2
)。 Hit_Or_Stand.main(null)
will return immediately after it's setting up the JFrame
. Hit_Or_Stand.main(null)
将在设置JFrame
后立即返回。 So you will have the next iteration immediately, decrease turns
again (now it's 1
), open another window.所以你将立即进行下一次迭代,再次减少turns
(现在是1
),打开另一个窗口。 After that turns
is decreased again (new value is now 0
) and because turns
is now no longer > 1
you leave the while
-loop.在此之后, turns
数再次减少(新值现在是0
)并且因为turns
数现在不再> 1
您离开了while
循环。
To tell you more about how to solve that problem, depends on what you actually want to achieve, so you might edit your question accordingly.要告诉您有关如何解决该问题的更多信息,取决于您实际想要实现的目标,因此您可以相应地编辑您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.