简体   繁体   中英

Storing values from textfields in jframe class in a object in another class

I'm quite new to programming and java in general. I am currently a student and I'm trying to do a project outside of the classroom to make myself more proficient in 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. 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. 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:

  1. You can change static int skill1 = 1; with public static int skill1 = 1; , and thake that data by calling 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.
  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. This class must implements the option 2 methods.
  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. 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; } } } 

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