简体   繁体   中英

How can I get my program to run within a window in Java?

I am learning Java and figured that the best way to try and learn was to create a small text adventure that actually incorporates certain functions that I am actually trying to learn.

So, I have slowly been building my little game by trial and error and I am now at a stage where I would like to display it within an actual window and not within the IDE itself.

Here is the class which is called RoleGame:

package rolegame;

import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class RoleGame extends JFrame{


public static void main(String[] args){
    RoleGame RoleGameObject = new RoleGame();
    RoleGameObject.roleGame();

}



public void roleGame() {
    JPanel roleGame = new JPanel();
    add(roleGame);
    this.setSize(1000, 1000);
    // pack();
    setVisible(true);



    TheFellowship TheFellowshipObject = new TheFellowship();
    TheFellowshipObject.yourParty();
    System.out.println();
    FellowshipInventory FellowshipInventoryObject = new FellowshipInventory();
    FellowshipInventoryObject.inventory();
    FellowshipAttributes FellowshipAttributesObject = new FellowshipAttributes();
    FellowshipAttributesObject.characterAttributes();


    Scanner in = new Scanner(System.in);

    System.out.println();
    System.out.println("You face a horde of terrible orcs...\nWhat are you going to do?\n\nYour options are as follows:\n ");
    System.out.println("Roll a 1 to run...\nRoll a 2 to stand your ground...");
    System.out.println("Roll a 3 to attack...\nRoll a 4 to attack the leader...");
    System.out.println("Roll a 5 to have your whole party attack...\nRoll a 6 to kill them all...");
    System.out.println();
    System.out.print("Press return to roll the dice!!");
    in.nextLine();

    //System.out.println("You rolled a "+ num());
    for(int i = 0; i < 1; i++){
        int num = (int) (Math.random() * 6) + 1;
        System.out.println("You rolled a " + num + "\n");
    if(num == 1){
        System.out.println("You run away to fight another day when the odds are better...");
        TheHorde TheHordeObject = new TheHorde();
        TheHordeObject.youRun();
    }
    else if(num == 2)
    {
        System.out.println("You stand your ground before the mighty horde...");
        TheHorde TheHordeObject = new TheHorde();
        TheHordeObject.standGround();
    }
    else if(num == 3)
    {
        System.out.println("You run towards your foe screaming at them as you go...");
        TheHorde TheHordeObject = new TheHorde();
        TheHordeObject.attack();
    }
    else if(num == 4)
    {
        System.out.println("You seek out the horde leader and make for him with your sword swinging...");
        TheHorde TheHordeObject = new TheHorde();
        TheHordeObject.attackLeader();
    }
    else if(num == 5)
    {
        System.out.println("You rally your party by shouting for their assistance to slain the horde...");
        TheHorde TheHordeObject = new TheHorde();
        TheHordeObject.rally();
    }
    else if(num == 6)
    {
        System.out.println("You and your party storm the horde and slain them all and cry    victory...!!");
        TheHorde TheHordeObject = new TheHorde();
        TheHordeObject.killThem();
    }
    Fighting FightingObject = new Fighting();
    FightingObject.fight();

    }

  }

}

First of all, please accept my apologies for the primitive code. I am sure you can appreciate that we all start at the bottom and this is a large learning curve for someone learning Java.

I have a couple of small issues with my code at the moment but I would really like to see it within its own window and then correct my other errors after.

Many thanks.

Nice covering of the basics. Make sure all of your source files are in one place. Remember, it is the .CLASS files that the IDE compiles that you want to run, not the .JAVA files.

http://www.cs.swarthmore.edu/~newhall/unixhelp/debuggingtips_Java.html

You first have to build an executable *. jar file. Doing that depends from the IDE, for example this is in Eclipse:

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm

In Netbeans: http://www.wikihow.com/Generate-JAR-File-in-Netbeans

And you can run the jar file in the command line using:

java -jar <your_jar_file>.jar

You can also generate an exe file out of that, but that is a topic on its own.

moonwave99 has a valid point about the blank jframe since all your messages using System.out.println() are sent to the console. So, if you want your messages to appear in the jframe you created, you should look up jTextArea. Here is a link to the oracle documentation on it. JTextArea Documentation

If you need a seperate dialogue box for each message, you could display it with the following statement.

    JOptionPane.showMessageDialog(null, "My game message");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM