将变量传递给另一个类(Java)

时间:2014-11-27 07:05:53

标签: java variables

我有一个包含多个类的Java程序,所有这些类都扩展了JPanel(包含main方法的类除外)。我试图将int变量从一个类传递到另一个类,并使用一些先前提出的问题,提出了这个:

Class RollPanel

    public int getbrainCount()
    {
    return brainCount;
    }

Class BrainsPanel(我试图发送变量的地方)

   void setbrainCount()
   {
   RollPanel rollpanel = new RollPanel();
   brainCount = rollpanel.getbrainCount();
   }

这并没有给出任何错误,但是当我将brainCount添加到标签以查看其值时,它总是为0.

brainCount在RollPanel的类级别声明,然后使用此代码在按钮侦听器中给出一个值:

        value1 = generator.nextInt(3) + 1;
        value2 = generator.nextInt(3) + 1;
        value3 = generator.nextInt(3) + 1;
        //rollTotal++;

        //Counts how many brains were rolled.
        if (value1 == 1)
            brainCount++;
        if (value2 == 1)
            brainCount++;
        if (value3 == 1)
            brainCount++;

据我所知,只是声明一个变量会自动意味着它的值一开始就为零(我假设这是为什么它显示为零),但我怎样才能在其后传递更新的值上面的代码到brainsPanel所以我可以将它的值添加到brainsPanel中的标签? getBrainCount()会在按钮监听器之后出现吗?我觉得我在这里忽略了一些简单的事情......

编辑: 我认为问题不在于设定者,因为它是吸气剂。因为在RollPanel(getter是)中我将brainCount声明为int,其初始值为0,所以当getter获取值时,它始终保持为0,即使在按钮监听器中应更改brainCount。这是RollPanel的全部内容。如何让getter获取brainCount的更新值而不是其初始0?

package zombiedice;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class RollPanel extends JPanel
{
JLabel die1Label, die2Label, die3Label, testLabel;
JButton rollButton, sortButton;
JPanel dicePanel, buttonPanel;
ImageIcon die1, die2, die3;
int rollTotal, value1, value2, value3;
int brainCount, blastCount;
Random generator = new Random();



public RollPanel()
{
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
    setBackground(Color.black);

    dicePanel = new JPanel();
    dicePanel.setBackground(Color.black);

    //Creates blank ImageIcons to be used later to display dice.
    die1 = new ImageIcon(); 
    die2 = new ImageIcon(); 
    die3 = new ImageIcon(); 

    //A panel just to hold the buttons.
    buttonPanel = new JPanel();

    //Creates and links roll button to RollListener.
    rollButton = new JButton("Roll");
    rollButton.addActionListener(new RollListener());
    buttonPanel.add(rollButton);

    //After a roll, this button will need to be clicked so brain and blast
    //die can be sorted into their proper catergories. 
    sortButton = new JButton("Sort");
    sortButton.addActionListener(new SortListener());


    //Creates labels out of the dice images.
    die1Label = new JLabel(die1);
    die2Label = new JLabel(die2);
    die3Label = new JLabel(die3);

    //Adds image labels to the panel that holds the dice.
    dicePanel.add(die1Label);
    dicePanel.add(die2Label);
    dicePanel.add(die3Label);

    add(dicePanel);
    add(buttonPanel);
} //Closes constructor



//Roll button listener.
private class RollListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        rollButton.setEnabled(false);
        repaint();
        buttonPanel.add(sortButton);
        repaint(); 
        sortButton.setEnabled(true);
        repaint();

        value1 = generator.nextInt(3) + 1;
        value2 = generator.nextInt(3) + 1;
        value3 = generator.nextInt(3) + 1;
        //rollTotal++;

        //Counts how many brains were rolled.
        if (value1 == 1)
            brainCount++;
        if (value2 == 1)
            brainCount++;
        if (value3 == 1)
            brainCount++;

        //Updates the dice
        die1Label.setIcon(new ImageIcon(value1 + ".png"));
        die2Label.setIcon(new ImageIcon(value2 + ".png"));
        die3Label.setIcon(new ImageIcon(value3 + ".png")); 



    } //Closes actionPerformed
} //Closes the listener for the roll button



//Sort button listener.
private class SortListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        sortButton.setEnabled(false);
        repaint();
        rollButton.setEnabled(true);
        repaint();



    } //Closes actionPerformed.

}//Closes sort button listener. 
public int getBrainCount()
{
return brainCount;
} 

} //Closes class

BrainsPanel

package zombiedice;
import javax.swing.*;
import java.awt.*;

public class BrainsPanel extends JPanel
{
ImageIcon icon1 = new ImageIcon();
ImageIcon icon2 = new ImageIcon();
JLabel brainTotal, label1, label2;
JPanel brainPanel;
int brainCount;


void setbrainCount(int count)
{
   // RollPanel rollpanel = new RollPanel();
   brainCount = count;
   //brainCount = count;
}

public BrainsPanel()
{


setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 

setBackground(Color.black);


icon1 = new ImageIcon("1.png");
icon2 = new ImageIcon("1.png");
label1 = new JLabel(icon1);
label2 = new JLabel(icon2);
brainTotal = new JLabel ("Brains eaten: " + brainCount);

add(label1);
add(label2);
add(brainTotal);



} //Closes constructor
} //Closes class

2 个答案:

答案 0 :(得分:0)

在这个代码的和平中:

void setbrainCount() {
    RollPanel rollpanel = new RollPanel();
    brainCount = rollpanel.getbrainCount();
}

在获得RollPanel之前,您始终会创建一个新的brainCount对象。当然,如果它在RollPanel中声明为字段,则为0。变量是新初始化的,对象创建只是之前的行。

您应该确保始终使用相同的RollPanel对象,例如将其保留为BrainsPanel中的字段。用给定的代码很难肯定地说。

答案 1 :(得分:0)

我相信你应该尝试以下方法:

void setbrainCount(RollPanel rollPanel)
{
    brainCount = rollpanel.getbrainCount();
}

这样您就不会尝试使用新初始化的值,该值始终为0.

Althozuh从技术上讲,只需发送计数并设置

就更有意义了
void setbrainCount(int brainCount)
{
    this.brainCount = brainCount;
}