[英]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.