- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已分配使用老师提供的 JFrame 和卡片图像编写一个基本的 BlackJack 窗口程序。我按照我认为应该有效的方式写下了所有内容,但是当我编译它时它根本没有显示出来。我是不是错过了什么?
当我编译程序时,它成功编译了,但是,在我执行库 BlackJack.java 中的文件后,什么也没有执行,我的程序也没有显示。在命令提示符中,它显示已执行,但屏幕上没有显示 BlackJack/JFrame。
我尝试调整 BlackJack 类和 MyJFrame 类,以便 JFrame 立即执行,但它似乎不起作用。在之前的作业中,我们需要为这个 BlackJack 类编写一个 JFrame,我的工作得很好。所以我觉得 BlackJack 工具引起了问题。
注意:我们有一个名为 DeckOfCards 的 java 文件和供卡片图像使用的 gif 文件
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.分析:
让我们开始手动执行代码,代码的起点是 static void main,我可以在其中看到 MyFrame 的对象正在初始化。然后 BlackJack 的对象正在初始化。
B.问题:
newGame()
再次创建一个新的 BlackJack 对象,但您尚未为此创建自己的构造函数。我认为 MyJFrame 方法应该转换为 BlackJack 构造函数。C.可能的解决方案:
MyJFrame table= new MyJFrame();
void MyJFrame()
转换为 BlackJack 的构造函数newGame()
中,不要创建新的局部变量“first”,而是这样做:first = new BlackJack();
注意:您的代码可能有更多错误。上述解决方案可能会解决您为什么不显示框架的问题。纠正的方法不止一种,我目前只提供了最好的方法。
关于java - BlackJack 窗口程序不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39782980/
https://github.com/mattdiamond/Recorderjs/blob/master/recorder.js中的代码 我不明白 JavaScript 语法,比如 (functio
在 iOS 7 及更早版本中,如果我们想在应用程序中找到 topMostWindow,我们通常使用以下代码行 [[[UIApplication sharedApplication] windows]
我已经尝试解决这个问题很长一段时间了:我无法访问窗口的 url,因为它位于另一个域上..有一些解决方案吗? function login() { var cb = window.ope
是否可以将 FFMPEG 视频流传递到 C# 窗口?现在它在新窗口中作为新进程打开,我只是想将它传递给我自己的 SessionWindow。 此时我像这样执行ffplay: public void E
我有一个名为 x 的矩阵看起来像这样: pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2
在编译时,发生搜索,grep搜索等,Emacs会在单独的窗口中创建一个新的缓冲区来显示结果,有没有自动跳转到那个窗口的方法?这很有用,因为我可以使用 n 和 p 而不是 M-g n 和 M-g p 移
我有一个启动 PowerShell 脚本的批处理文件。 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyS
我有一个基于菜单栏的应用程序,单击图标时会显示一个窗口。在 Mac OS X Lion 上一切正常,但由于某种原因,在 Snow Leopard 和早期版本的 Mac OS X 上会出现错误。任何时候
在 macOS 中,如何在 Xcode 和/或 Interface Builder 中创建带有“集成标题栏和工具栏”的窗口? 这是“宽标题栏”类型的窗口,已添加到 OS X 10.10 Yosemit
在浏览器 (Chrome) 中 JavaScript: var DataModler = { Data: { Something: 'value' }, Process: functi
我有 3 个 html 页面。第 1 页链接到第 2 页,第 2 页链接到第 3 页(为了简单起见)。 我希望页面 2 中的链接打开页面 3 并关闭页面 1(选项卡 1)。 据我了解,您无法使用 Ja
当点击“创建节点”按钮时,如何打开一个新的框架或窗口?我希望新框架包含一个文本字段和下拉菜单,以便用户可以选择一个选项。 Create node Search node
我有一个用户控件,用于编辑应用程序中的某些对象。 我最近遇到一个实例,我想弹出一个新的对话框(窗口)来托管此用户控件。 如何实例化新窗口并将需要设置的任何属性从窗口传递到用户控件? 感谢您的宝贵时间。
我有一个Observable,它发出许多对象,我想使用window或buffer操作对这些对象进行分组。但是,我不想指定count参数来确定窗口中应包含多少个对象,而是希望能够使用自定义条件。 例如,
我有以下代码,它打开一个新的 JavaFX 阶段(我们称之为窗口)。 openAlertBox.setOnAction(e -> { AlertBox alert = AlertBox
我要添加一个“在新窗口中打开”上下文菜单项,该菜单项将以新的UIScene打开我的应用程序文档之一。当然,我只想在实际上支持多个场景的设备上显示该菜单项。 目前,我只是在检查设备是否是使用旧设备的iP
我正在尝试创建一个 AIR 应用程序来记录应用程序的使用情况,使用 AIR 从系统获取信息的唯一简单方法是使用命令行工具和抓取 标准输出 . 我知道像 这样的工具顶部 和 ps 对于 OS X,但它们
所以我有这个简单的 turtle 螺旋制作器,我想知道是否有一种方法可以打印出由该程序创建的我的设计副本。 代码: import turtle x= float(input("Angle: ")) y
我正在编写一个 C# WPF 程序,它将文本消息发送到另一个程序的窗口。我有一个宏程序作为我的键盘驱动程序 (Logitech g15) 的一部分,它已经这样做了,尽管它不会将击键直接发送到进程,而是
我尝试使用以下代码通过 UDP 发送,但得到了奇怪的结果。 if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAd
我是一名优秀的程序员,十分优秀!