BlackJack窗口程序不会显示
问题描述:
我已经分配了使用JFrame和我的老师提供的卡片图像来编写基本的BlackJack窗口程序。我从我认为应该工作的地方写出所有东西,但是当我编译它时根本没有出现。我错过了什么吗?BlackJack窗口程序不会显示
当我编译程序成功编译,但是,当我执行我的库BlackJack.java中的文件后,没有执行任何操作,我的程序也没有显示出来。在命令提示符中,它显示它已执行但屏幕上没有显示BlackJack/JFrame。
我已经尝试调整BlackJack Classes和MyJFrame类,以便JFrame立即执行,但它似乎不工作。 而在之前的作业中,我们被要求为这个BlackJack类写一个JFrame,我的工作很好。所以我觉得像BlackJack的实现导致了问题。
注:我们所给出称为DeckOfCards和gif文件名片图像的Java文件使用
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;
public class BlackJack extends JFrame implements ActionListener
{
private JLabel[] lblPlayer = new JLabel[8];
private JLabel[] lblDealer = new JLabel[8];
private JPanel pnlPlayDeal = new JPanel(new GridLayout(2,8));
private DeckOfCards deck = new DeckOfCards();
private Vector <String> playerCardNames = new Vector<String>();
private Vector <String> dealerCardNames = new Vector<String>();
private ImageIcon[] icoPlayerHand = new ImageIcon[8];
private ImageIcon[] icoDealerHand = new ImageIcon[8];
private JButton btnDeal = new JButton("Deal");
private JButton btnPlayer = new JButton("Player");
private JButton btnDealer = new JButton("Dealer");
private JButton btnNew = new JButton("New");
private JButton btnAuthor = new JButton("Author");
private JPanel pnlButtons = new JPanel(new FlowLayout());
private int count = 1;
private int playerval = 0;
private int dealerval = 0;
public void MyJFrame()
{
for(int i=0;i<8;i++)
{
lblPlayer[i] = new JLabel("Player");
lblDealer[i] = new JLabel("Dealer");
}
this.setLocationRelativeTo(null);
this.setSize(800, 350);
this.setVisible(true);
add(pnlPlayDeal,BorderLayout.CENTER);
add(pnlButtons,BorderLayout.SOUTH);
this.addLabels();
this.addButtons();
registerListeners();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addLabels()
{
for(int i= 0;i<8;i++)
{
pnlPlayDeal.add(lblPlayer[i]);
}
for(int i= 0;i<8;i++)
pnlPlayDeal.add(lblDealer[i]);
}
public void addButtons()
{
pnlButtons.add(btnDeal);
pnlButtons.add(btnPlayer);
btnPlayer.setEnabled(false);
pnlButtons.add(btnDealer);
btnDealer.setEnabled(false);
pnlButtons.add(btnNew);
btnNew.setEnabled(false);
pnlButtons.add(btnAuthor);
}
public void registerListeners()
{
btnDeal.addActionListener(this);
btnPlayer.addActionListener(this);
btnDealer.addActionListener(this);
btnNew.addActionListener(this);
btnAuthor.addActionListener(this);
}
public int findRank(String x)
{
int result=0;
if(x.startsWith("Two"))
result=2;
else if(x.startsWith("Three"))
result=3;
else if(x.startsWith("Four"))
result=4;
else if(x.startsWith("Five"))
result=5;
else if(x.startsWith("Six"))
result=6;
else if(x.startsWith("Seven"))
result=7;
else if(x.startsWith("Eight"))
result=8;
else if(x.startsWith("Nine"))
result=9;
else if(x.startsWith("Ten"))
result=10;
else if(x.startsWith("Jack"))
result=10;
else if(x.startsWith("Queen"))
result=10;
else if(x.startsWith("King"))
result=10;
else if(x.startsWith("Ace"))
result=11;
return result;
}
public int getVal (Vector<String> v)
{
int total=0;
Iterator <String> iter = v.iterator();
while(iter.hasNext())
{
total+= findRank(iter.next());
}
return total;
}
public void deal()
{
deck.shuffle();
playerCardNames.add(deck.deal());
playerCardNames.add(deck.deal());
dealerCardNames.add(deck.deal());
dealerCardNames.add(deck.deal());
icoPlayerHand[0] = new ImageIcon("cardImages/"
+playerCardNames.get(0)+".gif");
icoPlayerHand[1] = new ImageIcon("cardImages/"
+playerCardNames.get(1)+".gif");
lblPlayer[0].setIcon(icoPlayerHand[0]);
lblPlayer[1].setIcon(icoPlayerHand[1]);
icoDealerHand[0] = new ImageIcon("cardImages/"+
dealerCardNames.get(0)+".gif");
icoDealerHand[1] = new ImageIcon("cardImages/card.gif");
lblDealer[0].setIcon(icoDealerHand[0]);
lblDealer[1].setIcon(new ImageIcon("cardImages/card.gif"));
btnDeal.setEnabled(false);
btnPlayer.setEnabled(true);
count ++;
}
public void player()
{
while(getVal(playerCardNames)<22)
{
String choice = JOptionPane.showInputDialog(null, "you have "
+getVal(playerCardNames)+" h/s",null);
if(choice.equalsIgnoreCase("h"))
{
playerCardNames.add(deck.deal());
icoPlayerHand[count] = new ImageIcon("cardImages/"
+playerCardNames.lastElement()+".gif");
lblPlayer[count].setIcon(icoPlayerHand[count]);
count++;
}
else
break;
}
if(getVal(playerCardNames)>21)
{
JOptionPane.showMessageDialog(this, "You Busted");
btnPlayer.setEnabled(false);
btnNew.setEnabled(true);
}
else
{
btnPlayer.setEnabled(false);
btnDealer.setEnabled(true);
}
}
public void dealer()
{
btnDealer.setEnabled(false);
count=1;
icoDealerHand[count] = new ImageIcon("cardImages/"
+dealerCardNames.lastElement()+".gif");
lblDealer[1].setIcon(icoDealerHand[count]);
count ++;
while(getVal(dealerCardNames)<17)
{
dealerCardNames.add(deck.deal());
icoDealerHand[count] = new ImageIcon("cardImages/"
+dealerCardNames.lastElement()+".gif");
lblDealer[count].setIcon(icoDealerHand[count]);
count ++;
}
if(getVal(dealerCardNames)>21)
JOptionPane.showMessageDialog(this, "Dealer bust you won");
else
whoWon();
btnNew.setEnabled(true);
}
public void whoWon()
{
if(getVal(playerCardNames)>getVal(dealerCardNames))
JOptionPane.showMessageDialog(this,"You won");
else
JOptionPane.showMessageDialog(this,"You lost");
}
public void newGame()
{
this.setVisible(false);
BlackJack game = new BlackJack();
}
public static void main(String[]args)
{
MyJFrame table= new MyJFrame();
BlackJack first = new BlackJack();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnAuthor)
((JButton)e.getSource()).setText("Conner M");
if(e.getSource()==btnDeal)
deal();
if(e.getSource()==btnPlayer)
player();
if(e.getSource()==btnDealer)
dealer();
if(e.getSource()==btnNew)
{
newGame();
}
}
}
答
A.分析:
让我们开始手工执行你的代码,代码的起点是静态无效主,我可以看到MyFrame的对象正在初始化。然后BlackJack的对象正在初始化。
B.问题:
- 有你已经在你的代码中声明没有MyFrame类。
- 二十一点延伸JFrame,所以你需要通过在MyJFrame()方法(而不是在BlackJack的构造函数)中完成的setVisible()方法来设置其引用可见。
- 该方法,
newGame()
再次创建一个新的BlackJack对象,但是您尚未为此创建自己的构造函数。我相信方法MyJFrame应该转换成BlackJack构造函数。
下可能的解决方案:
- 从静态无效的主要()
- 转换方法
void MyJFrame()
在主删除MyJFrame table= new MyJFrame();
酒杯的构造 - 而是 '第一' 创建本地的BlackJack变量()方法,使它成为一个类成员(在main()之外声明它)并在main()中初始化它。
- 大酒杯的变量是类成员现在,在方法
newGame()
,而不是创建一个新的局部变量“第一”,而是执行此操作:first = new BlackJack();
注意:您的代码可能有更多错误。上述解决方案可以解决你的问题,为什么它不显示框架。有不止一种方法来纠正它,我现在只提供了最好的方法。
嗨康纳,欢迎来到StackOverflow。您的问题没有适合我们的信息来帮助您。例如我们不知道“编译时根本没有出现”的意思,而且您还没有告诉我们您已经做了什么来尝试解决这个问题。阅读http:// stackoverflow。com/help/how-to-ask,然后用下面的方法更新你的问题:**你试图实现的目标,你试图做什么来解决它,你怎么知道它不工作。** – Tim
简化你的例子,你只是想展示一个JFrame。一旦你有了这些工作,就可以添加更多的功能层。你可以找到简单的在Web上显示JFrame的例子,例如:http://alvinalexander.com/java/jframe-example当你setVisible(true )以及示例设置它的时间和地点。 –
请看一些[调试技巧](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/),可以帮助你在这里。如果它提出了一个你不确定的具体问题,那么可以随时回答问题。 –