从 ListSelectionListeners 和 ActionListeners 传递变量

Passing Variables From ListSelectionListeners and ActionListeners

由于我的 GUI 中的 ListSelectionListenerActionListener 方法,我在正确传递 values/variables 时遇到了问题。基本上,我收到 (6) 六个来自同一问题的错误。

non-static variable cannot be referenced from a static context

我以前在完全理解这个错误时遇到过问题,并认为我遇到了它。但是一旦我们开始学习 GUI 以及如何实现它们,我又失去了理解。理想情况下,我希望做出最终用户的决定并将其传递给主要的 GUI 功能。下面是 public class EnemyPanel extends JPanel 的一部分,它来自我的主要 JFrame 程序。在我尝试执行以下操作之前一切都很好:

public class PlayerPanel extends JPanel
{

private JPanel compPlayerPanel;
private JPanel playerPanel;
private JList playerList;
private JPanel playerListPanel;
private JLabel playerImageLabel;
private JPanel playerImagePanel;
private JPanel buttonPanel;
private JButton selctButton;
private ImageIcon playerImage;
private String playerSlctd;
public int playerChoice;
public int playerWeaponChoice;


private void buildWeaponSelectionPanel()
{
    weaponPanel = new JPanel();

    weaponListPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    weaponListPanel.setPreferredSize(new Dimension(180, 85));
    weaponListPanel.setBackground(Color.WHITE);
    weaponPanel.setLayout(new BoxLayout(weaponPanel, BoxLayout.Y_AXIS));

    weaponList = new JList(weapons);

    weaponList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    weaponList.addListSelectionListener(new WeaponListListener());
    weaponListPanel.add(weaponList);
    weaponPanel.add(weaponListPanel);
}
private class WeaponListListener implements ListSelectionListener
{
    public void valueChanged(ListSelectionEvent e)
    {
        weaponSlctd = (String) weaponList.getSelectedValue();

        if (weaponSlctd == "Mace")
        {
            weaponImage = new ImageIcon("Mace.png");
            playerWeaponChoice = 1;
        }
        else if(weaponSlctd == "Short Sword")
        {
            weaponImage = new ImageIcon("ShortSword.png");
            playerWeaponChoice = 2;
        }
        else if(weaponSlctd == "Long Sword")
        {
            weaponImage = new ImageIcon("LongSword.png");
            playerWeaponChoice = 3;
        }
        else if(weaponSlctd == "Axe")
        {
            weaponImage = new ImageIcon("Axe.png");
            playerWeaponChoice = 4;
        }

        weaponImageLabel.setIcon(weaponImage);

        weaponImageLabel.setText(null);
    }
}

public int getPlayerWeaponSelected()
{
    return playerWeaponChoice;
}

基本上,最终用户从 JList 中进行选择,并根据他们的选择,显示 .png 并且他们的选择在上述方法中记录为 weaponSlctd。我的问题是我想获取该值并在我的 JFrame:

的以下方法中使用它
private class RunButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
                    playerWeaponType = PlayerPanel.getPlayerWeaponSelected();
    }
}

我还有很多代码,但由于多种原因,我觉得没有必要 post 所有这些。 A) 播放器遵循我的其他 JLabels 设计的模板。 B) 编译过程中的错误来自依赖于WeaponListListener class 的方法getPlayerSelected()。我很肯定,这是我尝试将整数 weaponSlctd 从一个方法传递到另一个方法,以及从一个 extended JPanel 传递给调用它的父 JFrame 的方式。

我希望能得到一些帮助并深入了解我做错了什么 and/or 我如何更改我的代码以允许我通过 JListJButton 和其他最终用户选择的变量放入 main 方法中。我在这样做的适当方式上脱节了。我已经阅读了一些关于静态和非静态变量的其他线程,它们在过去帮助过我。但这是我第一次构建和操作 swingawt 扩展来帮助使用 GUI。我相信出于某种原因,将 ActionListenerListSelectionListener 功能合并到组合中让我陷入了循环。

non-static variable cannot be referenced from a static context

我以前经常遇到这个错误......好的,所以你有两个选择:

  1. 您将字段设为静态
  2. 通过创建 class(对象)
  3. 的实例使您的方法非静态

使用该方法时,我建议您创建一个 class 的对象,然后通过以下方式调用它:

Object.method();

或者,您可以将字段本身设为静态,并通过静态变量访问它。

静态只能访问更多的静态。静态无法访问实例。

静态方法和变量不属于 class 的实例,这就是为什么不能将它们与对象一起使用的原因。因此,同样,要么将所有内容设为静态,要么使用对象访问方法和变量,该对象是 class.

的实例