- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 15 x 15 的 JButton
二维数组。
当我按住其中一个按钮(即 Button[i][j]
)时,我想更改相邻按钮的颜色(即 Button[i-1][j]
、Button[i+1][j]
、Button [i][j-1]
,按钮[i][j+1]
)。
我该怎么做?
下面是我的二维数组实现的一部分。该按钮目前不执行任何操作。
fb = new JButton[15][15];
for(int i = 0; i < 15; i++){
for(int j = 0; j < 15; j++){
fb[i][j] = new JButton();
fb[i][j].setBackground(Color.WHITE);
fb[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
fb[i][j].setPreferredSize(new Dimension(40, 40));
//fb[i][j].setEnabled(false);
grid.add(fb[i][j]);
}
}
最佳答案
尝试这个 block :
if (i - 1 >= 0) {
if (fb[i - 1][j] != null) {
fb[i - 1][j].setBackground(Color.RED);
}
} else if (i + 1 < 15) {
if (fb[i + 1][j] != null) {
fb[i + 1][j].setBackground(Color.RED);
}
} else if (j - 1 >= 0) {
if (fb[i][j - 1] != null) {
fb[i][j - 1].setBackground(Color.RED);
}
} else if (j + 1 < 15) {
if (fb[i][j + 1] != null) {
fb[i][j + 1].setBackground(Color.RED);
}
}
和监听器:
fb[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
int x, y;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if(fb[i][j].equals(b)){
x = i;
y = j;
break;
}
}
}
if (x - 1 >= 0) {
if (fb[x - 1][y] != null) {
fb[x - 1][y].setBackground(Color.RED);
}
} else if (x + 1 < 15) {
if (fb[x + 1][y] != null) {
fb[x + 1][y].setBackground(Color.RED);
}
} else if (y - 1 >= 0) {
if (fb[x][y - 1] != null) {
fb[x][y - 1].setBackground(Color.RED);
}
} else if (y + 1 < 15) {
if (fb[x][y + 1] != null) {
fb[x][y + 1].setBackground(Color.RED);
}
}
}
});
关于Java:使用MouseListener更改相邻JButton的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42947061/
我有一个程序,我必须在其中更改对象的状态,我想知道是否有人能够在外部创建 MouseListener 类或监听器类,该类仍然具有与对象交互的代码,允许他们更改字段从原始类中取出,然后将它们交换出来,如
我有一个 JPanel parent 和一个 JPanel child。我在父级和子级中添加了一个 MouseListener。这是代码: public void init(MouseListener
我需要先说明一下,我的导师不允许我们使用 IDE。我们使用文本板。我想单击此标签,然后它从“H”更改为“T”。目前,当我单击标签时什么也不做。我忘记了什么? import javax.swing.*;
我试图在屏幕上出现一个圆圈并跟随鼠标移动。 (最终我将把它变成一个带有光线转换的游戏)我正在使用 MouseMotionListener 并尝试使用 mouseMoved 方法在我的 JPanel 中
我有一个类,它实现 MouseListner。 如果我将此 class 实例设置为 null ,它会从 JPanel 中删除 MouseListener 吗?或者我是否必须使用 removeMouse
所以我尝试使用 Java 图形创建一个视频游戏,并且我需要使用 MouseListener 作为菜单屏幕上的“播放”按钮。当我按下按钮时,只要我按住按钮,它就会重复打印测试文本。我认为这就像编写只允许
我是 Java 新手,希望实现以下目标。 我有各种组合框。对于每个组合框,我都有一个按钮可以清除组合框的内容。清除按钮是从我创建的通用 ClearComboBoxButton 类创建的。作为此 Cle
我有 4 JLabel s。 首次点击时: 我将背景颜色更改为红色并删除了 JLabel的MouseListener我点击过的。 第二次点击时: 我将背景颜色更改为绿色,但是JLabel我之前单击的不
我的游戏中处理射击的 MouseListener 在使用单独的鼠标时没有响应,但在使用笔记本电脑上的内置鼠标时却没有响应。在我的播放器类中,mouseClicked 方法如下所示: public vo
在我目前制作的游戏中,我有三种不同的 mousePressed 方法,一种用于单次射击,一种用于自动射击,一种用于近战攻击。因为自动开火的方法使用了 Swing Timer,所以我可以在其他 mous
我有堆叠条形图,我希望能够在其中选择堆栈中的单个条形图。但是 ChartMouseListener 不会将 ChartMouseEvent 解析为对应的 ChartEntity。这是监听器片段:
对于我正在制作的这款游戏,我真的很想得到一些帮助。我已经尝试了最长的时间,但我却找不到解决方案。目前它的作用不大。基本上,我需要鼠标监听器寻找左键单击并以一种方式改变蛇的方向,另一种方式用于右键单击。
(注意:我知道 MouseAdapter 类的存在,但由于我稍后可能会覆盖所有方法,所以它的优势就消失了吗? ) 我有一个 MainProgram.java 类,我在其中添加了几个组件。他们中的大多数
我有一个 JPanel(网格),嵌套在主 JFrame 中。我正在尝试在网格上设置一个 Mouselistener 并将事件报告给主框架。 App.java (入口点) public class Ap
我有一个 JInternalFrame,我想在双击 JTable 时在其中显示 JOptionPane。我在互联网上查了一下,发现唯一的方法是重写 mousePressed() 方法,这就是我的做法:
我在 java 中使用 swing 创建了一个圆形 strip ,现在我想在鼠标单击 strip 的特定区域(例如 45 度到 135 度中心角之间的区域)时显示一些文本,有人可以帮我吗? impor
有谁知道以下是否是触发 MouseEvent 方面的预期行为?其中一颗似乎在其他地方丢失或被消耗掉。 右键单击JPanel并显示JPopupMenu: MousePressed java.awt.ev
我正在使用 Java 开发一款游戏,并面临以下挑战。 我有 2 个 JPanel,需要以可视方式将形状从一个 JPanel 拖动到另一个。我已经使用 JFrame 中的 GlassPane 完成了这项
我有一个类,它扩展了JPanel并包含一个带有MouseListener的JLabel。我有一个扩展它的子类,我希望它具有相同的 JLabel,但我希望 MouseListener 执行不同的操作。代
我知道在类(class)的开头您可以编写 implements ActionListener 然后您必须创建 public void actionPerformed(ActionEvent e) 才能
我是一名优秀的程序员,十分优秀!