简体   繁体   English

将变量从按钮按下传递到另一个类

[英]Passing variable from button press to another class

So I am trying to pass a variable from a button press in one class to another class, and can't quite figure it out. 因此,我试图将一个按钮中的变量从一个类传递给另一类,并且无法完全弄清楚。 The button press creates a random number to simulate a dice roll, adds it to a variable which then is suppose to be passed to the board class which has the game board built on it and will then use said variable to determine which space on the board the player is on. 按下按钮会创建一个随机数字来模拟掷骰子,将其添加到变量中,然后假设该变量将传递到构建有游戏棋盘的棋盘类,然后将使用该变量来确定棋盘上的空间播放器已开启。 Thank you in advance. 先感谢您。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class Game extends JPanel{
private JLabel lblP1Name, lblP2Name, lblRules, lblDiceRoll;
private JTextField txtP1Name, txtP2Name;
private JButton diceRoll;
private JRadioButton rdP1, rdP2;
private int dice;
private static int countP1;
private int countP2;
private JPanel panelNorth;

private void groupButton( ) {

    ButtonGroup bg1 = new ButtonGroup( );

    bg1.add(rdP1);
    bg1.add(rdP2);

    }
public Game() throws FileNotFoundException {
setLayout (new BorderLayout());




rdP1 = new JRadioButton("Player 1");
rdP2 = new JRadioButton("Player 2");
ButtonListener listener = new ButtonListener();
Player1 player1 = new Player1(countP1);
Player2 player2 = new Player2(countP2);
Board board = new Board();
Rules rules = new Rules();
JButton diceRoll = new JButton("Roll the dice!");
panelNorth = new JPanel();
panelNorth.setLayout(new GridLayout(1,3));
lblRules = new JLabel(rules.toString());

add(panelNorth, BorderLayout.NORTH);
panelNorth.add(rdP1);
panelNorth.add(diceRoll);
panelNorth.add(rdP2);

Card card = new Card();

add(player1, BorderLayout.WEST);
add(player2, BorderLayout.EAST);
add(lblRules, BorderLayout.SOUTH);
add(board, BorderLayout.CENTER);
diceRoll.addActionListener(listener);
}
private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent diceRoll){
        Random random = new Random();
        dice = random.nextInt(6)+1;


        if(rdP1.isSelected()){
            countP1 = countP1+dice;
            if(countP1>48){
                countP1=countP1-48;

            }
        }else if(rdP2.isSelected()){
            countP2 = countP2+dice;
            if(countP2>48){
                countP2=countP2-48;

            }
        }

    }
}


}

It's simple; 这很简单; just use references. 只需使用引用。

Instance your classes and pass the reference: 实例化您的课程并传递参考:

Board board = new Board();
YourClass yourClass = new YourClass(board);

This way you can set Board attributes from the class YourClass. 这样,您可以从类YourClass设置Board属性。 It's really easy, you could have learned how to do this just by reading basic Java books. 这真的很容易,您只要阅读基本的Java书籍就可以学习如何做到这一点。

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

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