由于 GUI 中的 ListSelectionListener
和 ActionListener
方法,我在正确传递值/变量时遇到问题。基本上,我收到来自同一问题的 (6) 六个错误。
无法从静态上下文引用非静态变量
我以前在完全理解这个错误时遇到过问题,并认为我遇到了这个错误。但是一旦我们开始学习 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();
}
}
我还有很多代码,但由于多种原因,我认为没有必要将其全部发布。 A) 播放器遵循我的其他 JLabels
设计的模板。 B) 编译期间的错误由依赖于 WeaponListListener
类的 getPlayerSelected()
方法引起。我确信这是我尝试将整数 weaponSlctd
从一个方法传递到下一个方法,以及从一个 extend JPanel
传递到父 JFrame 的方式
这就是一切。
如果我能得到一些帮助和见解,了解我做错了什么和/或如何更改我的代码,以允许我传递 JList
、JButton
,和其他最终用户选择变量放入主方法中。我对这样做的适当方式有分歧。我读过一些有关静态和非静态变量的其他线程,它们过去对我有帮助。但这是我第一次构建和操作 swing
和 awt
扩展来帮助使用 GUI。我相信,出于某种原因,将 ActionListener
和 ListSelectionListener
功能合并到一起已经让我陷入了困境。
最佳答案
non-static variable cannot be referenced from a static context
我曾经经常遇到此错误...好吧,所以您有两种选择:
- 您将字段设为静态
- 通过创建类(对象)的实例使您的方法成为非静态
使用该方法时,我建议您创建该类的一个对象,并通过执行以下操作来调用它:
Object.method();
或者,您可以将字段本身设为静态,并通过静态变量访问它。
静态只能访问更多的静态。静态无法访问实例。
静态方法和变量不属于类的实例,这就是为什么不能将它们与对象一起使用。因此,再次强调,要么将所有内容设为静态,要么使用对象(该类的实例)访问方法和变量。
关于java - 从 ListSelectionListeners 和 ActionListeners 传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34236374/