java中,定义一个二维JButton按钮数组,如何通过点击按钮来确定这个按钮数的坐标

小冷不冷 发布于 2012/03/28 16:46
阅读 7K+
收藏 0
如题
加载中
0
牛牛牛牛
牛牛牛牛
private JButton button = new JButton();

button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {

				JButton eventButton = (JButton) event.getSource();	//获取事件源

				//拿到按钮对象了,自己查API吧

			}

		});

没用2维,2维就把这个放循环体里

坐标有2种 相对窗体坐标 和 相对显示器的坐标

0
Jeky
Jeky

几个方法:

1、遍历查找,速度相对慢,不过对于小规模数组来说不是问题:

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

/**
 *
 * @author Jeky
 */
public class ShowIndexDemo extends JFrame implements ActionListener {

    public static final int BUTTON_ARRAY_SIZE = 5;

    public ShowIndexDemo() {
        super("Show Index Demo");
        this.setLayout(new GridLayout(BUTTON_ARRAY_SIZE, BUTTON_ARRAY_SIZE));
        buttons = new JButton[BUTTON_ARRAY_SIZE][BUTTON_ARRAY_SIZE];
        for (int i = 0; i < BUTTON_ARRAY_SIZE; i++) {
            for (int j = 0; j < BUTTON_ARRAY_SIZE; j++) {
                buttons[i][j] = new JButton("B");
                buttons[i][j].addActionListener(this);
                this.add(buttons[i][j]);
            }
        }

        this.pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < BUTTON_ARRAY_SIZE; i++) {
            for (int j = 0; j < BUTTON_ARRAY_SIZE; j++) {
                if (buttons[i][j] == e.getSource()) {
                    JOptionPane.showMessageDialog(this, "(" + i + "," + j + ")");
                }
            }
        }
    }

    public static void main(String[] args) {
        new ShowIndexDemo().setVisible(true);
    }
    private JButton[][] buttons;
}

2、新建一个类继承JButton

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

/**
 *
 * @author Jeky
 */
public class ShowIndexDemo extends JFrame implements ActionListener {

    public static final int BUTTON_ARRAY_SIZE = 5;

    public ShowIndexDemo() {
        super("Show Index Demo");
        this.setLayout(new GridLayout(BUTTON_ARRAY_SIZE, BUTTON_ARRAY_SIZE));
        buttons = new JButton[BUTTON_ARRAY_SIZE][BUTTON_ARRAY_SIZE];
        for (int i = 0; i < BUTTON_ARRAY_SIZE; i++) {
            for (int j = 0; j < BUTTON_ARRAY_SIZE; j++) {
                buttons[i][j] = new IndexButton("B", i, j);
                buttons[i][j].addActionListener(this);
                this.add(buttons[i][j]);
            }
        }

        this.pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        IndexButton button = (IndexButton) e.getSource();
        JOptionPane.showMessageDialog(this, "(" + button.x + "," + button.y + ")");
    }

    public static void main(String[] args) {
        new ShowIndexDemo().setVisible(true);
    }

    public static class IndexButton extends JButton {

        public IndexButton(String text, int x, int y) {
            super(text);
            this.x = x;
            this.y = y;
        }
        private int x;
        private int y;
    }
    private JButton[][] buttons;
}

 

0
liuex
liuex

两种方法:
1、ActionListener.actionPerformed(ActionEvent e)监听器上面,ActionEvent.getSource()就是触发事件的消息源,实际上就是触发点击事件的按钮
2、每个按钮都设置不同的action command, 参见JButton.setActionCommand(String)。然后在ActionEvent.getActionCommand()上面就能判断出到底哪个按钮触发的点击事件。

0
xoHome
xoHome
将坐标值绑定到JButton上
0
Monkey
Monkey
这个坐标到底是啥啊,是控件在的坐标点,还是按钮在数组里的索引坐标啊?
0
小冷不冷
小冷不冷
十分谢谢 各位 我会了
攸幽
攸幽
请问你是怎么做的啊
返回顶部
顶部