如何在静态实例中向JButton添加ActionListener?

IHa*_*one 3 java swing

我在任何地方都找不到任何工作,而且代码似乎与你在applet中的方式不同,我已经习惯了.我是新手.我怎样才能在我的JButtons中添加一个ActionListener(就是它?还是我在寻找其他东西?)?(我不确定是什么原因导致该code块变得如此不可思议,我无法解决它.)

`

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main implements ActionListener{

static String answerOne = "";
static String answerTwo = "";
static boolean answerable = false;
static int labelX = 240, labelY = 48;
static int questionWrite = 0;
static String text = "Welcome! I will ask simple, two-answer questions, and you will answer them. Simple as that. ";
static int charIndex = 0;

private static void createAndShowGUI() {
    JFrame frame = new JFrame("FrameDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel emptyLabel = new JLabel("");
    emptyLabel.setPreferredSize(new Dimension(300, 225));
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);

    frame.setLayout(null);
    frame.setResizable(false);

    JPanel buttons = new JPanel(new FlowLayout());
    final JButton buttonOne = new JButton(answerOne);
    final JButton buttonTwo = new JButton(answerTwo);
    final JButton buttonThree = new JButton("Continue");
    buttonThree.setActionCommand("continue");

    if (answerable == true) {
    buttons.add(buttonOne);
    buttons.add(buttonTwo);
    } else {
        buttons.add(buttonThree);
    }

    frame.add(buttons);

    final JLabel centerText = new JLabel("<html>");
    frame.add(centerText);

    buttons.reshape(50, 185, 200, 40);
    centerText.reshape(10, 10, 280, 160);

    Timer timer = new Timer(50, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String labelText = centerText.getText();
        labelText += text.charAt(charIndex);
        centerText.setText(labelText);
        charIndex++;
        if (charIndex >= text.length()) {
            ((Timer)e.getSource()).stop();
        }

        Object buttonClicked = e.getSource();
        if(buttonClicked == buttonOne) {
            System.out.println("One.");
        }
        else if(buttonClicked == buttonTwo) {
            System.out.println("Two.");
        }
        else if(buttonClicked == buttonThree) {
            System.out.println("Continuing.");
        }

        //writeNext(Text to write, button one text, button two text, yes/no or "continue"); 
        switch(questionWrite) {
        case 0:
            writeNext("Welcome! I will ask simple, two-answer questions, and you will answer them. Simple as that. ", null, null, false);
            break;
        case 1:
            writeNext("Is Canada the largest country?", "Yes", "No,", true);
            break;
        case 2:
            writeNext("Have humans been to Mars?", "Yes", "No", true);
            break;
        }
    }

});

    timer.start();
}

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });



}
public static void writeNext(String textM, String answerOneM, String answerTwoM, boolean answerableM) {
        text = textM;
        answerOne = answerOneM;
        answerTwo = answerTwoM;
        answerable = answerableM;
    }

@Override
public void actionPerformed(ActionEvent arg0) {

}
Run Code Online (Sandbox Code Playgroud)

}`

Cru*_*her 5

button.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        //action listener here
    }
});
Run Code Online (Sandbox Code Playgroud)

由于必须传入一个对象addActionListener,因此无法从静态上下文中执行此操作.因此,您创建一个匿名类的实例,并将其传递给您的动作事件处理程序.

或者,您可以actionPerformed在班级中编写该方法.尽管如此,最好给它一个构造函数来识别它.

private int button;
public Main(int button)
{
    this.button = button;
}
Run Code Online (Sandbox Code Playgroud)

然后添加动作侦听器:

button.addActionListener(new Main(1)); //or pass it 2, 3, 4 ... just some unique integer
Run Code Online (Sandbox Code Playgroud)

在你的actionPerformed中你可以读取你传入的整数.

if(this.button==1)
{
    //do button 1 stuff
}
Run Code Online (Sandbox Code Playgroud)