- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个带有 GUI 的 Hangman 游戏,我想为其构建一个 jar。在 Netbeans 中,我转到“运行”>“清理并构建项目”,它似乎构建了 jar 得很好。然而,当我真正去运行这个 jar 时,它所做的只是在屏幕左上角打开一个小窗口,而不是我设计的 GUI。不过,当我在 Netbeans 中运行 GUI 时,它会按预期打开。此外,当我尝试关闭它打开的这个窗口时,它实际上并没有结束 java 进程 - 它会保持运行状态,直到我从任务管理器结束它。下面将提供图片和代码。
这就是我运行 jar 时打开的内容 - 一个空框架,里面什么也没有。
这是应该打开的内容,也是我从 Netbeans 内部运行项目时打开的内容。
我尝试通过命令提示符运行它,它以相同的方式运行,没有错误。这是我的代码:
HangmanGUI 类(主类):
package csci242.a03;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.io.FileNotFoundException;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
public class HangmanGUI extends javax.swing.JFrame {
/**
* A hangman object used for the creation of the game
*/
protected Hangman hang;
/**
* A JLabel for displaying the text to label the currently guessed letters
*/
private javax.swing.JLabel guessedLettersInfo;
/**
* A JLabel for displaying the text to label the number of guesses remaining
*/
private javax.swing.JLabel guessesRemainingInfo;
/**
* A JLabel for displaying the text to label the current progress of the word
*/
private javax.swing.JLabel currentStateInfo;
/**
* A JLabel for displaying the text to label where to enter the next letter
*/
private javax.swing.JLabel enterLetterInfo;
/**
* A JLabel for displaying the text which says whether the user won or loss
*/
private javax.swing.JLabel winLabel;
/**
* A JLabel for displaying what the word was if the user lost
*/
private javax.swing.JLabel wordLabel;
/**
* A JButton for starting a new game
*/
private javax.swing.JButton newGameButton;
/**
* A JLabel for displaying what letters have been guessed
*/
private javax.swing.JLabel guessedLettersDisplay;
/**
* A JLabel for displaying the number of guesses remaining
*/
private javax.swing.JLabel guessesRemainingDisplay;
/**
* A JLabel for displaying the current progress of the word being sought
*/
private javax.swing.JLabel currentStateDisplay;
/**
* A JTextField to gather input for the next guess
*/
private javax.swing.JTextField guessTextField;
/**
* A JPanel to draw the hangman on
*/
private csci242.a03.HangmanPanel1 hangmanPanel1;
/**
* A JPanel used to separate the program into two halves for layout purposes
*/
private javax.swing.JPanel rightPanel;
/**
* A JPanel used to separate the program into two halves for layout purposes
*/
private javax.swing.JPanel leftPanel;
/**
* A JPanel used as a sub-container for JTextField
*/
private javax.swing.JPanel textFieldPanel;
/**
* A JPanel used as a sub-container for winLabel and wordLabel
*/
private javax.swing.JPanel winMessagePanel;
/**
* An empty panel to take up space in the gridLayout
*/
private javax.swing.JPanel emptyPanel1;
/**
* An empty panel to take up space in the gridLayout
*/
private javax.swing.JPanel emptyPanel2;
/**
* A JPanel used as a sub-container for buttonPanel
*/
private javax.swing.JPanel buttonPanel;
/**
* A constructor for creating a new HangmanGUI frame that will house the hangman game
*
*/
public HangmanGUI() {
try {
//Create a new hangman for the game
this.hang = new Hangman(new Lexicon("lexicon.txt"), 8);
//Initialize all GUI components for the game
initComponents();
//Share the hangman object with the hangmanPanel to help draw the hangman
hangmanPanel1.setHangman(hang);
}
catch (FileNotFoundException ex) {
}
}
/**
* This method initializes all of the components used for displaying the game such
* as the outer frame and any text/buttons/textFields.
*
*/
private void initComponents() {
//Set up the outer frame
setSize(1000,550);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the frames layout to a GridLayout with 2 columns
setLayout(new GridLayout(0,2));
//Instantiate all of the components
guessedLettersInfo = new javax.swing.JLabel();
guessesRemainingInfo = new javax.swing.JLabel();
currentStateInfo = new javax.swing.JLabel();
enterLetterInfo = new javax.swing.JLabel();
winLabel = new javax.swing.JLabel();
winLabel.setVisible(false);
wordLabel = new javax.swing.JLabel();
wordLabel.setVisible(false);
newGameButton = new javax.swing.JButton();
guessedLettersDisplay = new javax.swing.JLabel();
guessesRemainingDisplay = new javax.swing.JLabel();
currentStateDisplay = new javax.swing.JLabel();
guessTextField = new javax.swing.JTextField();
hangmanPanel1 = new csci242.a03.HangmanPanel1();
rightPanel = new javax.swing.JPanel();
leftPanel = new javax.swing.JPanel();
textFieldPanel = new javax.swing.JPanel();
winMessagePanel = new javax.swing.JPanel();
emptyPanel1 = new javax.swing.JPanel();
emptyPanel2 = new javax.swing.JPanel();
buttonPanel = new javax.swing.JPanel();
//Add leftPanel and rightPanel and set each to take up half of the frame
add(leftPanel);
add(rightPanel);
leftPanel.setSize(700,225);
rightPanel.setSize(300,225);
//Set each panel's layout based on what will be inside of them
leftPanel.setLayout(new GridLayout(6,3));
rightPanel.setLayout(new GridBagLayout());
//Add guessedLettersInfo and guessedLettersDisplay to the left panel as well as set the text of each if needed
guessedLettersInfo.setText("Letters Guessed:");
leftPanel.add(guessedLettersInfo);
guessedLettersInfo.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
leftPanel.add(guessedLettersDisplay);
//Add guessesRemainingInfo and guessesRemainingDisplay to the left panel as well as set the text of each if needed
guessesRemainingInfo.setText("Guesses Remaining:");
leftPanel.add(guessesRemainingInfo);
guessesRemainingInfo.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
leftPanel.add(guessesRemainingDisplay);
//Add currentStateInfo and currentStateDisplay to the left panel as well as set the text of each if needed
currentStateInfo.setText("Current Word Progress:");
currentStateDisplay.setFont(new java.awt.Font("Tahoma", 0, 24));
leftPanel.add(currentStateInfo);
currentStateInfo.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
leftPanel.add(currentStateDisplay);
//Add enterLetterInfo to the left panel as well as set the text if needed
enterLetterInfo.setText("Enter a Letter:");
leftPanel.add(enterLetterInfo);
enterLetterInfo.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
//Set the layout of the textFieldPanel to position the textField that will be inside
textFieldPanel.setLayout(new GridBagLayout());
//Make a GridBagConstraints object for positioning the textField
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0,0,0,210);
guessTextField.setColumns(2);
//Add the textField to the panel then add the panel to the leftPanel
textFieldPanel.add(guessTextField, gbc);
leftPanel.add(textFieldPanel);
//Set the layout of the winMessagePanel to a GridLayout with 2 rows
winMessagePanel.setLayout(new GridLayout(2,0));
//Set the text of each label and add them to the winMessage Panel
winLabel.setText("Win message here");
winMessagePanel.add(winLabel);
winLabel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
wordLabel.setText("Word here");
winMessagePanel.add(wordLabel);
wordLabel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
//Add winMessagePanel to leftPanel
leftPanel.add(winMessagePanel);
//Add an emtpty panel to take up space
leftPanel.add(emptyPanel1);
//Set the layout of the buttonPanel, add newGameButton to the buttonPanel, and add buttonPanel to the leftPanel
buttonPanel.setLayout(new GridBagLayout());
newGameButton.setText("New Game");
buttonPanel.add(newGameButton);
leftPanel.add(buttonPanel);
//Add an empty panel to take up space
leftPanel.add(emptyPanel2);
//Set the insets for the hangmanPanel as well as the prefered/minimum size
gbc.insets = new Insets(0,0,-50,0);
hangmanPanel1.setPreferredSize(new Dimension(300,450));
hangmanPanel1.setMinimumSize(new Dimension(300,450));
//Add the hangmanPanel to the right Panel with the insets defined above
rightPanel.add(hangmanPanel1, gbc);
//Create a new action lister for the textField
guessTextField.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
guessTextFieldActionPerformed(evt);
}
});
//Create a new action lister for the new game button
newGameButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
newGameButtonActionPerformed(evt);
}
});
//Make the frame visible
setVisible(true);
}
/**
* This method updates the game when a user enters something in the text field.
* The display is also updated.
*
* @param evt An event passed into the action listener
*/
private void guessTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
char guess; //Make a character to hold the guess
if (!(hang.gameOver())) {
try {
//Grab the first character of whatever is entered in the textField and assign it to guess
guess = Character.toUpperCase(guessTextField.getText().charAt(0));
//Update the game
hang.update(guess);
//Update the display
updateDisplay();
//Set the textField to be empty again
guessTextField.setText("");
}
catch (StringIndexOutOfBoundsException ex) {
guessTextField.setText("");
}
}
//Repaint the hangman
hangmanPanel1.repaint();
}
/**
* This method creates a new hangman game when the hangman button is pressed
* and updates the game/display along with it.
*
* @param evt An event passed into the action listener
*/
private void newGameButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
//Make a new hangman
hang = new Hangman(new Lexicon("lexicon.txt"), 8);
//Share the new hangman with the hangmanPanel
hangmanPanel1.setHangman(hang);
guessTextField.setText("");
//Make a new character set to null used for setting up the next game
char guess = '\u0000';
//Update the game
hang.update(guess);
//Update the display for the new game
updateDisplay();
}
catch (FileNotFoundException ex) {
}
//Repaint the hangman to restart the game
hangmanPanel1.repaint();
}
/**
* This method updates the display to display the guessedLetters, guesses
* remaining, and currentState in the game. In addition to that, it also
* displays a message if the user has won or lost the game.
*
*/
public void updateDisplay() {
winLabel.setVisible(false);
wordLabel.setVisible(false);
//Update the values for guessedLetters, guessesRemaining, and currentState
guessedLettersDisplay.setText(hang.getGuessedLetters().toString());
guessesRemainingDisplay.setText(Integer.toString(hang.getIncorrectGuessesRemaining()));
currentStateDisplay.setText(hang.getCurrentState());
//Create new font objects
Font winFont = winLabel.getFont();
Font wordFont = wordLabel.getFont();
//Create a new color - the Color.GREEN is too flourescent/bright - this one shows up better in the frame
Color darkGreen = new Color(0,100,0);
//Checks to how the game is over if it has ended and displays the appropriate message
if (hang.incorrectGuesses >= hang.allowedGuesses) {
winLabel.setText("You have run out of guesses!");
winLabel.setForeground(Color.red);
winLabel.setFont(new Font(winFont.getFontName(), Font.BOLD, winFont.getSize()));
winLabel.setVisible(true);
wordLabel.setText("The word was: " + hang.getWord());
wordLabel.setFont(new Font(wordFont.getFontName(), Font.BOLD, wordFont.getSize()));
wordLabel.setVisible(true);
} else if (hang.currentState.equalsIgnoreCase(hang.getWord())){
winLabel.setText("You win!");
winLabel.setForeground(darkGreen);
winLabel.setFont(new Font(winFont.getFontName(), Font.BOLD, winFont.getSize()));
winLabel.setVisible(true);
}
}
/**
*
* @param args
*/
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 ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(HangmanGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(HangmanGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(HangmanGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(HangmanGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new HangmanGUI().setVisible(true);
}
});
}
}
绘制刽子手的面板:
package csci242.a03;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
public class HangmanPanel1 extends javax.swing.JPanel {
/**
* A hangman object used for the drawing of the hangman
*
*/
protected Hangman hang;
/**
* A constructor for creating a panel that will house the hangman and gallows
*
*/
public HangmanPanel1() {
initComponents();
}
/**
* This method sets the Hangman instance variable to a new value.
*
* @param hang A new hangman object
*/
public void setHangman(Hangman hang) {
this.hang = hang;
}
/**
* This method draws the gallows for the hangman as well as the head,
* arms, legs, and feet depending on the number of incorrect guesses that
* have been made.
*
* @param g A graphics object
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.hang != null) {
if (hang.getIncorrectGuessesMade() >= 0) {
//Draw the gallows
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(9));
g2.draw(new Line2D.Float(0,0,0,450));
g2.draw(new Line2D.Float(0,0,150,0));
g2.setStroke(new BasicStroke(5));
g2.draw(new Line2D.Float(152,0,152,50));
g2.draw(new Line2D.Float(0,350,450,350));
g2.draw(new Line2D.Float(150,350,150,450));
g2.draw(new Line2D.Float(297,350,297,450));
g2.draw(new Line2D.Float(0,350,150,450));
g2.draw(new Line2D.Float(150,351,297,450));
g2.draw(new Line2D.Float(150,351,0,450));
g2.draw(new Line2D.Float(150,450,300,350));
g2.setStroke(new BasicStroke(1));
//Draw the head/face if there is 1 incorrect guess
if (hang.getIncorrectGuessesMade() >= 1) {
g.drawOval(127,51,50,50);
g.fillOval(137,61,10,10);
g.fillOval(157,61,10,10);
g.drawLine(137, 81, 167, 81);
//Draw the torso if there are 2 incorrect guesses
if (hang.getIncorrectGuessesMade() >= 2) {
g.drawRoundRect(127,101,50,100,30,30);
//Draw the left arm if there are 3 incorrect guesses
if (hang.getIncorrectGuessesMade() >= 3) {
g.drawLine(127, 110, 100, 175);
//Draw the right arm if there are 4 incorrect guesses
if (hang.getIncorrectGuessesMade() >= 4) {
g.drawLine(177, 110, 205, 175);
//Draw the left leg if there are 5 incorrect guesses
if (hang.getIncorrectGuessesMade() >= 5) {
g.drawLine(127, 190, 127, 270);
//Draw the right leg if there are 6 incorrect guesses
if (hang.getIncorrectGuessesMade() >= 6) {
g.drawLine(177, 190, 177, 270);
//Draw the left foot if there are 7 incorrect guesses
if (hang.getIncorrectGuessesMade() >= 7) {
g.drawRoundRect(100,268,27,10,10,10);
//Draw the right foot if there are 8 incorrect guesses
if (hang.getIncorrectGuessesMade() >= 8) {
g.drawRoundRect(177,268,27,10,10,10);
}
}
}
}
}
}
}
}
}
}
}
/**
* 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() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
还有另外两个类可以解决这个问题,但我认为它们不会造成问题。由于 30,000 字符的限制,我也无法在 OP 中发帖。感谢您的帮助。
最佳答案
尝试减少您提交的内容,遗憾的是没有人愿意筛选您的代码,因此您得不到答案
关于java - Netbeans JAR 未正常运行/打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29571707/
好的,所以我想从批处理文件运行我的整个工作环境... 我想要实现什么...... 打开新的 powershell,打开我的 API 文件夹并从该文件夹运行 VS Code 编辑器(cd c:\xy;
我正在查看 Cocoa Controls 上的示例并下载了一些演示。我遇到的问题是一些例子,比如 BCTabBarController ,不会在我的设备上构建或启动。当我打开项目时,它看起来很正常,没
我刚刚开始学习 C 语言(擅长 Java 和 Python)。 当编写 C 程序(例如 hello world)时,我在 ubuntu cmd 行上使用 gcc hello.c -o hello 编译
我在 php 脚本从 cron 开始运行到超时后注意到了这个问题,但是当它从命令行手动运行时这不是问题。 (对于 CLI,PHP 默认的 max_execution_time 是 0) 所以我尝试运行
我可以使用命令行运行测试 > ./node_modules/.bin/wdio wdio.conf.js 但是如果我尝试从 IntelliJ 的运行/调试配置运行它,我会遇到各种不同的错误。 Fea
Error occurred during initialization of VM. Could not reserve enough space for object heap. Error: C
将 Anaconda 安装到 C:\ 后,我无法打开 jupyter 笔记本。无论是在带有 jupyter notebook 的 Anaconda Prompt 中还是在导航器中。我就是无法让它工作。
我遇到一个问题,如果我双击我的脚本 (.py),或者使用 IDLE 打开它,它将正确编译并运行。但是,如果我尝试在 Windows 命令行中运行脚本,请使用 C:\> "C:\Software_Dev
情况 我正在使用 mysql 数据库。查询从 phpmyadmin 和 postman 运行 但是当我从 android 发送请求时(它返回零行) 我已经记录了从 android 发送的电子邮件是正确
所以这个有点奇怪 - 为什么从 Java 运行 .exe 文件会给出不同的输出而不是直接运行 .exe。 当 java 在下面的行执行时,它会调用我构建的可与 3CX 电话系统配合使用的 .exe 文
这行代码 Environment.Is64BitProcess 当我的应用单独运行时评估为真。 但是当它在我的 Visual Studio 单元测试中运行时,相同的表达式的计算结果为 false。 我
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我写了一个使用 libpq 连接到 PostgreSQL 数据库的演示。 我尝试通过包含将 C 文件连接到 PostgreSQL #include 在我将路径添加到系统变量 I:\Program F
如何从 Jenkins 运行 Android 模拟器来运行我的测试?当我在 Execiute Windows bath 命令中写入时,运行模拟器的命令: emulator -avd Tester 然后
我已经配置好东西,这样我就可以使用 ssl 登录和访问在 nginx 上运行的 errbit 我的问题是我不知道如何设置我的 Rails 应用程序的 errbit.rb 以便我可以运行测试 nginx
我编写了 flutter 应用程序,我通过 xcode 打开了 ios 部分并且应用程序正在运行,但是当我通过 flutter build ios 通过 vscode 运行应用程序时,我得到了这个错误
我有一个简短的 python 脚本,它使用日志记录模块和 configparser 模块。我在Win7下使用PyCharm 2.7.1和Python 3.3。 当我使用 PyCharm 运行我的脚本时
我在这里遇到了一些难题。 我的开发箱是 64 位的,windows 7。我所有的项目都编译为“任何 CPU”。该项目引用了 64 位版本的第 3 方软件 当我运行不使用任何 Web 引用的单元测试时,
当我注意到以下问题时,我正在做一些 C++ 练习。给定的代码将不会在 Visual Studio 2013 或 Qt Creator 5.4.1 中运行/编译 报错: invalid types 'd
假设我有一个 easteregg.py 文件: from airflow import DAG from dateutil import parser from datetime import tim
我是一名优秀的程序员,十分优秀!