行动听众 [英] Action Listeners

查看:105
本文介绍了行动听众的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用按钮上附带的动作侦听器。以及如何使它在单独的类文件中工作?

How do I use the action listeners that are attached to the buttons. And how would I make it work from a separate class file?

编辑:
这就是我所拥有的:

This is what i have:

import java.awt.*; 

导入java.awt.event。*;

import java.awt.event.*;

公共类Gui {

static boolean playerturn = true;

public static void main(String[] args) {

    Frame frame1 =new Frame("TickTacToe - By Fred");
    frame1.setLayout(null);
    frame1.setBounds(250,150,500,500);
    frame1.setVisible(true);
    frame1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    });

    final Button button11 = new Button("");
    button11.addActionListener(null);

    final Button button12 = new Button("");
    button11.addActionListener(null);

    final Button button13 = new Button("");
    button11.addActionListener(null);

    final Button button21 = new Button("");
    button11.addActionListener(null);

    final Button button22 = new Button("");
    button11.addActionListener(null);

    final Button button23 = new Button("");
    button11.addActionListener(null);

    final Button button31 = new Button("");
    button11.addActionListener(null);

    final Button button32 = new Button("");
    button11.addActionListener(null);

    final Button button33 = new Button("");
    button11.addActionListener(null);


    button11.setBounds(100, 100, 80, 70);
    button12.setBounds(100, 200, 80, 70);
    button13.setBounds(100, 300, 80, 70);
    button21.setBounds(200, 100, 80, 70);
    button22.setBounds(200, 200, 80, 70);
    button23.setBounds(200, 300, 80, 70);
    button31.setBounds(300, 100, 80, 70);
    button32.setBounds(300, 200, 80, 70);
    button33.setBounds(300, 300, 80, 70);

    frame1.add(button11); 
    frame1.add(button12); 
    frame1.add(button13); 
    frame1.add(button21); 
    frame1.add(button22); 
    frame1.add(button23); 
    frame1.add(button31); 
    frame1.add(button32);
    frame1.add(button33); 
}}

我需要的是某种能够利用听众的方式。

What i need is someway of being able to utilize the listeners.

推荐答案


我如何使它在另一个类文件中起作用?

And how would i make it work form a different class file?

如果需要,导入它,然后创建它的实例。

Import it, if you need to, and then create an instance of it.


如何使用按钮上的动作监听器

how to use the action listeners that are attached to the buttons

放置要执行的逻辑在 ActionListener 类(或子类)的 actionPerformed(ActionEvent e)方法中。

Place the logic that you'd like to be executed within the actionPerformed(ActionEvent e) method of the ActionListener class (or subclass).

有关更多信息,请参见如何编写动作监听器

For more information, see How to Write an Action Listener.

这篇关于行动听众的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆