user3068364
user3068364

Reputation: 31

Netbeans JAR not running/opening properly

I made a Hangman game with a GUI and I would like to build a jar for it. In Netbeans I go to Run>Clean and Build Project and it seems to build the jar just fine. However when I actually go to run the jar all it does is open a tiny window in the top left of my screen and not the GUI that I designed. Though when I run the GUI in Netbeans it opens as it should. Also when I try to close this window that it opened, it doesn't actually end the java process - it stays running until I end it from the task manager. Pictures and code will be provided below.

This is what opens when I run the jar - an empty frame with nothing in it.

This is what opens when I run the jar - an empty frame with nothing in it.

This is what should open and it is what opens when I run the project from inside Netbeans.

This is what should open

I've tried running it through the command prompt and it runs the same way with no errors. Here is my code:

The HangmanGUI class (the main class):

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);
        }
    });
}
}

The panel on which the Hangman is drawn:

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                   
}

There are two other classes for this, but I don't think they are causing the problem. I also can't post in the OP because of the 30,000 character limit. Thanks for any help.

Upvotes: 1

Views: 231

Answers (1)

GTR SEVER
GTR SEVER

Reputation: 15

Try to whittle down what you submit unfortunately no one wants to sift throught your code so you're not getting answers

Upvotes: 1

Related Questions