- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
PokerFrame(从查看器类调用)工作得很好,但是一旦我尝试将其转换为小程序,它就无法加载。我没有得到任何运行时异常,只是一个空白的小程序空间。直到几个小时后我才能回复/选择答案,但我真的很感激任何人可能有的见解......
<HTML><head></head>
<body>
<applet code="PokerApplet.class" width="600" height="350"> </applet>
</body>
</HTML>
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class PokerApplet extends JApplet {
public void init() {
final JApplet myApplet = this;
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JPanel frame = new PokerFrame();
frame.setVisible(true);
myApplet.getContentPane().add(frame);
}
});
}
catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
import java.awt.Color;
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.net.MalformedURLException;
/** A class that displays a poker game in a JFrame. */
public class PokerFrame extends JPanel {
public static final int HEIGHT = 350;
public static final int WIDTH = 600;
private static final int BUTTON_HEIGHT = 50;
private static final int BUTTON_WIDTH = 125;
// the following variables must be kept out as instance variables for scope reasons
private Deck deck;
private Hand hand;
private int[] handCount; // for keeping track of how many hands of each type player had
private int gamesPlayed;
private int playerScore;
private String message; // to Player
private boolean reviewMode; // determines state of game for conditional in doneBttn listener
private JLabel scoreLabel;
private JLabel gamesPlayedLabel;
private JLabel msgLabel;
private JLabel cardImg1;
private JLabel cardImg2;
private JLabel cardImg3;
private JLabel cardImg4;
private JLabel cardImg5;
/** Creates a new PokerFrame object. */
public PokerFrame() {
this.setSize(WIDTH, HEIGHT);
// this.setTitle("Poker");
gamesPlayed = 0;
playerScore = 0;
handCount = new int[10]; // 10 types of hands possible, including empty hand
message = "<HTML>Thanks for playing poker!"
+ "<br>You will be debited 1 point"
+ "<br>for every new game you start."
+ "<br>Click \"done\" to begin playing.</HTML>";
reviewMode = true;
this.add(createOuterPanel());
deck = new Deck();
hand = new Hand();
}
/** Creates the GUI. */
private JPanel createOuterPanel() {
JPanel outerPanel = new JPanel(new GridLayout(2, 1));
outerPanel.add(createControlPanel());
outerPanel.add(createHandPanel());
return outerPanel;
}
/** Creates the controlPanel */
private JPanel createControlPanel() {
JPanel controlPanel = new JPanel(new GridLayout(1, 2));
controlPanel.add(createMessagePanel());
controlPanel.add(createRightControlPanel());
return controlPanel;
}
/** Creates the message panel */
private JPanel createMessagePanel() {
JLabel msgHeaderLabel = new JLabel("<HTML>GAME STATUS:<br></HTML>");
msgLabel = new JLabel(message);
JPanel messagePanel = new JPanel();
messagePanel.add(msgHeaderLabel);
messagePanel.add(msgLabel);
return messagePanel;
}
/** Creates the right side of the control panel. */
private JPanel createRightControlPanel() {
scoreLabel = new JLabel("Score: 0");
gamesPlayedLabel = new JLabel("Games Played: 0");
JPanel labelPanel = new JPanel(new GridLayout(2, 1));
labelPanel.add(scoreLabel);
labelPanel.add(gamesPlayedLabel);
JButton doneBttn = new JButton("Done");
doneBttn.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
class DoneListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (reviewMode) {
reviewMode = false;
startNewHand();
return;
}
else {
reviewMode = true;
while (!hand.isFull()) {
hand.add(deck.dealCard());
}
updateCardImgs();
score();
}
}
}
ActionListener doneListener = new DoneListener();
doneBttn.addActionListener(doneListener);
JPanel donePanel = new JPanel();
donePanel.add(doneBttn);
// stats button!
JButton statsBttn = new JButton("Statistics");
statsBttn.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
final JPanel myFrame = this;
class StatsListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// add stats pop window functionality after changing score() method to keep track of that stuff
double numGames = gamesPlayed;
String popupText = "<HTML>You've played " + gamesPlayed + " games. This is how your luck has played out:<br>"
+ "<table>"
+ "<tr><td>HAND DESCRIPTION</td>PERCENTAGE</td></tr>"
+ "<tr><td>royal flush</td><td>" + getPercentage(0) + "</td></tr>"
+ "<tr><td>straight flush</td><td>" + getPercentage(1) + "</td></tr>"
+ "<tr><td>four of a kind</td><td>" + getPercentage(2) + "</td></tr>"
+ "<tr><td>full house</td><td>" + getPercentage(3) + "</td></tr>"
+ "<tr><td>straight</td><td>" + getPercentage(4) + "</td></tr>"
+ "<tr><td>four of a kind</td><td>" + getPercentage(5) + "</td></tr>"
+ "<tr><td>three of a kind</td><td>" + getPercentage(6) + "</td></tr>"
+ "<tr><td>two pair</td><td>" + getPercentage(7) + "</td></tr>"
+ "<tr><td>pair of jacks or better</td><td>" + getPercentage(8) + "</td></tr>"
+ "<tr><td>empty hand</td><td>" + getPercentage(9) + "</td></tr>"
+ "</table></HTML>";
JOptionPane.showMessageDialog(myFrame, popupText, "Statistics", 1);
}
private double getPercentage(int x) {
double numGames = gamesPlayed;
double percentage = handCount[x] / numGames * 100;
percentage = Math.round(percentage * 100) / 100;
return percentage;
}
}
ActionListener statsListener = new StatsListener();
statsBttn.addActionListener(statsListener);
JPanel statsPanel = new JPanel();
statsPanel.add(statsBttn);
JPanel bttnPanel = new JPanel(new GridLayout(1, 2));
bttnPanel.add(donePanel);
bttnPanel.add(statsPanel);
JPanel bottomRightControlPanel = new JPanel(new GridLayout(1,2));
bottomRightControlPanel.add(bttnPanel);
JPanel rightControlPanel = new JPanel(new GridLayout(2, 1));
rightControlPanel.add(labelPanel);
rightControlPanel.add(bottomRightControlPanel);
return rightControlPanel;
}
/** Creates the handPanel */
private JPanel createHandPanel() {
JPanel handPanel = new JPanel(new GridLayout(1, Hand.CARDS_IN_HAND));
JPanel[] cardPanels = createCardPanels();
for (JPanel each : cardPanels) {
handPanel.add(each);
}
return handPanel;
}
/** Creates the panel to view and modify the hand. */
private JPanel[] createCardPanels() {
JPanel[] panelArray = new JPanel[Hand.CARDS_IN_HAND];
class RejectListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (reviewMode) return;
// find out which # button triggered the listener
JButton thisBttn = (JButton) event.getSource();
String text = thisBttn.getText();
int cardIndex = Integer.parseInt(text.substring(text.length() - 1));
hand.reject(cardIndex-1);
switch (cardIndex) {
case 1:
cardImg1.setIcon(null);
cardImg1.repaint();
break;
case 2:
cardImg2.setIcon(null);
cardImg2.repaint();
break;
case 3:
cardImg3.setIcon(null);
cardImg3.repaint();
break;
case 4:
cardImg4.setIcon(null);
cardImg4.repaint();
break;
case 5:
cardImg5.setIcon(null);
cardImg5.repaint();
break;
}
}
}
ActionListener rejectListener = new RejectListener();
for (int i = 1; i <= Hand.CARDS_IN_HAND; i++) {
JLabel tempCardImg = new JLabel();
try {
tempCardImg.setIcon(new ImageIcon(new URL("http://erikaonearth.com/evergreen/cards/top.jpg")));
}
catch (MalformedURLException e) {
e.printStackTrace();
}
String bttnText = "Reject #" + i;
JButton rejectBttn = new JButton(bttnText);
rejectBttn.addActionListener(rejectListener);
switch (i) {
case 1:
cardImg1 = tempCardImg;
case 2:
cardImg2 = tempCardImg;
case 3:
cardImg3 = tempCardImg;
case 4:
cardImg4 = tempCardImg;
case 5:
cardImg5 = tempCardImg;
}
JPanel tempPanel = new JPanel(new BorderLayout());
tempPanel.add(tempCardImg, BorderLayout.CENTER);
tempPanel.add(rejectBttn, BorderLayout.SOUTH);
panelArray[i-1] = tempPanel;
}
return panelArray;
}
/** Clears the hand, debits the score 1 point (buy in),
* refills and shuffles the deck, and then deals until the hand is full. */
private void startNewHand() {
playerScore--;
gamesPlayed++;
message = "<HTML>You have been dealt a new hand.<br>Reject cards,\nthen click \"done\"<br>to get new ones and score your hand.";
hand.clear();
deck.shuffle();
while (!hand.isFull()) {
hand.add(deck.dealCard());
}
updateCardImgs();
updateLabels();
}
/** Updates the score and gamesPlayed labels. */
private void updateLabels() {
scoreLabel.setText("Score: " + playerScore);
gamesPlayedLabel.setText("Games played: " + gamesPlayed);
msgLabel.setText(message);
}
/** Updates the card images. */
private void updateCardImgs() {
try {
String host = "http://erikaonearth.com/evergreen/cards/";
String ext = ".jpg";
cardImg1.setIcon(new ImageIcon(new URL(host + hand.getCardAt(0).toString() + ext)));
cardImg2.setIcon(new ImageIcon(new URL(host + hand.getCardAt(1).toString() + ext)));
cardImg3.setIcon(new ImageIcon(new URL(host + hand.getCardAt(2).toString() + ext)));
cardImg4.setIcon(new ImageIcon(new URL(host + hand.getCardAt(3).toString() + ext)));
cardImg5.setIcon(new ImageIcon(new URL(host + hand.getCardAt(4).toString() + ext)));
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
/** Fills any open spots in the hand. */
private void fillHand() {
while (!hand.isFull()) {
hand.add(deck.dealCard());
updateCardImgs();
}
}
/** Scores the hand.
* @return a string with message to be displayed to player
* (Precondition: hand must be full) */
private void score() {
String handScore = hand.score();
int pointsEarned = 0;
String article = "a ";
if (handScore.equals("royal flush")) {
handCount[0]++;
pointsEarned = 250;
}
else if (handScore.equals("straight flush")) {
handCount[1]++;
pointsEarned = 50;
}
else if (handScore.equals("four of a kind")) {
handCount[2]++;
pointsEarned = 25;
article = "";
}
else if (handScore.equals("full house")) {
handCount[3]++;
pointsEarned = 6;
}
else if (handScore.equals("flush")) {
handCount[4]++;
pointsEarned = 5;
}
else if (handScore.equals("straight")) {
handCount[5]++;
pointsEarned = 4;
}
else if (handScore.equals("three of a kind")) {
handCount[6]++;
pointsEarned = 3; article = "";
}
else if (handScore.equals("two pair")) {
handCount[7]++;
pointsEarned = 2;
article = "";
}
else if (handScore.equals("pair of jacks or better")) {
handCount[8]++;
pointsEarned = 1;
}
else if (handScore.equals("empty hand")) {
handCount[9]++;
article = "an ";
}
playerScore = playerScore + pointsEarned;
message = "<HTML>You had " + article + handScore + ",<br>which earned you " + pointsEarned + " points.<br>Click \"done\" to start a new hand.";
updateLabels();
}
}
最佳答案
我可以看到您的代码中有两个弱点。
如果您的小程序由多个类组成,您应该将 codebase
属性添加到您的 applet
标记中,以让它知道在哪里可以找到它们。例如:
<applet code="PokerApplet.class"
codebase="http://localhost/classes" width="600" height="350">
</applet>
因此,在此示例中,您的 PockerApplet.class、PokerFrame.class
和其他使用的类应该位于 classes
文件夹中。
如果您不使用codebase
属性,那么您的类应该位于带有applet
标记的html页面所在的同一文件夹中。
你考虑这个吗?
你的小程序init()
方法看起来很奇怪。尝试使用以下变体:
public void init() {
// Bad idea: the applet is not initialized yet
// but you already use link to its instance.
//final JApplet myApplet = this;
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JPanel frame = new PokerFrame();
//frame.setVisible(true); // You don't need this.
// You don't need myApplet variable
// to call getContentPane() method.
//myApplet.getContentPane().add(frame);
getContentPane().add(frame);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
添加SSCCE后您可以获得更多帮助您的问题的代码。
关于java - 转换为 JApplet —— 我哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6273281/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!