如何访问另一个文件上的 JButton 或 Java 中的 class

How can I access a JButton on another file or class in Java

我在使用 JButton 时遇到问题。我需要在单击 goPauseButton 时更改它上的文本,但出现此错误:goPauseButton cannot be resolved。我是 Java 的新手,所以我开始尝试使用来自其他语言(例如 Free Pascal)的技术来解决这个问题。那里你需要参考按钮所在的class,然后是按钮。在我的代码中它看起来像这样:

PrisonersDilemma.goPauseButton.setText("Pause");

但是我得到这个错误:Cannot make a static reference to the non-static field PrisonersDilemma.goPauseButton

这是我的代码(到目前为止),我删除了不重要的东西:

主要class

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.util.Hashtable;

//...

public class PrisonersDilemma /* possible extends... */ {
    // declaring
    JFrame frame;
    PlayingField field;
    JPanel componentPanel;
    public JButton goPauseButton;

    public JPanel createComponentPanel() {

        componentPanel = new JPanel();
        componentPanel.setLayout(new GridLayout(2,6));
        
        // set goPauseButton
        goPauseButton = new JButton("GO!");
        goPauseButton.addActionListener(field);
        goPauseButton.setBounds(110,350, 80,20); // first coordinates, then size
        frame.add(goPauseButton);

        return componentPanel;
    }

    void buildGUI() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                field = new PlayingField();

                // set frame
                frame = new JFrame("Prisoners Dilemma");
                frame.add(field);

                createComponentPanel();

                frame.add(field, BorderLayout.CENTER);
                frame.setLocation(200, 200);
                frame.pack();
                frame.setVisible(true);

                frame.setSize(400, 450);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }

        } );
    }

Class 与 ActionEventHandler

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;

public class PlayingField extends JPanel 
                          implements ActionListener, 
                                     ChangeListener {
    
    private boolean started;

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO
        if ("GO!".equals(e.getActionCommand())){
            System.out.println("GO!");
            started = true;

            goPauseButton.setText("Pause"); // here is the error

        } else if ("Pause".equals(e.getActionCommand())){
            System.out.println("Pause");
            started = false;
        } else if ("Reset".equals(e.getActionCommand())){
            System.out.println("Reset");
        }
    }
}

我认为您需要改变处理问题的方式。 PlayingField 不负责修改 PrisonersDilemmagoPauseButton 的状态。相反,PrisonersDilemma 应该更新 goPauseButton 并调用 PlayingField

的适当方法

例如...

goPauseButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        goPauseButton.setText("Pause");
        field.start();
    }
});

还有...

public class PlayingField extends JPanel {

    public void start() {
        System.out.println("GO!");
        started = true;
    }

    public void pause() {
        started = false;
        System.out.println("Pause");
    }

    public void reset() {
        System.out.println("Reset");
    }
}