简体   繁体   English

如何将另一个类中的变量调用到 ActionListener 方法中?

[英]How to call variables from another class into an ActionListener method?

I would like to apologize in advance I am new to java and don't know very much so stick with me please.我想提前道歉,我是 Java 新手,不太了解,所以请坚持。 I would like to know how to get my character attributes from one ActionListener method in one class to another ActionListener method in another class.我想知道如何将我的角色属性从一个类中的一个 ActionListener 方法获取到另一个类中的另一个 ActionListener 方法。 I would like to get the inputs from the user about player1 in one class and then use them in the other class.我想在一个班级中从用户那里获取有关 player1 的输入,然后在另一班级中使用它们。 Please help, and I appreciate your time.请帮忙,我很感激你的时间。

public void actionPerformed(ActionEvent event) {
    // TODO Auto-generated method stub

    if(event.getSource() == create){

        Character player1 = new Character( Integer.parseInt(strength.getText()), Integer.parseInt(defense.getText()), Integer.parseInt(health.getText()) , Integer.parseInt(dexterity.getText()));
        player1.name = name.getText();

        JOptionPane.showMessageDialog(this, "\nName: " + player1.name + "\nHealth: " + player1.health + "\nStrength: " + player1.strength + "\nDefense: " + player1.defense + "\nDexterity: " + player1.dexterity);
        dispose();//To close the current window

       GameWindow gwindow = new GameWindow();
        gwindow.setVisible(true);//Open the new window
    }

put into投入

@Override
public void actionPerformed(ActionEvent event) {

    Object source = event.getSource();
    Character Goblin = new Character(10, 3, 6, 10);
    Character Witch = new Character(2, 7, 3, 20);
    Character Zombie = new Character(5, 5, 5, 15);
    int damage;

    if (event.getSource() == right) {

        label1.setText("You have encountered a goblin!");
        label2.setText("Do you wish to fight or flee?");
        fight.setVisible(true);
        flee.setVisible(true);
    }

        if(event.getSource() == fight) {

            System.out.println(player1 + " VS. " + Goblin.name);

            while(player1.isAlive() && Goblin.isAlive()){

                // player 1 attack
                damage = player1.attack(Goblin);
                System.out.println(player1.name + " hits " + Goblin.name + " for " + damage + " damage.");

                // player 2 attack
                damage = Goblin.attack(player1);
                System.out.println(Goblin.name + " hits " + player1.name + " for " + damage + " damage.\n");
            }

            // Check to see who won
            if( player1.isAlive()){
                System.out.println(player1.name + " wins!");
            }
            else{
                System.out.println("You have perished");
            }

        }
}

Declare Player1 as public Static member So it's Value can;t be changed.将 Player1 声明为公共静态成员,因此它的值不能更改。

and You can use player1 Through the object of that particular Class.并且你可以使用 player1 通过那个特定类的对象。

    Class First{
          //Declare That Character object as a static public here
          //Player1;
           }
    Class Second{
          //First Create Object Of that class....
          First f = new First(//Parameter For Constructor);
          f.Player1;

           }

Change your GameWindow constructor like this.像这样更改您的GameWindow构造函数。

class GameWindow extends JFrame implements ActionListener{
    private Character player1;

    public GameWindow(Character player1){
        this.player1 = player1;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        Character Goblin = new Character(10, 3, 6, 10);
        Character Witch = new Character(2, 7, 3, 20);
        Character Zombie = new Character(5, 5, 5, 15);
        int damage;

        if (event.getSource() == right) {

            label1.setText("You have encountered a goblin!");
            label2.setText("Do you wish to fight or flee?");
            fight.setVisible(true);
            flee.setVisible(true);
        }

        if(event.getSource() == fight) {

            System.out.println(player1 + " VS. " + Goblin.name);

            while(player1.isAlive() && Goblin.isAlive()){

                // player 1 attack
                damage = player1.attack(Goblin);
                System.out.println(player1.name + " hits " + Goblin.name + " for " + damage + " damage.");

                // player 2 attack
                damage = Goblin.attack(player1);
                System.out.println(Goblin.name + " hits " + player1.name + " for " + damage + " damage.\n");
            }

            // Check to see who won
            if( player1.isAlive()){
                System.out.println(player1.name + " wins!");
            }
            else{
                System.out.println("You have perished");
            }

        }
    }
}

And pass parameter to new contructor.并将参数传递给新的构造函数。

public void actionPerformed(ActionEvent event) {
    // TODO Auto-generated method stub

    if(event.getSource() == create){

        Character player1 = new Character( Integer.parseInt(strength.getText()), Integer.parseInt(defense.getText()), Integer.parseInt(health.getText()) , Integer.parseInt(dexterity.getText()));
        player1.name = name.getText();

        JOptionPane.showMessageDialog(this, "\nName: " + player1.name + "\nHealth: " + player1.health + "\nStrength: " + player1.strength + "\nDefense: " + player1.defense + "\nDexterity: " + player1.dexterity);
        dispose();//To close the current window

       GameWindow gwindow = new GameWindow(player1);
        gwindow.setVisible(true);//Open the new window
    }

put the static access modifier within the Class that contains the object you want to call in your class ActionListener and then call it in the ActionListener method.将静态访问修饰符放在包含要在类 ActionListener 中调用的对象的类中,然后在 ActionListener 方法中调用它。

Eg: class ClassName{

static JButton btn;

   public ClassName(){
   btn=new JButton("");
   add(btn)
  }
}
class Listener implements ActionListener{
   public void actionPerformed(ActionEvent e) {
 if(ClassName.btn.....) //with static access modifier you can 
//call an object from another class in this method;
 }
}

暂无
暂无

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

相关问题 如何将方法从另一个类连接到actionListener? - How to connect method from another class to actionListener? 如何从另一个 class 调用方法以在我的 ActionListener class 中使用 - How do I call a method from another class to use in my ActionListener class 从ActionListener方法访问类变量 - Accessing class variables from ActionListener method 如何在另一个类中调用ActionListener中的Java类 - How to call Java class in ActionListener in another class 从另一个类中的方法创建actionlistener - Creating an actionlistener from a method in another class 如何从一个actionListener返回一个数组列表,然后将其调用到另一个actionListener? - how to return an array-list from a actionListener and then call it to another actionListener? Java:使用actionlistener在该类的对象上调用另一个类中的函数 - Java: Using an actionlistener to call a function in another class on an object from that class 如何从同一类的另一个方法中调用变量,以及如何调用该方法? - How do I call for variables from another method in same class, as well as call upon the method? 我的JButton的ActionListener如何访问另一个类中的变量? - How can my ActionListener for a JButton access variables in another class? 如何在匿名内部actionListener类和actionPerformed方法中访问局部变量? - How to access local variables in anonymous inner actionListener class and actionPerformed method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM