我正在学习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的人来说是一个很大的学习曲线。
我的代码目前有几个小问题,但我真的希望在自己的窗口中看到它,然后纠正我的其他错误。
非常感谢。
答案 0 :(得分:1)
很好的基础知识。确保所有源文件都在一个位置。请记住,IDE编译的是.CLASS文件,而不是.JAVA文件。
http://www.cs.swarthmore.edu/~newhall/unixhelp/debuggingtips_Java.html
答案 1 :(得分:1)
首先必须构建可执行文件*。 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文件,但这本身就是一个主题。
答案 2 :(得分:1)
moonwave99有一个关于空白jframe的有效点,因为所有消息都使用System.out.println() 被发送到控制台。因此,如果您希望消息显示在您创建的jframe中,则应查找jTextArea。这是一个关于它的oracle文档的链接。 JTextArea Documentation
如果您需要为每条消息添加一个单独的对话框,您可以使用以下语句显示它。
JOptionPane.showMessageDialog(null, "My game message");