简体   繁体   English

将jframe类中的文本字段中的值存储在另一个类中的对象中

[英]Storing values from textfields in jframe class in a object in another class

I'm quite new to programming and java in general. 一般来说,我对编程和Java还是很陌生。 I am currently a student and I'm trying to do a project outside of the classroom to make myself more proficient in java. 我目前是一名学生,我正尝试在课堂外做一个项目,以使自己更加精通Java。 Currently, I am working on a text-based RPG where I am trying to create the main character, assign the basic skill points to the character and use that info throughout the game. 目前,我正在研究基于文本的RPG,我试图在其中创建主要角色,为角色分配基本技能点并在整个游戏中使用该信息。 So far, I've made a jframe class where the user has 6 different skills with a base value of 1. The user can add or deduct a value from the certain skill with plus and minus buttons. 到目前为止,我已经制作了一个jframe类,其中用户具有6个基本值为1的不同技能。用户可以使用加号和减号按钮从特定技能中添加或减去一个值。 And here is where I got stuck. 这就是我卡住的地方。 I don't know how to save these values to an object which can be used by other classes. 我不知道如何将这些值保存到其他类可以使用的对象。 I would really appreciate any help I could get. 我会很感激我能得到的任何帮助。 Thank you. 谢谢。

public class CharacterStatsGUI extends javax.swing.JFrame {

static int skill1 = 1;
static int skill2 = 1;
static int skill3 = 1;
static int skill4 = 1;
static int skill5 = 1;
static int skill6 = 1;

static int skillPoints = 10;

private void txthealthActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    txthealth.setText(String.valueOf(skill1));
}                                         

private void btnhpActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    if(skillPoints >= 1){
    skill1++;
    skillPoints--;
    txthealth.setText(String.valueOf(skill1));
    txtpoints.setText(String.valueOf(skillPoints));
    }
    else{
        JOptionPane.showMessageDialog(null, "You dont have enough skills points");
    }


}                                     

private void btnhmActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:
    if(skillPoints >= 0 && skill1 > 1){
    skill1--;
    skillPoints++;
    txthealth.setText(String.valueOf(skill1));
     txtpoints.setText(String.valueOf(skillPoints));
    }
    else{
        JOptionPane.showMessageDialog(null, "Minimum skill value is 1.");
    }

}

There are more than one solution for this problem. 有多个解决此问题的方法。 Not all of them are efficient, not all are beautiful, but if it is "to make yourself more proficient in java, it is good to know the various ways! Here's some options: 并非所有这些方法都是有效的,也不是全部都是美丽的,但是如果“要使自己更精通Java,那么最好了解各种方法!这里有一些选择:

  1. You can change static int skill1 = 1; 您可以更改static int skill1 = 1; with public static int skill1 = 1; public static int skill1 = 1; , and thake that data by calling myCharacterStatsGUI.skill1 . ,然后通过调用myCharacterStatsGUI.skill1处理该数据。
  2. Better than the firs option, you can make this int private and create some public methods like public int getSkill1 , or public int getSkill(int numSkill) that returns the int value of the correct skill. 比firs选项更好,您可以将此int public int getSkill1 private并创建一些公共方法,例如public int getSkill1public int getSkill(int numSkill) ,它们返回正确技能的int值。
  3. A lot of programmer suggest that the GUI shouldn't related with the data and so, you can create a class MainCharacter , that contains all the character values, skills included. 许多程序员建议GUI不应与数据相关,因此,您可以创建一个MainCharacter类,其中包含所有字符值和所包含的技能。 This class must implements the option 2 methods. 此类必须实现选项2方法。
  4. Better than the third point, you can separate the MainCharacter class from its values, and store all the skills in a inner class Skills. 比第三点更好的是,您可以将MainCharacter类与其值分开,并将所有技能存储在内部类Skills中。 When you want to pick up these values by calling a specific method. 当您想通过调用特定方法来获取这些值时。 In the following code I want to show you this option: 在下面的代码中,我想向您显示此选项:

     public Class MainChar{ private Skills skills; public MainChar(){ this.skills=new Skills(); } /* Call it from your CharacterStatsGUI class, in the method txthealthActionPerformed */ public void augmentSkill(){ skills.augment() } public int getHealt(){ return skills.getHealt(); } private class Skills{ /*This example use only a skill, but you can extend it with others values*/ private int health; public Skills(){ health=1; } public void augment(){ health++; } public int getHealt(){ return health; } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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