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

类RollPanel

    public int getbrainCount()
    {
    return brainCount;
    }


类BrainsPanel(我尝试在其中发送变量的地方)

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


这不会产生任何错误,但是当我将brainCount添加到标签以查看其值时,它始终为0。

在RollPanel的类级别上声明brainCount,然后在按钮侦听器中给此代码赋值:

        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()会在按钮侦听器之后吗?我觉得我在这里忽略了一些简单的事情...

编辑:
我认为问题不在于setter,因为它是getter。由于在RollPanel(getter所在的位置)中,我将brainCount声明为一个int,其初始值为0,因此,即使应该在按钮侦听器中更改brainCount,当getter获得该值时,它始终保持为0。这是RollPanel的全部内容。我怎样才能使吸气剂获得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

最佳答案

在这种和平的代码中:

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


在获取RollPanel之前,总是创建一个新的brainCount对象。如果在RollPanel中声明为字段,则当然为0。该变量是使用刚创建的对象刚初始化的行。

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

关于java - 将变量传递给另一个类(Java),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27164907/

10-10 03:19