我正在学习 Java,并认为尝试和学习的最佳方法是创建一个小型文本冒险,它实际上包含了我实际上正在尝试学习的某些功能。
所以,我一直在通过反复试验慢慢构建我的小游戏,现在我希望在实际窗口中而不是在 IDE 本身中显示它。
这是名为 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();
}
}
}
首先,请接受我对原始代码的歉意。我相信您会理解我们都是从底层开始的,这对于学习 Java 的人来说是一个很大的学习曲线。
目前我的代码有一些小问题,但我真的很想在自己的窗口中看到它,然后纠正我的其他错误。
非常感谢。
最佳答案
您首先必须构建一个可执行文件 *. jar 文件。执行此操作取决于 IDE,例如在 Eclipse 中:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm
在 Netbeans 中: http://www.wikihow.com/Generate-JAR-File-in-Netbeans
您可以使用以下命令在命令行中运行 jar 文件:
java -jar <your_jar_file>.jar
您还可以从中生成一个 exe 文件,但这本身就是一个主题。
关于java - 如何让我的程序在 Java 窗口中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20792515/