如何在 JButton 矩阵中创建事件?
How to make an event in a matrix of JButtons?
我正在尝试创建 MouseListener
。当我悬停 JButton
时,我希望它更改其背景颜色和数组中的下一个 JButton
。例如,当我悬停JButton[0][0]
时,它会改变JButton[0][0]
、JButton[1][0]
、JButton[2][0]
等的背景。
下面是我创建 JButton
数组的方法:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
btn[i][j] = new JButton();
btn[i][j].addMouseListener(this);
btn[i][j].setBackground(Color.black);
panel.add(btn[i][j]);
}
}
及其 MouseListener
:
@Override
public void mouseEntered(MouseEvent me) {
JButton event = (JButton) me.getSource();
int i = 0;
int j = 0;
btn[i][j] = event;
btn[i][j].setBackground(Color.blue);
}
@Override
public void mouseExited(MouseEvent me) {
JButton event = (JButton) me.getSource();
int i = 0;
int j = 0;
btn[i][j] = event;
btn[i][j].setBackground(Color.black);
}
我试过 btn[i+1][j].setBackground(Color.black);
并且它设置为蓝色 [1][0]
、[2][0]
... 但不是 [i+1][j]
.
我运行我的程序没有错误。
上图显示了我正在尝试做的事情。
无需引用数组——您只需更改通过 getSource()
返回的按钮的状态即可。例如,
@Override
public void mouseEntered(MouseEvent me) {
JButton event = (JButton) me.getSource();
event.setBackground(Color.blue);
}
同样适用于 mouseExited。
如果您需要知道特定鼠标的 i 和 j,则使用嵌套的 for 循环遍历数组,
int i = 0;
int j = 0;
for (int i2 = 0; i2 < btn.length; i2++) {
for (int j2 = 0; j2 < btn[i2].length; j2++) {
if (event == btn[i2][j2]) {
i = i2;
j = j2;
}
}
}
// i and j set to appropriate value
或获取并设置按钮的客户端 属性 与完成的类似 . If you need more detailed help, then create and post a valid minimal example program
你要找的方法叫做Component.dispatchEvent(AWTEvent e)
,我想出了一个一维的例子:
package scratch.pad.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;
/**
* Propagate event to neighbor buttons
*/
public class EventPropagation extends JFrame {
private List<JButton> buttons = new ArrayList<>();
private class MouseEventPropagationListener extends MouseAdapter {
// The event source, for controlling the propagation of the event.
// To prevent infinite loop, we only dispatch the event to targets when
// e.getSource() == this.source;
private JButton source;
// Targets to propagate the event.
private java.util.List<JButton> targets;
public MouseEventPropagationListener(JButton source, JButton ... targets) {
this.source = source;
this.targets = Arrays.asList(targets);
}
@Override
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
// Use this.source because e.getSource() could be different.
this.source.setBackground(Color.WHITE);
dispatchEvent(e);
}
@Override
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
this.source.setBackground(Color.BLACK);
dispatchEvent(e);
}
private void dispatchEvent(MouseEvent e) {
if (e.getSource() == source) {
for (JButton target : targets) {
target.dispatchEvent(e);
}
}
}
}
public EventPropagation() throws HeadlessException {
final int n = 10;
this.setTitle("Event propagation test");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout());
this.setSize(300, 300);
this.setResizable(false);
// Create the buttons
for (int i = 0; i < n; i++) {
JButton btn = new JButton("Button " + i);
btn.setBackground(Color.BLACK);
this.buttons.add(btn);
this.getContentPane().add(btn);
}
// Setup propagation:
for (int i = 0; i < n; i++) {
JButton btn = this.buttons.get(i);
MouseEventPropagationListener listener = new MouseEventPropagationListener(btn, getNeighbors(i));
btn.addMouseListener(listener);
}
}
private JButton [] getNeighbors(int i) {
List<JButton> neighbors = new ArrayList<>();
if (i > 0) neighbors.add(this.buttons.get(i-1));
if (i < this.buttons.size() - 1) neighbors.add(this.buttons.get(i + 1));
return neighbors.toArray(new JButton[0]);
}
public static void main(String [] args) {
EventPropagation ep = new EventPropagation();
ep.setVisible(true);
}
}
我正在尝试创建 MouseListener
。当我悬停 JButton
时,我希望它更改其背景颜色和数组中的下一个 JButton
。例如,当我悬停JButton[0][0]
时,它会改变JButton[0][0]
、JButton[1][0]
、JButton[2][0]
等的背景。
下面是我创建 JButton
数组的方法:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
btn[i][j] = new JButton();
btn[i][j].addMouseListener(this);
btn[i][j].setBackground(Color.black);
panel.add(btn[i][j]);
}
}
及其 MouseListener
:
@Override
public void mouseEntered(MouseEvent me) {
JButton event = (JButton) me.getSource();
int i = 0;
int j = 0;
btn[i][j] = event;
btn[i][j].setBackground(Color.blue);
}
@Override
public void mouseExited(MouseEvent me) {
JButton event = (JButton) me.getSource();
int i = 0;
int j = 0;
btn[i][j] = event;
btn[i][j].setBackground(Color.black);
}
我试过 btn[i+1][j].setBackground(Color.black);
并且它设置为蓝色 [1][0]
、[2][0]
... 但不是 [i+1][j]
.
我运行我的程序没有错误。
上图显示了我正在尝试做的事情。
无需引用数组——您只需更改通过 getSource()
返回的按钮的状态即可。例如,
@Override
public void mouseEntered(MouseEvent me) {
JButton event = (JButton) me.getSource();
event.setBackground(Color.blue);
}
同样适用于 mouseExited。
如果您需要知道特定鼠标的 i 和 j,则使用嵌套的 for 循环遍历数组,
int i = 0;
int j = 0;
for (int i2 = 0; i2 < btn.length; i2++) {
for (int j2 = 0; j2 < btn[i2].length; j2++) {
if (event == btn[i2][j2]) {
i = i2;
j = j2;
}
}
}
// i and j set to appropriate value
或获取并设置按钮的客户端 属性 与完成的类似
你要找的方法叫做Component.dispatchEvent(AWTEvent e)
,我想出了一个一维的例子:
package scratch.pad.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;
/**
* Propagate event to neighbor buttons
*/
public class EventPropagation extends JFrame {
private List<JButton> buttons = new ArrayList<>();
private class MouseEventPropagationListener extends MouseAdapter {
// The event source, for controlling the propagation of the event.
// To prevent infinite loop, we only dispatch the event to targets when
// e.getSource() == this.source;
private JButton source;
// Targets to propagate the event.
private java.util.List<JButton> targets;
public MouseEventPropagationListener(JButton source, JButton ... targets) {
this.source = source;
this.targets = Arrays.asList(targets);
}
@Override
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
// Use this.source because e.getSource() could be different.
this.source.setBackground(Color.WHITE);
dispatchEvent(e);
}
@Override
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
this.source.setBackground(Color.BLACK);
dispatchEvent(e);
}
private void dispatchEvent(MouseEvent e) {
if (e.getSource() == source) {
for (JButton target : targets) {
target.dispatchEvent(e);
}
}
}
}
public EventPropagation() throws HeadlessException {
final int n = 10;
this.setTitle("Event propagation test");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout());
this.setSize(300, 300);
this.setResizable(false);
// Create the buttons
for (int i = 0; i < n; i++) {
JButton btn = new JButton("Button " + i);
btn.setBackground(Color.BLACK);
this.buttons.add(btn);
this.getContentPane().add(btn);
}
// Setup propagation:
for (int i = 0; i < n; i++) {
JButton btn = this.buttons.get(i);
MouseEventPropagationListener listener = new MouseEventPropagationListener(btn, getNeighbors(i));
btn.addMouseListener(listener);
}
}
private JButton [] getNeighbors(int i) {
List<JButton> neighbors = new ArrayList<>();
if (i > 0) neighbors.add(this.buttons.get(i-1));
if (i < this.buttons.size() - 1) neighbors.add(this.buttons.get(i + 1));
return neighbors.toArray(new JButton[0]);
}
public static void main(String [] args) {
EventPropagation ep = new EventPropagation();
ep.setVisible(true);
}
}